美文网首页
Recycleview 拖拽后位置错乱

Recycleview 拖拽后位置错乱

作者: 念念不忘_2016 | 来源:发表于2021-08-30 16:37 被阅读0次

核心代码如下

@Override
public void onItemSwap(int from, int target) {
    if (from < target) {
        //从上往下拖动,每滑动一个item,都将list中的item向下交换,向上滑同理。
        for (int i = from; i < target; i++) {
            Collections.swap(mData, i, i + 1);//交换数据源两个数据的位置
        }
    } else if (from > target) {
        for (int i = from; i > target; i--) {
            Collections.swap(mData, i, i - 1);//交换数据源两个数据的位置
        }
    }
    //刷新列表数据
    notifyItemMoved(from, target);
    notifyItemRangeChanged(Math.min(from, target), Math.abs(from - target) +1);
}

相关文章

网友评论

      本文标题:Recycleview 拖拽后位置错乱

      本文链接:https://www.haomeiwen.com/subject/wmloiltx.html