This article is the second in a series of introduction to RecyclerView. If you already have some knowledge of creating RecyclerView, read on. If you are not already familiar with it, I suggest you read the first article in this series.

RecyclerView is a very efficient way to display list data. For static list data, the default Adapter is sufficient. However, in most cases, RecyclerView data is dynamic. Take the memo work application example: the main operation is to add new work items, delete already completed work items. NotifyItemRemoved () adds a new task to a specified position, but the problem arises when an element needs to be removed; notifyItemRemoved() is only effective if you already know the position of the task to be removed. While you could write code to determine the location of the task to be removed and then call notifyItemRemoved(), the code would become cumbersome. Calling notifyDataSetChanged() is another option, but it redraws the entire view, including the parts of the data that have not changed, making this more expensive. The ListAdapter can handle the addition and removal of elements without redrawing the view, and can even animate changes.

Another benefit of using ListAdapter is that you can also add animations when you add or remove elements. This allows users to visually see changes in the list data. It is possible to animate without ListAdapter, but this would have to be done by the developer himself, and since the view with the animation would need to be redrawn, it would not achieve the same performance.

Animates the element

Comparison of treatment differences

DiffUtil is the secret of ListAdapter’s ability to efficiently change elements. DiffUtil compares which elements were added, moved, and removed from the old and new lists, and then outputs the list of update operations to efficiently convert the elements from the original list to the new ones.

To be able to recognize new data, DiffUtil requires you to override areItemsTheSame() and areContentsTheSame(). AreItemsTheSame () checks whether two elements are the same element. AreContentsTheSame () checks whether two elements contain the same data.

AreItemsTheSame () compares elements schematics

AreContentsTheSame () is a schematic of comparing elements

Add the DiffUtil object to the Adapter class and overwrite areItemsTheSame() and areContentsTheSame().

<! -- Copyright2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

object FlowerDiffCallback : DiffUtil.ItemCallback<Flower>() {
   override fun areItemsTheSame(oldItem: Flower, newItem: Flower): Boolean {
      return oldItem.id == newItem.id
   }

   override fun areContentsTheSame(oldItem: Flower, newItem: Flower): Boolean {
      return oldItem == newItem
   }
}
Copy the code

Change the parent of Adapter from recyclerView. Adapter to ListAdapter, and pass DiffCallback.

<! -- Copyright2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->
   
class FlowerAdapter : ListAdapter<String, FlowerAdapter.FlowerViewHolder>(FlowerDiffCallback)
Copy the code

Update the list

The ListAdapter retrieves the data through the submitList() method, which submits a list to compare and display against the current list. That means you don’t need to rewrite getItemCount() because the ListAdapter manages the list.

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

<! -- Copyright2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

val flowerList = resources.getStringArray(R.array.flower_array).toMutableList()
val flowerAdapter = FlowerAdapter()
flowerAdapter.submitList(flowerList)
Copy the code

In the Adapter class, onBindViewHolder() can now use getItem() to retrieve elements from the data list at a specified location.

<! -- Copyright2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

override fun onBindViewHolder(holder: FlowerViewHolder, position: Int) {
  holder.bind(getItem(position))
}
Copy the code

It’s that simple. Use ListAdapter in your RecyclerView in a few simple steps. Now your application can use ListAdapter to update the changed elements for better performance and user experience.

The next step

The complete sample code for ListAdapter is here.

Thanks for reading the second article in our RecyclerView series. Stay tuned for more RecyclerView in the future.

If you want to learn more about ListAdapter, please refer to the official documentation.