DiffUtil + RecyclerView 在 Kotlin中的使用

2023-12-23 01:45

本文主要是介绍DiffUtil + RecyclerView 在 Kotlin中的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

很惭愧, 做了多年的Android开发还没有使用过DiffUtil这样解放双手的工具。

文章目录

  • 1 DiffUtil 用来解决什么问题?
  • 2 DiffUtil 是什么?
  • 3 DiffUtil的使用
  • 4 参考文章

1 DiffUtil 用来解决什么问题?

List发生变化, 我们使用 RecyclerView.Adapter.notifyDataChanged很熟练了

  1. 如果List仅仅是一个item变化了,其他item没有变化怎么办? notifyItemChanged
  2. 如果List仅仅是一个item移除了,其他item没有移除怎么办? notifyItemRemoved
  3. 如果List部分item发生变化,其他的item都没有变化怎么办?
  4. 如果List部分item移除了,其他item没有移除怎么办?

有如下解决思路:

  1. 你可以无脑继续使用notifyDataChanged 但失去了性能
  2. 自己判断发生变化的item,自己调用notifyItemxxx
  3. DiffUtil帮你判断哪里发生了变化,并自动帮你调用 notifyItemxxx

2 DiffUtil 是什么?

DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second one.

It can be used to calculate updates for a RecyclerView Adapter. See ListAdapter and AsyncListDiffer which can simplify the use of DiffUtil on a background thread.

DiffUtil uses Eugene W. Myers’s difference algorithm to calculate the minimal number of updates to convert one list into another. Myers’s algorithm does not handle items that are moved so DiffUtil runs a second pass on the result to detect items that were moved.

DiffUtil 是一个实用程序类,它计算两个列表之间的差异并输出将第一个列表转换为第二个列表的更新操作列表。

它可用于计算 RecyclerView 适配器的更新。请参阅 ListAdapter 和 AsyncListDiffer,它们可以简化后台线程上 DiffUtil 的使用。

DiffUtil 使用 Eugene W. Myers 的差分算法来计算将一个列表转换为另一列表所需的最小更新次数。 Myers 的算法不处理已移动的项目,因此 DiffUtil 对结果运行第二遍以检测已移动的项目。

3 DiffUtil的使用

item_song_info.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="16dp"><!-- Title TextView --><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Title"android:textSize="20sp"android:textStyle="bold" /><!-- Spacer View to add space between title and subtitle --><Viewandroid:layout_width="8dp"android:layout_height="match_parent" /><!-- Subtitle TextView --><TextViewandroid:id="@+id/tv_sub_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Subtitle"android:textSize="16sp" />
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

MusicBean.kt

data class MusicBean(var type: Int, var title: String, val subTitle: String)

MainActivity.kt

class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val recyclerView: RecyclerView = findViewById(R.id.recyclerView)recyclerView.layoutManager = LinearLayoutManager(this)val adapter = MyAdapter()recyclerView.adapter = adapteradapter.data = getSampleDataA()Handler(Looper.getMainLooper()).postDelayed({adapter.data = getSampleDataB()}, 2000)}// 用于生成初始数据private fun getSampleDataA(): List<MusicBean> {val data = mutableListOf<MusicBean>()for (i in 1..10) {MusicBean(type = i, title = "ItemA $i", subTitle = "subTitle $i").let {data.add(it)}}return data}// 用于生成变化后的数据private fun getSampleDataB(): List<MusicBean> {val data = mutableListOf<MusicBean>()for (i in 1..10) {val tag = if (i <= 5) {"B"} else "A"MusicBean(type = i, title = "Item$tag $i", subTitle = "subTitle $i").let {data.add(it)}}return data}class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {var data: List<MusicBean> = emptyList()set(value) {// 如果比较的集合较多(比如超过1000个), 建议使用子线程去比较val diffResult = DiffUtil.calculateDiff(MyDiffCallback(field, value))// 旧值赋新值field = value// 这里一定要保证在主线程调用diffResult.dispatchUpdatesTo(this)}class MyDiffCallback(private val oldList: List<MusicBean>, private val newList: List<MusicBean>) : DiffUtil.Callback() {override fun getOldListSize(): Int = oldList.sizeoverride fun getNewListSize(): Int = newList.sizeoverride fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {// 把这里想成是比较holder的类型, 比如纯文本的holder和纯图片的holder的type肯定不同return oldList[oldItemPosition].type == newList[newItemPosition].type}override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {// 把这里想成是同一种holder的比较,比如都是纯文本holder,但是title不一致return oldList[oldItemPosition].title == newList[newItemPosition].title}}override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {val view =LayoutInflater.from(parent.context).inflate(R.layout.item_song_info, parent, false)return MyViewHolder(view)}override fun onBindViewHolder(holder: MyViewHolder, position: Int) {val item = data[position]holder.bind(item)}override fun getItemCount(): Int {return data.size}class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {fun bind(item: MusicBean) {val tvTitle: TextView = itemView.findViewById(R.id.tv_title)val tvSubTitle: TextView = itemView.findViewById(R.id.tv_sub_title)tvTitle.text = item.titletvSubTitle.text = item.subTitle}}}
}

在这里插入图片描述

4 参考文章

DiffUtil 官方介绍
将 DiffUtil 和数据绑定与 RecyclerView 结合使用
DiffUtil和它的差量算法
DiffUtils 遇到 Kotlin,榨干视图局部刷新的最后一滴性能

这篇关于DiffUtil + RecyclerView 在 Kotlin中的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/526295

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

kotlin中的模块化结构组件及工作原理

《kotlin中的模块化结构组件及工作原理》本文介绍了Kotlin中模块化结构组件,包括ViewModel、LiveData、Room和Navigation的工作原理和基础使用,本文通过实例代码给大家... 目录ViewModel 工作原理LiveData 工作原理Room 工作原理Navigation 工

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g