ItemTouchHelper.SimpleCallback onMoveviewHoldersource和目标似乎已交换

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?