在执行recyclerView拖放操作后,将新职位保存在会议室中降低 [英] Save new positions in Room after recyclerView drag & drop

查看:46
本文介绍了在执行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.

我尝试过:

我当前的代码如下:

在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.

这篇关于在执行recyclerView拖放操作后,将新职位保存在会议室中降低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆