变量名 | 属性 | 说明 |
---|---|---|
mAttachedScrap | ArrayList<ViewHolder> | 屏幕内viewholder的快速重用 |
mChangedScrap | ArrayList<ViewHolder> | |
mCachedViews | ArrayList<ViewHolder> | 移除屏幕外的viewholder缓存集合,size默认为2,当大于2时,将第一个,移动到recyclerpool,并将移出屏幕外的viewholder,加入到cachedview集合里面 |
mViewCacheExtension | ViewCacheExtension | 用户自己实现的缓存类 |
mRecyclerPool | RecycledViewPool | 多个recyclerview之间共享viewholder缓冲池。缓存viewholder的对象,当mCachedViews长度大于2,就会把mCachedViews超出的内容放到mRecyclerPool,然后给viewholder复用。 |
// 1) Find by position from scrap/hidden list/cache
if (holder == null) {
holder = getScrapOrHiddenOrCachedHolderForPosition(position, dryRun);
if (holder != null) {
if (!validateViewHolderForOffsetPosition(holder)) {
// recycle holder (and unscrap if relevant) since it can't be used
if (!dryRun) {
// we would like to recycle this but need to make sure it is not used by
// animation logic etc.
holder.addFlags(ViewHolder.FLAG_INVALID);
if (holder.isScrap()) {
removeDetachedView(holder.itemView, false);
holder.unScrap();
} else if (holder.wasReturnedFromScrap()) {
holder.clearReturnedFromScrapFlag();
}
recycleViewHolderInternal(holder);
}
holder = null;
} else {
fromScrapOrHiddenOrCache = true;
}
}
}
if (holder == null) {
final int offsetPosition = mAdapterHelper.findPositionOffset(position);
if (offsetPosition < 0 || offsetPosition >= mAdapter.getItemCount()) {
throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item "
+ "position " + position + "(offset:" + offsetPosition + ")."
+ "state:" + mState.getItemCount() + exceptionLabel());
}
final int type = mAdapter.getItemViewType(offsetPosition);
// 2) Find from scrap/cache via stable ids, if exists
if (mAdapter.hasStableIds()) {
holder = getScrapOrCachedViewForId(mAdapter.getItemId(offsetPosition),
type, dryRun);
if (holder != null) {
// update position
holder.mPosition = offsetPosition;
fromScrapOrHiddenOrCache = true;
}
}
if (holder == null && mViewCacheExtension != null) {
// We are NOT sending the offsetPosition because LayoutManager does not
// know it.
final View view = mViewCacheExtension
.getViewForPositionAndType(this, position, type);
if (view != null) {
holder = getChildViewHolder(view);
if (holder == null) {
throw new IllegalArgumentException("getViewForPositionAndType returned"
+ " a view which does not have a ViewHolder"
+ exceptionLabel());
} else if (holder.shouldIgnore()) {
throw new IllegalArgumentException("getViewForPositionAndType returned"
+ " a view that is ignored. You must call stopIgnoring before"
+ " returning this view." + exceptionLabel());
}
}
}
if (holder == null) { // fallback to pool
if (DEBUG) {
Log.d(TAG, "tryGetViewHolderForPositionByDeadline("
+ position + ") fetching from shared pool");
}
holder = getRecycledViewPool().getRecycledView(type);
if (holder != null) {
holder.resetInternal();
if (FORCE_INVALIDATE_DISPLAY_LIST) {
invalidateDisplayListInt(holder);
}
}
}
if (holder == null) {
long start = getNanoTime();
if (deadlineNs != FOREVER_NS
&& !mRecyclerPool.willCreateInTime(type, start, deadlineNs)) {
// abort - we have a deadline we can't meet
return null;
}
holder = mAdapter.createViewHolder(RecyclerView.this, type);
if (ALLOW_THREAD_GAP_WORK) {
// only bother finding nested RV if prefetching
RecyclerView innerView = findNestedRecyclerView(holder.itemView);
if (innerView != null) {
holder.mNestedRecyclerView = new WeakReference<>(innerView);
}
}
long end = getNanoTime();
mRecyclerPool.factorInCreateTime(type, end - start);
if (DEBUG) {
Log.d(TAG, "tryGetViewHolderForPositionByDeadline created new ViewHolder");
}
}
}
recyclerview 的 getViewForPosition 会先判断holder为不为空,为空就去getScrapViewForPosition 方法获取holder,如果这时候holder还是空,就去执行viewCacheExtension$getViewFromPositionAndType方法,返回view 并且实例化成holder。第三部,如果这时候 holder还是空的话,就从recyclerviewpool调用getRecyclerView里面根据type获取 holder。第四部。如果这时候 holder还是空的话,就去调用adapter的oncreateViewHolder方法。返回holder。经过以上四部,最后holder肯定不是空了。
流程
一开始缓存都是空的,所以会走oncreateviewholder方法来创建viewholder,然后通过onbindviewholder来绑定数据。
假如屏幕有一次只能显示3个item,然后将item0 滑出屏幕,就会将item0加入到长度为空的mcacheviews,同理item1也滑出屏幕,加入mcacheviews。当item2滑出屏幕,而mcacheviews默认长度是2,就会将item0转移到recyclerdviewpool中,item2放到mcacheviews中。然后继续创建item3 item4 item5,(其中item0recyclerdviewpool,item1和item2在mcacheviews)在当要创建item6时,复用了recyclerdviewpool里面的item0了。然后循环复用recyclerdviewpool里面的item。