android-RecyclerView的DiffUtil差异化工具使用

2024-01-02 02:59

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

没有效果图的示例简直就是扯淡

在这里插入图片描述

有点模糊,大家凑活看吧。。。

DiffUtil是什么?

DiffUtil是一个工具类,当你的RecyclerView需要更新数据时,将新旧数据集传给它,它就能快速告知adapter有哪些数据需要更新。就相当于如果改变了就对某个item刷新,没改变就没刷新,可以简称为局部刷新。

DiffUtil 的优势

我在最初接触 DiffUtil 时, 心中便对它有颇多的好感, 包括:

算法听提来就很nb, 一定是个好东西;
简化了 RecyclerView 的刷新逻辑, 无须关心该调用 notifyItemInserted 还是 notifyItemChanged, 一律submitList 就完事了(虽然 notifyDataSetChanged 也能做到, 但是性能拉胯, 而且没有动画);
LiveData 或者 Flow 监听单一 List 数据源时, 往往很难知道, 整个 List 中到底哪些数据项被更新了, 只能调用notifyDataSetChanged 方法, 而 DiffUtil 恰好就能解决这个问题, 无脑 submitList 就完事了.

总的来说,尽量使用diffutil,可以更好的去刷新数据,速度更快,性能更好,效果更棒。

好了不说了,上代码吧

核心类:DiffCallBack.class

import android.os.Bundle;import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;import java.util.List;/*** cc--核心类 用来判断 新旧Item是否相等* cc 96.10.11*/
public class DiffCallBack extends DiffUtil.Callback {//旧数据集合private List<TestBean> mOldData;//新数据集合private List<TestBean> mNewData;/*** 拿到两个集合* @param mOldData* @param mNewData*/public DiffCallBack(List<TestBean> mOldData, List<TestBean> mNewData) {this.mOldData = mOldData;this.mNewData = mNewData;}/*** 旧数据集合* @return*/@Overridepublic int getOldListSize() {return mOldData != null ? mOldData.size() : 0;}/*** 新数据集合* @return*/@Overridepublic int getNewListSize() {return mNewData != null ? mNewData.size() : 0;}/*** Called by the DiffUtil to decide whether two object represent the same Item.* 被DiffUtil调用,用来判断 两个对象是否是相同的Item。* For example, if your items have unique ids, this method should check their id equality.* 例如,如果你的Item有唯一的id字段,这个方法就 判断id是否相等。* 本例判断name字段是否一致** @param oldItemPosition The position of the item in the old list* @param newItemPosition The position of the item in the new list* @return True if the two items represent the same object or false if they are different.*/@Overridepublic boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {return mOldData.get(oldItemPosition).getName().equals(mNewData.get(newItemPosition).getName());}/*** Called by the DiffUtil when it wants to check whether two items have the same data.* 被DiffUtil调用,用来检查 两个item是否含有相同的数据* DiffUtil uses this information to detect if the contents of an item has changed.* DiffUtil用返回的信息(true false)来检测当前item的内容是否发生了变化* DiffUtil uses this method to check equality instead of {@link Object#equals(Object)}* DiffUtil 用这个方法替代equals方法去检查是否相等。* so that you can change its behavior depending on your UI.* 所以你可以根据你的UI去改变它的返回值* For example, if you are using DiffUtil with a* {@link androidx.recyclerview.widget.RecyclerView.Adapter RecyclerView.Adapter}, you should* return whether the items' visual representations are the same.* 例如,如果你用RecyclerView.Adapter 配合DiffUtil使用,你需要返回Item的视觉表现是否相同。* This method is called only if {@link #areItemsTheSame(int, int)} returns* {@code true} for these items.* 这个方法仅仅在areItemsTheSame()返回true时,才调用。** @param oldItemPosition The position of the item in the old list* @param newItemPosition The position of the item in the new list which replaces the*                        oldItem* @return True if the contents of the items are the same or false if they are different.*/@Overridepublic boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {TestBean beanOld = mOldData.get(oldItemPosition);TestBean beanNew = mNewData.get(newItemPosition);if (!beanOld.getContent().equals(beanNew.getContent())) {return false;//如果有内容不同,就返回false}if (beanOld.getPic() != beanNew.getPic()) {return false;//如果有内容不同,就返回false}if (beanOld.getLookNumber() != beanNew.getLookNumber()) {return false;//如果有内容不同,就返回false}if (beanOld.getCommentNumber() != beanNew.getCommentNumber()) {return false;//如果有内容不同,就返回false}return true; //默认两个data内容是相同的}/*** When {@link #areItemsTheSame(int, int)} returns {@code true} for two items and* {@link #areContentsTheSame(int, int)} returns false for them, DiffUtil* calls this method to get a payload about the change.* <p>* 当{@link #areItemsTheSame(int, int)} 返回true,且{@link #areContentsTheSame(int, int)} 返回false时,DiffUtils会回调此方法,* 去得到这个Item(有哪些)改变的payload。* <p>* For example, if you are using DiffUtil with {@link androidx.recyclerview.widget.RecyclerView}, you can return the* particular field that changed in the item and your* {@link androidx.recyclerview.widget.RecyclerView.ItemAnimator ItemAnimator} can use that* information to run the correct animation.* <p>* 例如,如果你用RecyclerView配合DiffUtils,你可以返回  这个Item改变的那些字段,* {@link androidx.recyclerview.widget.RecyclerView.ItemAnimator ItemAnimator} 可以用那些信息去执行正确的动画* <p>* Default implementation returns {@code null}.\* 默认的实现是返回null** @param oldItemPosition The position of the item in the old list* @param newItemPosition The position of the item in the new list* @return A payload object that represents the change between the two items.* 返回 一个 代表着新老item的改变内容的 payload对象,*/@Nullable@Overridepublic Object getChangePayload(int oldItemPosition, int newItemPosition) {//实现这个方法 就能成为文艺青年中的文艺青年// 定向刷新中的部分更新// 效率最高//只是没有了ItemChange的白光一闪动画,(反正我也觉得不太重要)TestBean oldBean = mOldData.get(oldItemPosition);TestBean newBean = mNewData.get(newItemPosition);//这里就不用比较核心字段了,一定相等Bundle payload = new Bundle();if (!oldBean.getContent().equals(newBean.getContent())) {payload.putString("KEY_DESC", newBean.getContent());}if (oldBean.getPic() != newBean.getPic()) {payload.putInt("KEY_PIC", newBean.getPic());}if (oldBean.getLookNumber() != newBean.getLookNumber()) {payload.putInt("KEY_LOOK_NUMBER", newBean.getLookNumber());}if (oldBean.getCommentNumber() != newBean.getCommentNumber()) {payload.putInt("KEY_COMMENT_NUMBER", newBean.getCommentNumber());}if (payload.size() == 0)//如果没有变化 就传空return null;return payload;//}
}

DiffAdapter.class

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;import androidx.recyclerview.widget.RecyclerView;import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.mcxtzhang.diffutils.R;import java.util.List;/*** 介绍:普普通的adapter,* 但是 唯一亮点~* public void onBindViewHolder(DiffVH holder, int position, List<Object> payloads)* 重写这个方法* cc--* cc 96.10.11*/
public class DiffAdapter extends RecyclerView.Adapter<DiffAdapter.DiffVH> {private final static String TAG = "zxt";private List<TestBean> mDatas;private Context mContext;private LayoutInflater mInflater;public DiffAdapter(Context mContext, List<TestBean> mDatas) {this.mContext = mContext;this.mDatas = mDatas;mInflater = LayoutInflater.from(mContext);}public void setDatas(List<TestBean> mDatas) {this.mDatas = mDatas;}@Overridepublic DiffVH onCreateViewHolder(ViewGroup parent, int viewType) {return new DiffVH(mInflater.inflate(R.layout.item_diff, parent, false));}@Overridepublic void onBindViewHolder(final DiffVH holder, final int position) {TestBean bean = mDatas.get(position);holder.mTvName.setText(bean.getName());holder.mTvContent.setText(bean.getContent());displayCircleImage(mContext, bean.getPic(), holder.mImgUrl, 0);holder.mTvLookNumber.setText(new StringBuffer("浏览量:").append(bean.getLookNumber()));holder.mTvCommentNumber.setText(new StringBuffer("评论量:").append(bean.getCommentNumber()));}@Overridepublic void onBindViewHolder(DiffVH holder, int position, List<Object> payloads) {if (payloads.isEmpty()) {onBindViewHolder(holder, position);} else {//文艺青年中的文青Bundle payload = (Bundle) payloads.get(0);//取出我们在getChangePayload()方法返回的bundleTestBean bean = mDatas.get(position);//取出新数据源,(可以不用)for (String key : payload.keySet()) {switch (key) {case "KEY_DESC"://这里可以用payload里的数据,不过data也是新的 也可以用holder.mTvContent.setText(bean.getContent());break;case "KEY_PIC":displayCircleImage(mContext, payload.getInt(key), holder.mImgUrl, 0);break;case "KEY_LOOK_NUMBER":holder.mTvLookNumber.setText(new StringBuffer("浏览量:").append(payload.getInt(key)));break;case "KEY_COMMENT_NUMBER":holder.mTvCommentNumber.setText(new StringBuffer("评论量:").append(payload.getInt(key)));break;default:break;}}}}@Overridepublic int getItemCount() {return mDatas != null ? mDatas.size() : 0;}class DiffVH extends RecyclerView.ViewHolder {ImageView mImgUrl;TextView mTvName, mTvContent, mTvLookNumber, mTvCommentNumber;public DiffVH(View itemView) {super(itemView);mImgUrl = itemView.findViewById(R.id.img_url);mTvName = itemView.findViewById(R.id.txt_name);mTvContent = itemView.findViewById(R.id.txt_content);mTvLookNumber = itemView.findViewById(R.id.tv_look_number);mTvCommentNumber = itemView.findViewById(R.id.tv_comment_number);}}public static void displayCircleImage(Context context, int url, ImageView imageView, int placeHolderId) {RequestOptions options = (new RequestOptions()).circleCrop().placeholder(placeHolderId);Glide.with(context).load(url).transition(DrawableTransitionOptions.withCrossFade()).apply(options).into(imageView);}}

TestBean.class

/*** 介绍:一个普通的JavaBean,但是实现了clone方法,仅仅用于写Demo时,模拟刷新从网络获取数据用,* 因为使用DiffUtils比较新老数据集差异时,会遍历新老数据集的每个data,要确保他们的内存地址(指针)不一样,否则比较的是新老data是同一个,就一定相同,* 实际项目不需要,因为刷新时,数据一般从网络拉取,并且用Gson等解析出来,内存地址一定是不一样的。* cc--* cc 96.10.11*/
public class TestBean implements Cloneable {private String name;private String content;private int pic;private int lookNumber;private int commentNumber;public TestBean(String name, String content, int pic, int lookNumber, int commentNumber) {this.name = name;this.content = content;this.pic = pic;this.lookNumber = lookNumber;this.commentNumber = commentNumber;}public int getLookNumber() {return lookNumber;}public void setLookNumber(int lookNumber) {this.lookNumber = lookNumber;}public int getCommentNumber() {return commentNumber;}public void setCommentNumber(int commentNumber) {this.commentNumber = commentNumber;}public int getPic() {return pic;}public void setPic(int pic) {this.pic = pic;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}//仅写DEMO 用 实现克隆方法@Overridepublic TestBean clone() throws CloneNotSupportedException {TestBean bean = null;try {bean = (TestBean) super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return bean;}
}

MainActivity.class

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;import com.mcxtzhang.diffutils.R;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private List<TestBean> mDatas;private RecyclerView mRv;private DiffAdapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initData();mRv = (RecyclerView) findViewById(R.id.rv);mRv.setLayoutManager(new LinearLayoutManager(this));mAdapter = new DiffAdapter(this, mDatas);mRv.setAdapter(mAdapter);}private void initData() {mDatas = new ArrayList<>();mDatas.add(new TestBean("吴大哥", "我的内容是一样的。。。", R.drawable.pic1, 34456, 865));mDatas.add(new TestBean("吴大哥", "我的内容是一样的。。。", R.drawable.pic2, 2345, 98765));mDatas.add(new TestBean("吴大哥", "我的内容是一样的。。。", R.drawable.pic3, 87, 45));mDatas.add(new TestBean("吴大哥", "我的内容是一样的。。。", R.drawable.pic5, 2368435, 7587));}/*** 模拟刷新操作** @param view*/public void onRefresh(View view) {try {mNewDatas = new ArrayList<>();for (TestBean bean : mDatas) {mNewDatas.add(bean.clone());//clone一遍旧数据 ,模拟刷新操作}mNewDatas.get(0).setLookNumber((int) (Math.random() * 200));mNewDatas.get(0).setCommentNumber((int) (Math.random() * 200));mNewDatas.get(1).setLookNumber((int) (Math.random() * 200));mNewDatas.get(1).setCommentNumber((int) (Math.random() * 200));mNewDatas.get(2).setLookNumber((int) (Math.random() * 200));mNewDatas.get(2).setCommentNumber((int) (Math.random() * 200));mNewDatas.get(3).setLookNumber((int) (Math.random() * 200));mNewDatas.get(3).setCommentNumber((int) (Math.random() * 200));//新宠//利用DiffUtil.calculateDiff()方法,传入一个规则DiffUtil.Callback对象,和是否检测移动item的 boolean变量,得到DiffUtil.DiffResult 的对象new Thread(new Runnable() {@Overridepublic void run() {//放在子线程中计算DiffResultDiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack(mDatas, mNewDatas), true);Message message = mHandler.obtainMessage(1);message.obj = diffResult;//obj存放DiffResultmessage.sendToTarget();}}).start();//mAdapter.notifyDataSetChanged();//以前普通青年的我们只能这样,现在我们是文艺青年了,有新宠了} catch (CloneNotSupportedException e) {e.printStackTrace();}}private List<TestBean> mNewDatas;//增加一个变量暂存newList@SuppressLint("HandlerLeak")private Handler mHandler = new Handler() {@SuppressLint("HandlerLeak")@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 1://取出ResultDiffUtil.DiffResult diffResult = (DiffUtil.DiffResult) msg.obj;//利用DiffUtil.DiffResult对象的dispatchUpdatesTo()方法,传入RecyclerView的Adapter,轻松成为文艺青年diffResult.dispatchUpdatesTo(mAdapter);//别忘了将新数据给AdaptermDatas = mNewDatas;mAdapter.setDatas(mDatas);break;}}};}

item_diff.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"xmlns:app="http://schemas.android.com/apk/res-auto"android:padding="20dp"android:background="@color/colorAccent"android:orientation="vertical"><ImageViewandroid:id="@+id/img_url"tools:src="@mipmap/ic_launcher"android:layout_width="50dp"android:layout_height="50dp"android:scaleType="centerCrop"tools:ignore="MissingConstraints" /><TextViewandroid:id="@+id/txt_name"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintStart_toEndOf="@+id/img_url"android:layout_marginStart="10dp"android:textColor="#ffffff"tools:text="第一个"tools:ignore="MissingConstraints" /><TextViewandroid:id="@+id/txt_content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textColor="#ffffff"app:layout_constraintTop_toBottomOf="@+id/img_url"tools:text="我的存在只为了证明定向刷新中的定向刷新"tools:ignore="MissingConstraints" /><TextViewandroid:id="@+id/tv_look_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"tools:text="阅读量:100"android:padding="2dp"android:textColor="#ffffff"app:layout_constraintTop_toBottomOf="@+id/txt_content"tools:ignore="MissingConstraints" /><TextViewandroid:id="@+id/tv_comment_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"tools:text="评论量:50"android:padding="2dp"android:textColor="#ffffff"android:layout_marginStart="30dp"app:layout_constraintTop_toBottomOf="@+id/txt_content"app:layout_constraintStart_toEndOf="@+id/tv_look_number"tools:ignore="MissingConstraints" /></androidx.constraintlayout.widget.ConstraintLayout>
注:至此,所有的代码开发工作都已经结束了。

附上demo源码。

源码:源码请点这里

如果下不了源码,可以加微信,手机号在下面。


Q:486789970(QQ现在很少用)
V:18588400509(如果着急,可以直接加微信)
email:mr.cai_cai@foxmail.com
扫码加入微信群:在这里插入图片描述

如果有什么问题,欢迎大家指导。并相互联系,希望能够通过文章互相学习。

											                               	---财财亲笔

这篇关于android-RecyclerView的DiffUtil差异化工具使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

java变量内存中存储的使用方式

《java变量内存中存储的使用方式》:本文主要介绍java变量内存中存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、变量的定义3、 变量的类型4、 变量的作用域5、 内存中的存储方式总结1、介绍在 Java 中,变量是用于存储程序中数据

关于Mybatis和JDBC的使用及区别

《关于Mybatis和JDBC的使用及区别》:本文主要介绍关于Mybatis和JDBC的使用及区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、JDBC1.1、流程1.2、优缺点2、MyBATis2.1、执行流程2.2、使用2.3、实现方式1、XML配置文件

macOS Sequoia 15.5 发布: 改进邮件和屏幕使用时间功能

《macOSSequoia15.5发布:改进邮件和屏幕使用时间功能》经过常规Beta测试后,新的macOSSequoia15.5现已公开发布,但重要的新功能将被保留到WWDC和... MACOS Sequoia 15.5 正式发布!本次更新为 Mac 用户带来了一系列功能强化、错误修复和安全性提升,进一步增

Java资源管理和引用体系的使用详解

《Java资源管理和引用体系的使用详解》:本文主要介绍Java资源管理和引用体系的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Java的引用体系1、强引用 (Strong Reference)2、软引用 (Soft Reference)3、弱引用 (W

ubuntu系统使用官方操作命令升级Dify指南

《ubuntu系统使用官方操作命令升级Dify指南》Dify支持自动化执行、日志记录和结果管理,适用于数据处理、模型训练和部署等场景,今天我们就来看看ubuntu系统中使用官方操作命令升级Dify的方... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

IDEA之MyBatisX使用的图文步骤

《IDEA之MyBatisX使用的图文步骤》本文主要介绍了IDEA之MyBatisX使用,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 目录一、idea插件安装二、IDEA配置数据库连接(以mysql为例)三、生产基础代码一、idea插