/** * This ViewHolder has been bound to a position; mPosition, mItemId and mItemViewType * are all valid. ViewHolder 已经绑定到了一个位置,它的数据,mPosition,mItemId和 mItemViewType 都是有效的 */ staticfinalint FLAG_BOUND = 1 << 0;
/** * The data this ViewHolder's view reflects is stale and needs to be rebound * by the adapter. mPosition and mItemId are consistent. ViewHolder 的view 对应的数据需要更新和重新绑定,mPosition和mItemId没变 */ staticfinalint FLAG_UPDATE = 1 << 1;
/** * This ViewHolder's data is invalid. The identity implied by mPosition and mItemId * are not to be trusted and may no longer match the item view type. * This ViewHolder must be fully rebound to different data. ViewHolder 的数据完全无效,mPosition,mItemId,mItemViewType 都变了,必须重新完全绑定到一个不同的数据 */ staticfinalint FLAG_INVALID = 1 << 2;
/** * This ViewHolder points at data that represents an item previously removed from the * data set. Its view may still be used for things like outgoing animations. ViewHolder 的view 对应的数据被移除了,这个view可能仍然会被用在动画中 */ staticfinalint FLAG_REMOVED = 1 << 3;
/** * This ViewHolder should not be recycled. This flag is set via setIsRecyclable() * and is intended to keep views around during animations. ViewHolder 不应该被回收,用于在动画期间使用 */ staticfinalint FLAG_NOT_RECYCLABLE = 1 << 4;
/** * This ViewHolder is returned from scrap which means we are expecting an addView call * for this itemView. When returned from scrap, ViewHolder stays in the scrap list until * the end of the layout pass and then recycled by RecyclerView if it is not added back to * the RecyclerView. */ staticfinalint FLAG_RETURNED_FROM_SCRAP = 1 << 5;
/** * This ViewHolder is fully managed by the LayoutManager. We do not scrap, recycle or remove * it unless LayoutManager is replaced. * It is still fully visible to the LayoutManager. ViewHolder 完全被 LayoutManager 控制,Recycler 不处理它 */ staticfinalint FLAG_IGNORE = 1 << 7;
/** * When the View is detached form the parent, we set this flag so that we can take correct * action when we need to remove it or add it back. */ staticfinalint FLAG_TMP_DETACHED = 1 << 8;
/** * Set when we can no longer determine the adapter position of this ViewHolder until it is * rebound to a new position. It is different than FLAG_INVALID because FLAG_INVALID is * set even when the type does not match. Also, FLAG_ADAPTER_POSITION_UNKNOWN is set as soon * as adapter notification arrives vs FLAG_INVALID is set lazily before layout is * re-calculated. */ staticfinalint FLAG_ADAPTER_POSITION_UNKNOWN = 1 << 9;
/** * Set when a addChangePayload(null) is called */ staticfinalint FLAG_ADAPTER_FULLUPDATE = 1 << 10;
/** * Used by ItemAnimator when a ViewHolder's position changes */ staticfinalint FLAG_MOVED = 1 << 11;
/** * Used by ItemAnimator when a ViewHolder appears in pre-layout */ staticfinalint FLAG_APPEARED_IN_PRE_LAYOUT = 1 << 12;
publicvoiddetachAndScrapAttachedViews(Recycler recycler){ finalint childCount = getChildCount(); for (int i = childCount - 1; i >= 0; i--) { final View v = getChildAt(i); scrapOrRecycleView(recycler, i, v); } }
privatevoidscrapOrRecycleView(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); } else { detachViewAt(index); recycler.scrapView(view); mRecyclerView.mViewInfoStore.onViewDetached(viewHolder); } }
voidscrapView(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()) { thrownew 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.add(holder); } else { if (mChangedScrap == null) { mChangedScrap = new ArrayList<ViewHolder>(); } holder.setScrapContainer(this, true); mChangedScrap.add(holder); } }