目录

RecyclerView性能提升神器-DiffUtil(实用)


使用步骤:

定义数据bean

  • 代码如下 public class DiffBean { private Long id; private String content;

        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
     }
    

实现自己的回调函数

  • 代码如下

    public class DiffCallBack extends DiffUtil.Callback {
        private List<DiffBean> mNewDatas,mOldDatas;
        public List<DiffBean> getNewDatas() {
            return mNewDatas;
        }
        public void setNewDatas(List<DiffBean> newDatas) {
            mNewDatas = newDatas;
        }
        public List<DiffBean> getOldDatas() {
            return mOldDatas;
        }
        public void setOldDatas(List<DiffBean> oldDatas) {
            mOldDatas = oldDatas;
        }
        @Override
        public int getOldListSize() {
            return mOldDatas==null?0:mOldDatas.size();
        }
        @Override
        public int getNewListSize() {
            return mNewDatas==null?0:mNewDatas.size();
        }
        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return mOldDatas.get(oldItemPosition).getId().equals(mNewDatas.get(newItemPosition).getId());
        }
    
        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
    
            return mOldDatas.get(oldItemPosition).getContent().equals(mNewDatas.get(newItemPosition).getContent());
        }
        @Nullable
        @Override
        public Object getChangePayload(int oldItemPosition, int newItemPosition) {
            DiffBean oldBean = mOldDatas.get(oldItemPosition);
            DiffBean newBean = mNewDatas.get(newItemPosition);
            //这里就不用比较核心字段了,一定相等
            Bundle payload = new Bundle();
            if (!oldBean.getContent().equals(newBean.getContent())) {
                payload.putString("KEY_CONTENT", newBean.getContent());
            }
            if (payload.size() == 0){
                return null;
                }
            return payload;
    }
    
  • 重写适配器方法

    public Object getChangePayload(int oldItemPosition, int newItemPosition) {
            //高级用法,实现局部刷新
            // 定向刷新中的部分更新,重此没有白光一闪
            DiffBean oldBean = mOldDatas.get(oldItemPosition);
            DiffBean newBean = mNewDatas.get(newItemPosition);
            Bundle payload = new Bundle();
            if (!oldBean.getContent().equals(newBean.getContent())) {
                payload.putString("KEY_CONTENT", newBean.getContent());
            }
            if (payload.size() == 0)//如果没有变化 就传空
                return null;
            return payload;//
        }
    

  • 代码如下

       List oldList = mAdapter.getData();
    DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
     mAdapter.setData(newList);
    result.dispatchUpdatesTo(mAdapter);      
    
总结:
  • 至此完工,代码量不多,推荐可以直接看下源码,喜欢就给我留言吧