首页 > 解决方案 > 切换activity后RecyclerView不返回原位

问题描述

我已经使用 ItemTouchHelper 类在回收器视图中实现了拖动项目,它运行良好,但是当我在活动之间切换时,项目位置恢复为默认值。我希望即使活动切换,回收站视图项目位置也保持不变。

public RecyclerViewItemTouchHelper(Context context, RecyclerView recyclerView) {
    super(ItemTouchHelper.UP | ItemTouchHelper.DOWN |
            ItemTouchHelper.START | ItemTouchHelper.END, ItemTouchHelper.LEFT);
    this.recyclerView = recyclerView;
    this.context = context;

}

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
    int fromPosition = viewHolder.getAbsoluteAdapterPosition();
    int toPosition = target.getAbsoluteAdapterPosition();

    Collections.swap(MainActivity.dataArrayList, fromPosition, toPosition);

    recyclerView.getAdapter().notifyItemMoved(fromPosition, toPosition);

    return false;
}

标签: javaandroidandroid-studioandroid-recyclerviewitemtouchhelper

解决方案


推荐阅读