简体   繁体   English

当我获得要分配给它的新列表时,如何在 RecyclerView 旧数据列表中正确通知、插入和删除

[英]How to properly notifyItemMoved, insert and removed in RecyclerView old data list when i get new fresh list to assign to it

I am populating new data in my RecyclerView adapter all at once, so there are no insert or remove one item actions.我一次在 RecyclerView 适配器中填充新数据,因此没有插入或删除一项操作。

So simply, i have an old list and when some Event occurs i get the new list and i can assign the new list to the old.很简单,我有一个旧列表,当发生某些事件时,我得到新列表,我可以将新列表分配给旧列表。

Problems are i cannot make properly the animation for each item in the old list问题是我无法为旧列表中的每个项目正确制作动画

  • when item has new position in the new list (should notifyItemMoved from old position to new)当项目在新列表中具有新位置时(应将 notifyItemMoved 从旧位置移至新位置)
  • when there is a new item in the new list (should notifyItemInserted with that position in the new list )当新列表中有新项目时(应该用新列表中的那个位置通知ItemInserted)
  • when the old item is not present in the new list (should notifyItemRemoved with that position)当新列表中不存在旧项目时(应使用该位置通知项目移除)

Here is something i have now, which i thought will work for first case - item move to new position:这是我现在拥有的东西,我认为它适用于第一种情况 - 项目移动到新位置:

     if(currentAdapterData!= null){
            for(int i = 0; i < currentAdapterData.size(); i++){
                for(int j = 0; j < newData.size(); j++){
                    if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
                        Log.v("same item", "currentAdapterData index :" + i  + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
                        if(i != j){
                            notifyItemMoved(i, j);
                        }
                    }
                }
            }
        }

        currentAdapterData = newData;

However it does not work as expected, and there is difference between logs(which are correct) and the list appearing on the phone(with wrong items positions, some duplicates, buggy etc.)但是它没有按预期工作,并且日志(正确的)和手机上出现的列表(项目位置错误,一些重复,马车等)之间存在差异。

So how can i make it work?那么我怎样才能让它工作呢? With notifyItemMoved, notifyItemInserted and notifyItemRemoved?使用notifyItemMoved、notifyItemInserted 和notifyItemRemoved?

I don't want to just use NofifyDataSetChanged, because it refresh the entire list instead of just updating the items with animations that have changed.我不想只使用 NofifyDataSetChanged,因为它会刷新整个列表,而不仅仅是使用已更改的动画更新项目。

It looks like that your new data is also a form of list, not a single item.看起来您的新数据也是一种列表形式,而不是单个项目。 I think this could be a good candidate for using DiffUtil in the support library.我认为这可能是在支持库中使用DiffUtil的不错选择。 Here is also a nice tutorial for it. 也是一个很好的教程。

It will allow you to calculate the difference in the new data and only update needed fields.它将允许您计算新数据中的差异并仅更新所需的字段。 It will also offload the work asynchronously.它还将异步卸载工作。

You just need to implement a DiffUtil.Callback to indicate if your items are the same or the contents are the same.您只需要实现一个DiffUtil.Callback来指示您的项目是否相同或内容是否相同。

You update your recyclerView like that:你像这样更新你的 recyclerView :

DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
diffResult.dispatchUpdatesTo(yourAdapter);

Simply use DiffUtil like简单地使用 DiffUtil 就像

final MyDiffCallback diffCallback = new MyDiffCallback(prevList, newList); final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

Create a Callback by extending MyDiffCallback and override methods and do as needed.通过扩展 MyDiffCallback 和覆盖方法创建回调并根据需要执行。

public class MyDiffCallback extends DiffUtil.Callback

// override methods // 覆盖方法

Well for this ,I feel this would be the easiest.Just follow it ->好吧,我觉得这将是最简单的。只要按照它 ->

Replace this替换这个

if(currentAdapterData!= null){
        for(int i = 0; i < currentAdapterData.size(); i++){
            for(int j = 0; j < newData.size(); j++){
                if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
                    Log.v("same item", "currentAdapterData index :" + i  + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
                    if(i != j){
                        notifyItemMoved(i, j);
                    }
                }
            }
        }
    }

    currentAdapterData = newData;

with

if(currentAdapterData!= null){
        for(int i = 0; i < currentAdapterData.size(); i++){
            for(int j = 0; j < newData.size(); j++){
                if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
                    Log.v("same item", "currentAdapterData index :" + i  + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
                    if(i != j){
                        notifyDataSetChanged();
                       new CountDownTimer(250, 250) {

                                @Override
                                public void onTick(long millisUntilFinished) {
                                    Log.d("millisUntilFinished", "" + millisUntilFinished);
                                }

                                @Override
                                public void onFinish() {
                                    notifyItemMoved(i, j);
                                }
                        }.start();                      
                    }
                }
            }
        }
    }

this will update the values and after 250 millisecond(1/4th a second),the value with be moved with animation.这将更新值,并在 250 毫秒(每秒 1/4 秒)后,随着动画移动该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView显示列表中已删除的数据 - RecyclerView shows removed data on List Android,我可以在使用 recyclerview 时获取要持久化的列表数据吗 - Android, Can I get list data to persist when using a recyclerview Recyclerview刷新列表在旧列表的顶部显示新列表 - Recyclerview refreshing list displaying new list on top of old list 每次数组列表都会覆盖旧值。 如何将新值插入数组? - Every time array list overrides the old values. How can I insert new values to array? 如何在传递单击的自定义列表项的数据时单击带有RecyclerView的片段项时打开新活动? - How to open a new activity when an item of a fragment with RecyclerView is clicked while passing the data of the clicked custom list item? 如何在列表中添加新项目时更新 RecyclerView 适配器数据 - How to update RecyclerView Adapter Data while adding new item in List Android recyclerView updata所有列表数据仍与旧数据合并 - Android recyclerView updata all list data still merge with old data RecyclerView 清除列表并添加新项目也显示旧项目 - RecyclerView clear list and add new item show old item too 当Firebase Realtime db中的数据被删除时,如何从列表中删除特定项目? - How can I remove specific item from List when data in firebase realtime db is removed? 提交新列表时,RecyclerView 不会自行更新 - RecyclerView is not updating itself when submiting new list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM