DiffUtils实现partial bindData
来源:互联网 发布:google 云计算 入华 编辑:程序博客网 时间:2024/04/20 20:32
DiffUtils实现partial bindData
support-v7:24.2.0中出来的一个兼容工具,它用来比较两个数据集,寻找出旧数据集-》新数据集的最小变化量。
这个工具对于列表中需要局部内容发生变化是时,只需要计算出发生变化的部分,只刷新变化部分的内容,即可极大的提高刷新页面数据的效率问题。不必通过notifyDataSetChanged方法进行整个界面的刷新
DiffUtil的作用,就是找出集合中每一个Item发生的变化,然后对每个变化给予对应的刷新。
1.先看DiffUtil.Callback这个抽象内的源码入下:
/** * A Callback class used by DiffUtil while calculating the diff between two lists. */public abstract static class Callback { /** * Returns the size of the old list. * * @return The size of the old list. */ public abstract int getOldListSize(); /** * Returns the size of the new list. * * @return The size of the new list. */ public abstract int getNewListSize(); /** * Called by the DiffUtil to decide whether two object represent the same Item. * <p> * For example, if your items have unique ids, this method should check their id equality. * * @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. */ public abstract boolean areItemsTheSame(int oldItemPosition, int newItemPosition); /** * Called by the DiffUtil when it wants to check whether two items have the same data. * DiffUtil uses this information to detect if the contents of an item has changed. * <p> * DiffUtil uses this method to check equality instead of {@link Object#equals(Object)} * so that you can change its behavior depending on your UI. * For example, if you are using DiffUtil with a * {@link android.support.v7.widget.RecyclerView.Adapter RecyclerView.Adapter}, you should * return whether the items' visual representations are the same. * <p> * This method is called only if {@link #areItemsTheSame(int, int)} returns * {@code true} for these items. * * @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. */ public abstract boolean areContentsTheSame(int oldItemPosition, int newItemPosition); /** * 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> * For example, if you are using DiffUtil with {@link RecyclerView}, you can return the * particular field that changed in the item and your * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} can use that * information to run the correct animation. * <p> * Default implementation returns {@code 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. */ @Nullable public Object getChangePayload(int oldItemPosition, int newItemPosition) { return null; }}
2.使用方法:
1.新建一个类继承DiffUtil.Callback实现抽象方法(前4个必须,最后一个可选)
2.
//比较新老数据的差异
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack(mDatas, newDatas), true);//然后利用DiffUtil.DiffResult对象的dispatchUpdatesTo()
方法,传入RecyclerView的Adapter,替代mAdapter.notifyDataSetChanged()
方法。diffResult.dispatchUpdatesTo(mAdapter);//将新数据添加适配器中
mMyAdapter.setList(newList);举例说明:
需求:如下图:
点击===》关注,实现点击条目的局部数据更新
核心代码如下:
DiffUtil.Callback的子类:
Adapter的代码
list_item.xml
MainActivity代码如下:
轻松实现列表数据的partial binddata~~~
阅读全文
1 0
- DiffUtils实现partial bindData
- partial
- partial
- partial
- DiffUtils for Windows
- PHP 5.2 中实现partial参数绑定
- Diffutils打造不一样的recyclerview
- Android开发Diffutils打造不一样的recyclerview
- Android开发Diffutils打造不一样的recyclerview
- Android开发Diffutils打造不一样的recyclerview
- QlikView实现部分加载数据的功能(Partial Load)
- QlikView 笔记(五) 通过Partial Reload实现增量抽取
- Partial Class
- C# partial
- C# partial
- C# partial
- partial使用
- partial关键字
- linux中获取/更新Let’s encrypt 证书的脚本
- Adobe cc 2017 全系列软件下载 附破解安装教程
- 解决LinearLayout中控件不能居右对齐
- Cloudera Hive传统权限管理设置
- Linux内存管理之kmalloc 与 __get_free_page()
- DiffUtils实现partial bindData
- 20170609大数据工作
- 加载gif
- qt configure 时提示【file to open is a directory】
- 第二章:初探Spring Cloud Eureka+Hystrix+Ribbon+Feign微服务搭建
- ActionScript3.0自定义鼠标指针
- svg.js使用中的一个bug
- 整理linux图形化磁盘管理工具使用
- c语言编程一些小技巧