In order to update the data in the adapter, I used this method in the adapter

public void setData(@NotNull List<HomeScreenAdapterItem> iData) { mHomeItems = iData; notifyDataSetChanged(); } 

then changed implementation to DiffUtill

 public void setData(@NotNull List<HomeScreenAdapterItem> iData) { MyDiffUtil<HomeScreenAdapterItem> diff = new MyDiffUtil<>(mHomeItems, iData); DiffUtil.DiffResult result = DiffUtil.calculateDiff(diff, false); mHomeItems = iData; result.dispatchUpdatesTo(this); } 

How it works for me. There is a screen with RecyclerView , when I click on a list item, the next screen opens and it is possible to change the list data, including the sequence of elements.

So, with the first approach, when you return to the first screen, the replacement of the list items is not noticeable, the updated data is immediately visible, and with the DiffUtill , the screen opens and I see how the new elements change to new ones.

Question

How to make the second method work at the speed of the first?

  • 3
    so why do you need then DiffUtill if the animation is not needed? - ermak0ff
  • maybe you can see how the elements change, because the animation works (this is how it should be). diffutil updates only what changed using the methods of notifyItemChanged () and others, which include action animations - pavlofff
  • @ ermak0ff diffUtill is needed not to animate, but to properly update items, since the notifyItemChanges method notifyItemChanges very expensive in terms of resources. But I don’t need animation - Aleksey Timoshchenko
  • @pavlofff shouldn’t be like that, because diff util has a special flag in the method calculateDiff(... , FALSE) which should turn off animation. - Aleksey Timoshchenko

1 answer 1

DiffUtill is needed for optimal updating of the View elements in the RecyclerView. Updating occurs only to those View in which the data has changed.

If you are confused by the animation, you can turn it off:

 ((SimpleItemAnimator) RecyclerView.getItemAnimator()).setSupportsChangeAnimations(false); 
  • I still see the animation. So did goo.gl/ix7jL5 - Aleksey Timoshchenko