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 (将#修改为@)