Making Recyclerview Efficient using List Adapter and DiffUtil

Manoj Mohanty
Tech@Carnot
Published in
4 min readAug 17, 2020

This article will help you understand what is List Adapter, how to implement recyclerview with list adapter and its benefits over default recyclerview adapter and when to use List Adapter.

User scrolling through list of task

If you are an Android app developer probability is you would have implemented recyclerview. A Recyclerview is used to display a list of data. It can be things like a list of issues, a to-do list, a movie list, etc. Most of the time we end up using notifyDataSetChanged() to inform data updates but google suggests

If you are writing an adapter it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort

So lets get started and see how we can implement Recyclerview with List Adapter.

No additional dependency is required we just need to add recyclerview

implementation "androidx.recyclerview:recyclerview:1.1.0"

Lets assume we have a data class with following details

Setting Up Data Class

data class Post(val id:Int, val title:String, val body:String)

Implementing Adapter Class

Here we extend ListAdapter<T, YourAdapter.ViewHolder> and implement constructor with a class extending DiffUtil.ItemCallback<T> as a parameter which does the data update calculation for us, which is a bit different when compared with the existing RecyclerView Adapter

Below is an example how to do it in Kotlin. Here we extend ListAdapter and pass TaskDiffCallBack which extends DiffUtilItemCallback<Post>

DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second one.

Let’s Understand Few Important Things

  • areItemsTheSame(T oldItem, T newItem): Called to check whether two objects represent the same item. If your items have unique ids, this method should check their id equality. Here we check as our is Post is unique we have a check on id, it could have been any unique constraint for your model.
  • areContentsTheSame(T oldItem, T newItem): Checks whether two items have the same data. This method is called only if areItemsTheSame(T, T) returns true for these items, i.e. if any of the id is available in both new and old list it would compare actual properties to verify if they have changed and with Kotlin all we need to do is oldItem == newItem

Accessing Data in onBindViewHolder

Initializing And Calling Our Adapter Class

Here we initialized our Adapter and set our recyclerview adapter to Adapter instance, and call our updatePostAdapter with the required parameter. At last, we just need to call the adapter.submitList(postList). So next time we have if we have new data we would just call the same function with updated and Our DiffUtil Implementation would take care of calculating the changes in new List and communicate it to our PostAdapter.

Let’s Understand Few Important Things

  • No need Of notifyDataSetChanged: submitList takes care of replacing the existing list of data with new and notify the same to the adapter.
  • Internally it calls DiffUtil.calculateDiff which does the handling for us
  • All this calculation happens off the main thread
  • For the first run, nothing happens its just displays the list directly without any check.
Confused When to use what?

Conclusion

  • If we have a static list that doesn’t change once loaded then it is better to use normal Adapter.
  • If Data source is dynamic and something which changes with real-time syncing I would use ListAdapter.
  • The actual runtime of the algorithm/calculation of diff in the new list and old list - significantly depends on the number of changes in the list and the cost of your comparison methods (Runtime Info from Android Docs using Diff Util)
  • If the data in list mostly changes with user interaction I would still use normal Adapter and use adapter methods like notifyItemChanged(int) notifyItemInserted(int) notifyItemRemoved(int), etc as this is the most efficient way to update your view.
  • Google suggests notifyDataSetChanged should be the last option to update data

Please let me know your thoughts about it in comments and if think this was helpful then do share and give a clap.

We are trying to fix some broken benches in the Indian agriculture ecosystem through technology, to improve farmers’ income. If you share the same passion join us in the pursuit, or simply drop us a line on report@carnot.co.in

Follow Tech@Carnot for more such blogs on topics like Data Science and Visualization, Cloud Engineering, Firmware Development, and many more.

--

--

Manoj Mohanty
Tech@Carnot

Android Engineer @PayPay | Ex Rapido, Medibuddy, Carnot Technologies, Deloitte