For the More Curious: A Smarter Adapter with ListAdapter

As the backing data changes, RecyclerView provides all the tools needed to perform animations to reflect those changes. As in the example above, you could call APIs like RecyclerView​.Adapter​.notifyItemMoved(…) or RecyclerView​.Adapter​.notifyItemInserted(…) to tell the RecyclerView to animate those changes in. However, you do not usually have visibility on specific changes to your data, so you cannot easily call those functions on the individual changes in the list.

Instead, it is much more common to be presented with a copy of the list of data, with the changes embedded in it. Unless you manually calculate all the changes between the old list of data and the new list of data, the best you can do is reassign the backing list of data of your RecyclerView.Adapter and force it to re-render every element in the list. That calculation of changes is difficult, so developers often rely on RecyclerView​.Adapter​.notifyDataSetChanged(…), which will redraw and rebind all the items in the RecyclerView’s layout.

A key aspect of RecyclerView’s design is that it tries to be efficient, avoiding unnecessary work. The benefit of APIs such as RecyclerView​.Adapter​.notifyItemMoved(…) and RecyclerView​.Adapter​.notifyItemInserted(…) is that they will only update or alter the relevant views to perform those animations. All the other views in the list will remain untouched. In comparison, RecyclerView​.Adapter​.notifyDataSetChanged(…) is a blunt instrument that often does a lot of unnecessary work.

When you are presented with an entirely new list of (very similar) data, it would be convenient to have a tool that calculates the differences between the new and old lists and then calls the appropriate RecyclerView.Adapter.notifyItem…() functions to animate those changes in. That is where ListAdapter comes in.

ListAdapter extends the regular RecyclerView.Adapter, bringing with it extra goodies to help you efficiently display and update lists of data. By using ListAdapter instead of RecyclerView.Adapter, you can have the library calculate the individual insert/move/remove/update operations to efficiently update the views in your RecyclerView.

This calculation does not happen magically. ListAdapter uses a class called DiffUtil, which is included in the RecyclerView library. DiffUtil can detect which items have changed between your original list and your updated list, but it needs a bit of help. The key component that makes this process work is an instance of a class you define that extends DiffUtil.ItemCallback.

The DiffUtil.ItemCallback class has two functions that you must implement (areContentsTheSame(…) and areItemsTheSame(…)), which ListAdapter uses internally to determine the differences between the lists and then call the appropriate APIs.

With that set up, whenever you have a new list of data to display in your RecyclerView, all you have to do is call ListAdapter.submitList(…), passing in the new list of data, and your RecyclerView will beautifully and efficiently animate the new data onscreen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85