gpt4 book ai didi

android - 如何捕捉与 RecyclerView 一起使用的 ItemTouchHelper 的 Drop Action

转载 作者:行者123 更新时间:2023-11-30 05:07:14 24 4
gpt4 key购买 nike

RecyclerViewItemTouchHelper 有问题。

我在做游戏。游戏板实际上是一个 RecyclerView。 RecyclerView 有 GridLayoutManager 和一些跨度数。我想实现拖放 recyclerview 的项目。任何项目都可以在所有方向(上、下、左、右)上拖动。

private void initializeLayout() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutFrozen(true);
recyclerView.setNestedScrollingEnabled(false);

// set layout manager
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), BOARD_SIZE,
LinearLayoutManager.VERTICAL, true);
recyclerView.setLayoutManager(layoutManager);

// Extend the Callback class
ItemTouchHelper.Callback itemTouchCallback = new ItemTouchHelper.Callback() {

public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Log.w(TAG, "onMove");
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Application does not include swipe feature.
}

@Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
Log.d(TAG, "onMoved");
// this is calling every time, but I need only when user dropped item, not after every onMove function.
}

@Override
public boolean isItemViewSwipeEnabled() {
return false;
}

@Override
public boolean isLongPressDragEnabled() {
return true;
}

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END;
int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
};

ItemTouchHelper touchHelper = new ItemTouchHelper(itemTouchCallback);
touchHelper.attachToRecyclerView(recyclerView);
}

那么,当我仍在 RecyclerView 上拖动项目时,为什么 ItemTouchHelper 的 onMoved 函数有效?我怎样才能做到这一点?

最佳答案

拖放项目时,可以多次调用onMove(),但会调用一次clearView()。所以你可以用它来表示拖动已经结束(发生了下降)。并使用两个变量 dragFrom 和 dragTo 来跟踪完成的“拖放”中的真实位置。

private ItemTouchHelper.Callback dragCallback = new ItemTouchHelper.Callback() {

int dragFrom = -1;
int dragTo = -1;

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT,
0);
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {

int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();


if(dragFrom == -1) {
dragFrom = fromPosition;
}
dragTo = toPosition;

adapter.onItemMove(fromPosition, toPosition);

return true;
}

private void reallyMoved(int from, int to) {
// I guessed this was what you want...
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

}

@Override
public boolean isLongPressDragEnabled() {
return true;
}

@Override
public boolean isItemViewSwipeEnabled() {
return false;
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);

if(dragFrom != -1 && dragTo != -1 && dragFrom != dragTo) {
reallyMoved(dragFrom, dragTo);
}

dragFrom = dragTo = -1;
}

};

adapter.onItemMove(fromPosition, toPosition) 如下所示:

public void onItemMove(int fromPosition, int toPosition) {
list.add(toPosition, list.remove(fromPosition));
notifyItemMoved(fromPosition, toPosition);
}

关于android - 如何捕捉与 RecyclerView 一起使用的 ItemTouchHelper 的 Drop Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com