Room + ViewModel + LiveData + RecyclerView (MVVM) Part 10 - ADD ANIMATIONS WITH LISTADAPTER/DIFFUTIL

Coding in Flow · 66K views · 2018 · 11:36

Open on YouTube

Software (other)Testing, debugging & developer toolstutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering RecyclerView questions about refreshing lists, moving items or adding animations without calling notifyDataSetChanged.

Recommended by 2 people across 2 different months, 2018 to 2020.

What it is

Part 10 of Coding in Flow's Android Architecture Components series, a screencast tutorial refactoring a RecyclerView adapter into a ListAdapter that uses AsyncListDiffer and DiffUtil. It shows how to implement DiffUtil.ItemCallback with areItemsTheSame and areContentsTheSame and use submitList to get default insert, remove and update animations.

What people said

Recommends ueen · 2019-05 · Stack Overflow · on “LifecycleOwner onChanged loops forever”

The easiest and cleanest way would be to simply pass the item you want in the list to your Repository and add it to the Database, the recycled view is then hooked up to live data from fetchAllTasks(); and will display all task, to see how to do this refer to this video example [this video] Bonus: it will add nice Animations to your RecyclerView :)

Recommends Merthan Erdem · 2020-01 · Stack Overflow · on “Move Recyclerview Item position based on String”

You probably want to consider the ListAdapter class ListAdapter Sample and Documentation [LINK] You can then easily just sort your list using things like loops and then pass it to submitList(newList); The ListAdapter then handles everything for you, you even get great animations for the moved positions. So you don't have to tell it which stuff in the list has changed, it finds it out itself. [this video] In your case you can then just manually sort the list for example using a Comperator and pass it to submitList each time something changes.

Cites Matt · 2018-12 · Stack Overflow · on “Refresh RecyclerView When Delete Items”

After a lot of frustration with your same issue, I modified my RecyclerViewAdapter to use a ListAdapter . Here is how my RecyclerViewAdapter looks like: public class RecyclerViewAdapter extends ListAdapter<YourObjectType, RecyclerViewAdapter.ViewHolder> { ... public RecyclerViewAdapter() { super(DIFF_CALLBACK); ... } public static final DiffUtil.ItemCallback<YourObjectType> DIFF_CALLBACK = new DiffUtil.ItemCallback<YourObjectType>() { @Override public boolean areItemsTheSame(@NonNull YourObjectType oldItem, @NonNull YourObjectType newItem) { return oldItem.compareTo(newItem)==0; //I added a compareTo method to myObjectClass so that I can compare them. } @Override public boolean areContentsTh

Cites eegooDeveloper · 2019-07 · Stack Overflow · on “Why Recyclerview adapter onBindViewHolder is called more than once?”

I use this tutorial [this video] to implement DiffUtil [LINK] in my recyclerview. My aim is to add single item to the bottom of the recyclerview without reloading the rest of the recyclerview. I used firestore addSnapshotListener to call the adapter. The problem is the onBindViewHolder is called multiple times (ie, no of items present in the list). I dont think that is suppose to happen when using DiffUtil, right? It should only call onBindViewHolder for the item that is added to recyclerview, right? This is the code whre I call the adapter: @Override protected void onStart() { super.onStart(); reference .addSnapshotListener((Activity)context, new EventListener<QuerySnapshot>() { @Override public