头图

This article is the second in the series of articles that introduces the introduction of RecyclerView. If you already have a certain understanding of creating RecyclerView, please continue reading this article. If you are not yet familiar with it, I suggest you first read first article this series.

RecyclerView can display list data very efficiently. For static list data, the default adapter is sufficient. However, in most cases, the data of RecyclerView changes dynamically. Take memo job application example: The main operation is to add new work items and delete completed work items. notifyItemInserted() can add a new task to a specified location, but the problem comes when you need to delete an element. notifyItemRemoved() only works when you know the location of the task to be deleted. Although you can write code to determine the location of the task to be deleted, and then call notifyItemRemoved(), the code will become very complicated. Calling notifyDataSetChanged() is also a way, but it will redraw the entire view, including the part where the data has not changed, making the operation more expensive. The ListAdapter can handle the addition and deletion of elements without redrawing the view, and can even add animation effects to changes.

Another advantage of using ListAdapter is: When adding or deleting elements, you can also add animation. In this way, the user can intuitively see the changes in the list data. Although animation effects can be achieved without ListAdapter, developers need to implement them themselves, and the view with animation needs to be redrawn, so the same performance cannot be achieved.

添加元素的动画效果

Add the animation effect of the element

treatment difference comparison

DiffUtil is the secret of ListAdapter's ability to efficiently change elements. DiffUtil compares which elements are added, moved, and deleted in the old and new lists, and then outputs the updated list to efficiently convert the elements in the original list into new elements.

In order to be able to recognize the 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() 比较元素的示意图

areItemsTheSame() Diagram of comparing elements

areContentsTheSame() 比较元素的示意图

areContentsTheSame() Diagram of comparing elements

Add the DiffUtil object in the Adapter class, and copy areItemsTheSame() and areContentsTheSame() .

<!-- Copyright 2019 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
   }
}

Adapter the parent class of RecyclerView.Adapter from 060a4be828a73e to ListAdapter , and pass in DiffCallback .

<!-- Copyright 2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->
   
class FlowerAdapter : ListAdapter<String, FlowerAdapter.FlowerViewHolder>(FlowerDiffCallback)

update list

ListAdapter obtains data through submitList() ) method, which submits a list to compare with the current list and display it. In other words, you do not need to rewrite getItemCount() , because ListAdapter will manage the list.

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

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

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

In the Adapter class, onBindViewHolder() can now use getItem() ) to get the element at the specified position from the data list.

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

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

It's that simple. You can use ListAdapter RecyclerView in just a few simple operations. Now your application can use ListAdapter to update those changed elements to obtain better performance and user experience.

Next

About ListAdapter of complete sample code are here.

Thanks for reading the second article of RecyclerView series Please continue to pay attention to more about RecyclerView in the future.

If you want to know more about ListAdapter , please refer to official document .


Android开发者
404 声望2k 粉丝

Android 最新开发技术更新,包括 Kotlin、Android Studio、Jetpack 和 Android 最新系统技术特性分享。更多内容,请关注 官方 Android 开发者文档。