-1

I am populating new data in my RecyclerView adapter all at once, so there are no insert or remove one item actions.

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)
  • when there is a new item in the new list (should notifyItemInserted with that position in the new list)
  • 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 not works 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?

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.

K.Os
  • 3,769
  • 5
  • 32
  • 73

2 Answers2

1

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

You update your recyclerView like that:

DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
diffResult.dispatchUpdatesTo(yourAdapter);
elmorabea
  • 3,133
  • 1
  • 12
  • 20
  • Is there a way to prevent unecessary "blink" from notifyDataSetChanged and show only itemMove, Added, Removed animations? – K.Os Nov 29 '17 at 12:44
  • You shouldn't be using notifyDataSetChanged, look at answer for updated usage. – elmorabea Nov 29 '17 at 12:46
  • Is there a way to disable auto scroll to bottom? As described here: https://stackoverflow.com/questions/47632693/recyclerview-using-diffutil-prevent-to-scroll-bottom-on-change – K.Os Dec 04 '17 at 12:52
0

Simply use DiffUtil like

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.

public class MyDiffCallback extends DiffUtil.Callback

// override methods

  • Is there a way to prevent unecessary "blink" from notifyDataSetChanged and show only itemMove, Added, Removed animations? – K.Os Nov 29 '17 at 12:44
  • then don't call notifydatasetchanged , call notifyitemrangechanged(0,listsize) , this will help – Rahul Baboria Nov 30 '17 at 10:54
  • Is there a way to disable auto scroll to bottom? As described here: https://stackoverflow.com/questions/47632693/recyclerview-using-diffutil-prevent-to-scroll-bottom-on-change – K.Os Dec 04 '17 at 12:52