java - 切换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;
}
解决方案
推荐阅读
- python - Speed up Boto3 file transfer across buckets
- google-tag-manager - Google 跟踪代码管理器 - 计算延迟加载帖子的访问次数
- python-3.x - 从 Tensorflow_datasets 下载“Imdb_reviews”:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 30 invalid continuation byte
- flutter - 在 stream.listen() 完成后 Flutter-Firebase 等待返回
- performance - 我想在空手道加特林中将一个 api 的结果用于另一个 api。两个 api 都在 gatling 中进行模拟
- python - 如何使用 Python 仅查找包含该月最后一天的日期的文件?
- reactjs - ReactJS - 创建 API 端点
- sql-server - DMS SQL Server 复制失败停止原因 FATAL_ERROR 错误级别 FATAL
- flutter-web - 未捕获 [firebase_messaging/failed-service-worker-registration] 消息:我们无法注册默认服务工作者
- yaml - 如何在 yaml 中将长字符串拆分为多行而不引入空格字符