Image for post
Image for post

Adapting to ListAdapter

Meghan Mehta
Dec 9, 2020 · 3 min read

This is the second in a series of articles which cover the fundamentals of creating and using RecyclerView. If you already have a solid understanding of how to create a RecyclerView, then carry on. Otherwise, consider starting with this post.

RecyclerView is a great way to display a list of data items efficiently. For displaying a list of static data, the default adapter works great. However, in most use cases, RecyclerView data is dynamic. Take a todo list app for example: new items are added, and completed items are removed. notifyItemInserted() can insert new tasks at a specified index, but the issue comes when removing items. notifyItemRemoved() is only useful if you have the position of the task you want to remove. It is possible to write code to get the position of the task to be removed and then call notifyItemRemoved(), but this code can get messy. Calling notifyDataSetChanged() is an option, but it redraws the entire view, even the unchanged parts, which is an expensive operation. ListAdapter handles addition and removal without the need to redraw the entire view, and even animate those changes.

Another benefit of using ListAdapter is that it comes with animations for when an item is added or removed. This provides a nice visual cue for the user to see the changes in the list. Animations are possible without ListAdapter but they must be implemented by the developer and won’t have the same performance because the view will likely need to be redrawn along with being animated.

Image for post
Image for post
Animation after adding an item.

Split the difference

In order to identify new data, DiffUtil requires you to override areItemsTheSame() and areContentsTheSame(). areItemsTheSame() checks if two items are actually the same item. areContentsTheSame() checks if two items have the same data.

Image for post
Image for post
Diagram showing how areItemsTheSame() compares items.
Image for post
Image for post
Diagram showing how areContentsTheSame() compares items.

In the Adapter class add in a DiffUtil object that overrides areItemsTheSame() and areContentsTheSame().

Update the Adapter class to inherit from the ListAdapter class instead of RecyclerView.Adapter. Pass in the DiffCallback.

Updating the list

In the Activity class, call submitList() on the Adapter and pass in the data list.

In the Adapter class, onBindViewHolder() can now use getItem() to retrieve items from the data list given a position.

That’s it! It only takes a few steps to convert your RecyclerView to use ListAdapter. Now your application can automatically get better performance and a better user experience by using ListAdapter to update only those items that have changed.

Next Steps

Thank you for reading the second installment in my RecyclerView series! Stay tuned as I write about more RecyclerView features.

If you want to learn more about ListAdapter check out the documentation.

Android Developers

The official Android Developers publication on Medium

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