如何解决ItemTouchHelper.SimpleCallback onMoveviewHoldersource和目标似乎已交换
我遇到一个奇怪的问题。
我创建了自己的itemtouchhelper.SimpleCallback
:
public class SimpleitemtouchhelperCallback extends itemtouchhelper.SimpleCallback {
private MoveListener mMoveListener;
private int mDragFrom = -1;
private int mDragTo = -1;
public SimpleitemtouchhelperCallback(int dragDirs,int swipeDirs,MoveListener moveListener) {
super(dragDirs,swipeDirs);
mMoveListener = moveListener;
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView,@NonNull RecyclerView.ViewHolder viewHolder,@NonNull RecyclerView.ViewHolder target) {
// Notify Adapter of the moved item!
mMoveListener.onMove(viewHolder,target);
Log.d("DEBUG","viewHolder: " + viewHolder.getAdapterPosition() + " target: " + target.getAdapterPosition());
// save where dragging starts at first movement,otherwise when moving from position 3 to 1,// mDragFrom first will be the correct 3,but then will change to 2,as the last move is from 2 to 3
if(mDragFrom == -1) {
mDragFrom = viewHolder.getAdapterPosition();
}
mDragTo = target.getAdapterPosition();
Log.d("DEBUG","mDragFrom: " + mDragFrom+ " mDragTo: " + mDragTo);
return true;
}
@Override
public void clearView(@NonNull RecyclerView recyclerView,@NonNull RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView,viewHolder);
// clear view is only called once,so reallyMoved() is only called,when moving is finished and not a million times in between
// https://stackoverflow.com/questions/35920584/android-how-to-catch-drop-action-of-itemtouchhelper-which-is-used-with-recycle
if(mDragFrom != -1 && mDragTo != -1 && mDragFrom != mDragTo) {
mMoveListener.reallyMoved(mDragFrom,mDragTo);
}
mDragFrom = mDragTo = -1;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder,int direction) {
// No swipe action
}
@Override
public boolean isItemViewSwipeEnabled() {
// disable swipe (dont override this method or return true,if you want to have swipe)
return false;
}
@Override
public int getMovementFlags(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder) {
// Set movement flags to specify the movement direction
// final int dragFlags = itemtouchhelper.UP | itemtouchhelper.DOWN | itemtouchhelper.RIGHT | itemtouchhelper.LEFT; <-- for all directions
// In this case only up and down is allowed
final int dragFlags = itemtouchhelper.UP | itemtouchhelper.DOWN;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags,swipeFlags);
}
public interface MoveListener {
void onMove(RecyclerView.ViewHolder source,RecyclerView.ViewHolder target);
void reallyMoved(int dragFrom,int dragTo);
}
}
在onMove(@NonNull RecyclerView.ViewHolder viewHolder,@NonNull RecyclerView.ViewHolder target)
中,通常viewHolder
是源,target
是项目的移动位置。
但是正如我在日志中看到的那样,当将第一项移动到第二位置时,这是另一种方式。 这是我将第一项向下移动两个位置时的日志。
2020-10-08 14:32:49.352 5465-5465 / com.example.smartpremix D /调试:viewHolder:1目标:0
2020-10-08 14:32:50.634 5465-5465 / com.example.smartpremix D /调试:viewHolder:2目标:1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。