這是RecyclerView
緩存機制系列文章的第四篇,系列文章的目錄以下:緩存
第一篇中遺留的一個問題尚未解決:複用表項時優先級最高的scrap view
是用來幹嗎的?這篇文章試着經過閱讀源碼來解答這個問題。bash
scrap view
對應的存儲結構是final ArrayList<ViewHolder> mAttachedScrap = new ArrayList<>();
。理解成員變量用途的最好辦法是 「搜索它在何時被訪問」 。對於列表結構來講就至關於 1. 在何時往列表添加內容? 2. 在何時清空列表內容?app
全局搜索mAttachedScrap
被訪問的地方,其中只有一處調用了mAttachedScrap.add()
:oop
public final class Recycler {
/**
* Mark an attached view as scrap.
* 回收ViewHolder到scrap集合(mAttachedScrap或mChangedScrap)
*
* <p>"Scrap" views are still attached to their parent RecyclerView but are eligible
* for rebinding and reuse. Requests for a view for a given position may return a
* reused or rebound scrap view instance.</p>
* scrap view依然依附於它的父親。。。
*
* @param view View to scrap
*/
void scrapView(View view) {
final ViewHolder holder = getChildViewHolderInt(view);
if (holder.hasAnyOfTheFlags(ViewHolder.FLAG_REMOVED | ViewHolder.FLAG_INVALID)
|| !holder.isUpdated() || canReuseUpdatedViewHolder(holder)) {
if (holder.isInvalid() && !holder.isRemoved() && !mAdapter.hasStableIds()) {
throw new IllegalArgumentException("Called scrap view with an invalid view."
+ " Invalid views cannot be reused from scrap, they should rebound from"
+ " recycler pool." + exceptionLabel());
}
holder.setScrapContainer(this, false);
//添加到mAttachedScrap集合中
mAttachedScrap.add(holder);
} else {
if (mChangedScrap == null) {
mChangedScrap = new ArrayList<ViewHolder>();
}
holder.setScrapContainer(this, true);
//添加到mChangedScrap集合中
mChangedScrap.add(holder);
}
}
}
複製代碼
沿着調用鏈繼續往上:佈局
public abstract static class LayoutManager {
private void scrapOrRecycleView(Recycler recycler, int index, View view) {
final ViewHolder viewHolder = getChildViewHolderInt(view);
if (viewHolder.shouldIgnore()) {
if (DEBUG) {
Log.d(TAG, "ignoring view " + viewHolder);
}
return;
}
//刪除表項併入回收池
if (viewHolder.isInvalid() && !viewHolder.isRemoved()
&& !mRecyclerView.mAdapter.hasStableIds()) {
removeViewAt(index);
recycler.recycleViewHolderInternal(viewHolder);
}
//detach表項併入scrap集合
else {
detachViewAt(index);
recycler.scrapView(view);
mRecyclerView.mViewInfoStore.onViewDetached(viewHolder);
}
}
}
複製代碼
根據viewHolder
的不一樣狀態,要麼將其添加到mAttachedScrap
集合,要麼將其存入回收池。其中recycleViewHolderInternal()
在RecyclerView緩存機制(回收去哪?)分析過。 沿着調用鏈繼續向上:post
public abstract static class LayoutManager {
/**
* Temporarily detach and scrap all currently attached child views. Views will be scrapped
* into the given Recycler. The Recycler may prefer to reuse scrap views before
* other views that were previously recycled.
* 暫時將當可見表項進行分離並回收
*
* @param recycler Recycler to scrap views into
*/
public void detachAndScrapAttachedViews(Recycler recycler) {
final int childCount = getChildCount();
//遍歷全部可見表項並回收他們
for (int i = childCount - 1; i >= 0; i--) {
final View v = getChildAt(i);
scrapOrRecycleView(recycler, i, v);
}
}
/**
* Lay out all relevant child views from the given adapter.
* 從給定的adapter佈局全部的孩子
*/
public void onLayoutChildren(Recycler recycler, State state) {
...
//在填充表項以前回收全部表項
detachAndScrapAttachedViews(recycler);
...
if (mAnchorInfo.mLayoutFromEnd) {
...
//填充表項
fill(recycler, mLayoutState, state, false);
...
}
...
}
}
public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2 {
//RecyclerView佈局的第二步
private void dispatchLayoutStep2() {
...
mLayout.onLayoutChildren(mRecycler, mState);
...
}
}
複製代碼
mAttachedScrap
中,回收數據的來源是LayoutManager
的孩子,而LayoutManager
的孩子都是屏幕上可見的表項。mAttachedScrap
用於屏幕中可見表項的回收和複用全局搜索mAttachedScrap
被訪問的地方,其中只有一處調用了mAttachedScrap.clear()
:性能
public final class Recycler {
void clearScrap() {
mAttachedScrap.clear();
if (mChangedScrap != null) {
mChangedScrap.clear();
}
}
}
public abstract static class LayoutManager {
/**
* Recycles the scrapped views.
* 回收全部scrapped view
*/
void removeAndRecycleScrapInt(Recycler recycler) {
final int scrapCount = recycler.getScrapCount();
// Loop backward, recycler might be changed by removeDetachedView()
//遍歷搜有scrap view並重置ViewHolder狀態
for (int i = scrapCount - 1; i >= 0; i--) {
final View scrap = recycler.getScrapViewAt(i);
final ViewHolder vh = getChildViewHolderInt(scrap);
if (vh.shouldIgnore()) {
continue;
}
vh.setIsRecyclable(false);
if (vh.isTmpDetached()) {
mRecyclerView.removeDetachedView(scrap, false);
}
if (mRecyclerView.mItemAnimator != null) {
mRecyclerView.mItemAnimator.endAnimation(vh);
}
vh.setIsRecyclable(true);
recycler.quickRecycleScrapView(scrap);
}
//清空scrap view集合
recycler.clearScrap();
if (scrapCount > 0) {
mRecyclerView.invalidate();
}
}
}
複製代碼
沿着調用鏈向上:ui
public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2 {
//RecyclerView佈局的最後一步
private void dispatchLayoutStep3() {
...
mLayout.removeAndRecycleScrapInt(mRecycler);
...
}
複製代碼
至此能夠得出結論:mAttachedScrap
生命週期起始於RecyclerView
佈局開始,終止於RecyclerView
佈局結束。this
通過四篇文章的分析,RecyclerVeiw
的四級緩存都分析完了,總結以下:spa
Recycler
有4個層次用於緩存ViewHolder
對象,優先級從高到底依次爲ArrayList<ViewHolder> mAttachedScrap
、ArrayList<ViewHolder> mCachedViews
、ViewCacheExtension mViewCacheExtension
、RecycledViewPool mRecyclerPool
。若是四層緩存都未命中,則從新建立並綁定ViewHolder
對象
緩存性能:
緩存 | 從新建立ViewHolder |
從新綁定數據 |
---|---|---|
mAttachedScrap | false | false |
mCachedViews | false | false |
mRecyclerPool | false | true |
緩存容量:
mAttachedScrap
:沒有大小限制,但最多包含屏幕可見表項。mCachedViews
:默認大小限制爲2,放不下時,按照先進先出原則將最早進入的ViewHolder
存入回收池以騰出空間。mRecyclerPool
:對ViewHolder
按viewType
分類存儲(經過SparseArray
),同類ViewHolder
存儲在默認大小爲5的ArrayList
中。緩存用途:
mAttachedScrap
:用於佈局過程當中屏幕可見表項的回收和複用。mCachedViews
:用於移出屏幕表項的回收和複用,且只能用於指定位置的表項,有點像「回收池預備隊列」,即老是先回收到mCachedViews
,當它放不下的時候,按照先進先出原則將最早進入的ViewHolder
存入回收池。mRecyclerPool
:用於移出屏幕表項的回收和複用,且只能用於指定viewType
的表項緩存結構:
mAttachedScrap
:ArrayList<ViewHolder>
mCachedViews
:ArrayList<ViewHolder>
mRecyclerPool
:對ViewHolder
按viewType
分類存儲在SparseArray<ScrapData>
中,同類ViewHolder
存儲在ScrapData
中的ArrayList
中