Android从源码分析RecyclerView四级缓存复用机制一(缓存ViewHolder)

//一级缓存中用来存储屏幕中显示的ViewHolder
final ArrayList mAttachedScrap = new ArrayList<>();
ArrayList mChangedScrap = null;
//二级缓存中用来存储屏幕外的ViewHolder
final ArrayList mCachedViews = new ArrayList();
//暂可忽略 mAttachedScrap的不可变视图
private final List
mUnmodifiableAttachedScrap = Collections.unmodifiableList(mAttachedScrap);

private int mRequestedCacheMax = DEFAULT_CACHE_SIZE;
//mCachedViews屏幕外缓存的存储上限默认为DEFAULT_CACHE_SIZE也就是2,可变
int mViewCacheMax = DEFAULT_CACHE_SIZE;
//四级缓存当屏幕外缓存的大小大于2,便放入mRecyclerPool中缓存
RecycledViewPool mRecyclerPool;
//三级缓存自定义缓存,自己定义的缓存规则
private ViewCacheExtension mViewCacheExtension;
//默认屏幕外缓存大小
static final int DEFAULT_CACHE_SIZE = 2;
//…

2.再看一下主要的方法调用流程,从RecyclerView的onMeasure方法开始一直到三个存储的地方一级,二级和四级缓存,别问为啥没有mViewCacheExtension,问就是这个你需要自己去存 Android从源码分析RecyclerView四级缓存复用机制一(缓存ViewHolder)

3.源码分析,本文对重点方法和重点代码进行分析(流程最好自己去跟一下)。

/**

  • 5.RecyclerView.scrapOrRecycleView
    */
    private void scrapOrRecycleView(Recycler recycler, int index, View view) {
    final ViewHolder viewHolder = getChildViewHolderInt(view);
    //只展示重点代码…
    if (viewHolder.isInvalid() && !viewHolder.isRemoved()
    && !mRecyclerView.mAdapter.hasStableIds()) {
    removeViewAt(index);
    //这里调用到二级和四级缓存
    recycler.recycleViewHolderInternal(viewHolder);
    } else {
    detachViewAt(index);
    //这里调用到一级缓存
    recycler.scrapView(view);
    mRecyclerView.mViewInfoStore.onViewDetached(viewHolder);
    }
    }

① 分析一级缓存

/**

  • 13.RecyclerView.scrapView
    */
    void scrapView(View view) {
    final ViewHolder holder = getChildViewHolderInt(view);
    if (holder.hasAnyOfTheFlags(ViewHolder.FLAG_REMOVED | ViewHolder.FLAG_INVALID)
    || !holder.isUpdated() || canReuseUpdatedViewHolder(holder)) {
    //…
    holder.setScrapContainer(this, false);
    //缓存adapter其他notify系列方法(包括notifyDataSetChanged)被移除的ViewHolder
    mAttachedScrap.add(holder);
    } else {
    if (mChangedScrap == null) {
    mChangedScrap = new ArrayList();
    }
    holder.setScrapContainer(this, true);
    //缓存adapter的notifyItemRangeChanged被移除的ViewHolder
    mChangedScrap.add(holder);
    }
    }

② 分析二级缓存

/**

  • 6.RecyclerView.recycleViewHolderInternal
    */
    void recycleViewHolderInternal(ViewHolder holder) {
    //…一系列是否需要二级回收的判断
    if (forceRecycle || holder.isRecyclable()) {
    if (mViewCacheMax > 0
    && !holder.hasAnyOfTheFlags(ViewHolder.FLAG_INVALID
    | ViewHolder.FLAG_REMOVED
    | ViewHolder.FLAG_UPDATE
    | ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN)) {
    // Retire oldest cached view
    int cachedViewSize = mCachedViews.size();
    //判断mCachedViews的大小是否大于2
    if (cachedViewSize >= mViewCacheMax && cachedViewSize > 0) {
    //重点分析一
    recycle

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

CachedViewAt(0);
cachedViewSize–;
}
//…计算targetCacheIndex的下标 让mCachedViews满足队列先进先出原则
mCachedViews.add(targetCacheIndex, holder);
cached = true;
}
if (!cached) {
//…如果二级缓存没有存储则添加到四级缓存
addViewHolderToRecycledViewPool(holder, true);
recycled = true;
}
} else {
//…
}
//…
}
/**

  • 重点分析一:7.RecyclerView.recycleViewHolderInternal
  • 作用:如果mCachedViews的大小大于2则内部调用addViewHolderToRecycledViewPool方法添加到RecycledViewPool中
    */
    void recycleCachedViewAt(int cachedViewIndex) {

ViewHolder viewHolder = mCachedViews.get(cachedViewIndex);
//Viewholder存储到四级缓存
addViewHolderToRecycledViewPool(viewHolder, true);
//Viewholder在四级缓存存储后移除mCachedViews中对应的Viewholder
mCachedViews.remove(cachedViewIndex);
}

③分析四级缓存

/**

  • 8.RecyclerView.recycleViewHolderInternal
    hedViews中对应的Viewholder
    mCachedViews.remove(cachedViewIndex);
    }

③分析四级缓存

/**

  • 8.RecyclerView.recycleViewHolderInternal
上一篇:山东大学项目实训-地图圈系统-APP(6)


下一篇:RecyclerView的复用机制,薪资翻倍