DiffUtils实现partial bindData

                                                                               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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值