/** * internal implementation checks if view is scrapped or attached and throws an exception * if so. * Public version un-scraps before calling recycle. */ voidrecycleViewHolderInternal(ViewHolder holder) { if (holder.isScrap() || holder.itemView.getParent() != null) { thrownewIllegalArgumentException( "Scrapped or attached views may not be recycled. isScrap:" + holder.isScrap() + " isAttached:" + (holder.itemView.getParent() != null)); } if (holder.isTmpDetached()) { thrownewIllegalArgumentException("Tmp detached view should be removed " + "from RecyclerView before it can be recycled: " + holder); } if (holder.shouldIgnore()) { thrownewIllegalArgumentException("Trying to recycle an ignored view holder. You" + " should first call stopIgnoringView(view) before calling recycle."); } //noinspection unchecked finalbooleantransientStatePreventsRecycling= holder .doesTransientStatePreventRecycling(); finalbooleanforceRecycle= mAdapter != null && transientStatePreventsRecycling && mAdapter.onFailedToRecycleView(holder); booleancached=false; booleanrecycled=false; if (DEBUG && mCachedViews.contains(holder)) { thrownewIllegalArgumentException("cached view received recycle internal? " + holder); } if (forceRecycle || holder.isRecyclable()) { if (!holder.hasAnyOfTheFlags(ViewHolder.FLAG_INVALID | ViewHolder.FLAG_REMOVED | ViewHolder.FLAG_UPDATE)) { // Retire oldest cached view finalintcachedViewSize= mCachedViews.size(); if (cachedViewSize == mViewCacheMax && cachedViewSize > 0) { recycleCachedViewAt(0); } if (cachedViewSize < mViewCacheMax) { mCachedViews.add(holder); cached = true; } } if (!cached) { addViewHolderToRecycledViewPool(holder); recycled = true; } } elseif (DEBUG) { Log.d(TAG, "trying to recycle a non-recycleable holder. Hopefully, it will " + "re-visit here. We are still removing it from animation lists"); } // even if the holder is not removed, we still call this method so that it is removed // from view holder lists. mViewInfoStore.removeViewHolder(holder); if (!cached && !recycled && transientStatePreventsRecycling) { holder.mOwnerRecyclerView = null; } }
其中有一段代码:
1 2 3 4
if (cachedViewSize < mViewCacheMax) { mCachedViews.add(holder); cached = true; }