Android: ListAdapter, a Better Implementation for the RecyclerView

Anubhav Arora
May 8 · 3 min read
Photo by Nick Fewings on Unsplash

I am assuming you are aware of the RecyclerView and it’s implementation!

Almost every app that we use has some sort of a list, be it a vertical list or a horizontal list. RecyclerView was introduced as a successor to ListView and GridView, and it has been here ever since, rendering any adapter-based views in an efficient way. But there is always some room for improvement, isn’t there?
There comes ListAdapter in the picture. As per the google docs,
ListAdapter is RecyclerView.Adapter base class for presenting List data in a RecyclerView, including computing diffs between Lists on a background thread.

Ok, now what is computing diffs ?
Most of the times only a few parts of our list are getting updated (addition, updation or deletion) and not the whole list data, and when we call notifyDataSetChanged(), it refreshes the whole list. As a result onBindViewHolder() is called for every item of the list; and there comes DiffUtil to our rescue.

DiffUtils is a convenience wrapper around AsyncListDiffer that implements Adapter common default behaviour for item access and counting.

AsyncListDiffer is a helper class for computing the difference between two lists via DiffUtil on a background thread.

Enough theory, let us get to writing some code and understanding the implementation.

The ListAdapter implementation needs a Model class(in our case it is User), a RecylerView.ViewHolder implementation, and a DiffUtil.ItemCallBack<T> as a parameter to the constructor.

RecylerView.ViewHolder and DiffUtil.ItemCallBack<T> snippets

Above is the basic implementation of a Model class and a ViewHolder class.

Here, I have created a class UserDiffCallBack which implements DiffUtil.ItemCallBack<T>, where T is our model class, and in turn we need to override two methods, which compare the items.

Here I have used oldItem == newItem expression for checking if the contents are same or not as we are using data class which provides implementation for equals() method by default.

Now let’s move to the ListAdapter’s implementation.

ListAdapter snippet

Other than the implementations explained above, we need to override the old OnCreateViewHolder and onBindViewHolder methods, as we did with the RecyclerView implementation.

All that is left to do is initialise the adapter, and provide the list to the adapter.

Initialising the adapter

ListAdapter comes with a method submitList(List<T> list) to provide new or modified data to the adapter and handles all the diffs computation. As a result we don’t need a setter method to set the list anymore. Diffs will be calculated on background thread and adapter will be notified with the results on the main thread.

That’s the implementation! It removes all the boilerplate code, and makes the development process more efficient and seamless.

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store