首页 >> 大全

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

2024-01-02 大全 23 作者:考证青年

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

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

是什么?

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

的优势

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

算法听提来就很nb, 一定是个好东西;

简化了 的刷新逻辑, 无须关心该调用 还是 , 一律 就完事了(虽然 也能做到, 但是性能拉胯, 而且没有动画);

或者 Flow 监听单一 List 数据源时, 往往很难知道, 整个 List 中到底哪些数据项被更新了, 只能调用 方法, 而 恰好就能解决这个问题, 无脑 就完事了.

差异化分析__差异化模块

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

好了不说了,上代码吧

核心类:.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 mOldData;//新数据集合private List mNewData;/*** 拿到两个集合* @param mOldData* @param mNewData*/public DiffCallBack(List mOldData, List 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.* 

* 当{@link #areItemsTheSame(int, int)} 返回true,且{@link #areContentsTheSame(int, int)} 返回false时,DiffUtils会回调此方法,* 去得到这个Item(有哪些)改变的payload。*

* 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.*

* 例如,如果你用RecyclerView配合DiffUtils,你可以返回 这个Item改变的那些字段,* {@link androidx.recyclerview.widget.RecyclerView.ItemAnimator ItemAnimator} 可以用那些信息去执行正确的动画*

* 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;//} }

.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 payloads)* 重写这个方法* cc--* cc 96.10.11*/
public class DiffAdapter extends RecyclerView.Adapter {private final static String TAG = "zxt";private List mDatas;private Context mContext;private LayoutInflater mInflater;public DiffAdapter(Context mContext, List mDatas) {this.mContext = mContext;this.mDatas = mDatas;mInflater = LayoutInflater.from(mContext);}public void setDatas(List 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 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);}}

.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;}
}

.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 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 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;}}};}

.xml



注:至此,所有的代码开发工作都已经结束了。

附上demo源码。 源码:源码请点这里 如果下不了源码,可以加微信,手机号在下面。

Q:(QQ现在很少用)

V:(如果着急,可以直接加微信)

email:

扫码加入微信群:

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

											                               	---财财亲笔

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了