在执行recyclerView拖放操作后,将新职位保存在会议室中降低
我已经在我的应用程序中实现了具有拖放功能的recyclerView.一切正常,直到重新启动该应用程序为止-应用程序未保存/记住任何拖放更改.
I've implemented a recyclerView with a drag and drop feature in my app. Everything works fine until the app is relaunched --any drag and drop changes were not saved/remembered by the app.
我尝试过:
- 使用SharedPreference + GSON
- 在此处阅读有关此类的其他SQLite答案,例如:保罗·伯克的中篇文章
- Using SharedPreference + GSON
- Reading other SQLite answers here on SO like this one: Store new position of RecyclerView items in SQLite after being dragged and dropped
- Reading Paul Burke's Medium Post
我当前的代码如下:
在onCreate中
ItemViewModel itemViewModel = ViewModelProviders.of(this).get(ItemViewModel.class);
itemViewModel.getAllItems().observe(this, new Observer<List<Item>>() {
@Override
public void onChanged(List<Item> items) {
adapter.setItemList(items);
itemList = items; //Global variable itemList
}
});
拖动/移动项目时调用的方法
private void swap(int firstPosition, int secondPosition) {
Collections.swap(itemList, firstPosition, secondPosition);
for(int i = 0; i < itemList.size(); i++) {
Item currentItem = itemList.get(i);
ItemViewModel.update(currentItem );
}
adapter.notifyItemMoved(firstPosition, secondPosition);
}
有什么想法可以让我的应用在拖放后保存重新排序的recyclerView?预先感谢.
Any ideas how I can let my app save the reordered recyclerView after drag and drop? Thanks in advance.
如果您希望在关闭应用或转到其他活动后保留商品的顺序,则需要在实体文件中添加一个属性,将服务器作为其订单值.添加后,您可以访问dao并在对项目进行重新排序时在Collections.swap
方法中运行UPDATE
SQL,然后在列表的每次加载时,返回该属性所排序的项目.
If you wish to preserve the order of your items once you close your app or move to another activity, you will need to add a property to your entity files which will server as an order value for it. Once added, you could access your dao and run an UPDATE
SQL in the Collections.swap
method when reordering items and than on every load of the list, return the items ordered by that property.