notifyDataSetChanged
notifyDataSetChanged和notifyItemChanged 區(qū)別
notifyDataSetChanged 調(diào)用到RecyclerViewDataObserver.onChanged()
@Override
public void onChanged() {
assertNotInLayoutOrScroll(null);
mState.mStructureChanged = true;
setDataSetChangedAfterLayout();
if (!mAdapterHelper.hasPendingUpdates()) {
requestLayout();
}
}
/**
* Set to true when an adapter data set changed notification is received.
* In that case, we cannot run any animations since we don't know what happened until layout.
*
* Attached items are invalid until next layout, at which point layout will animate/replace
* items as necessary, building up content from the (effectively) new adapter from scratch.
*
* Cached items must be discarded when setting this to true, so that the cache may be freely
* used by prefetching until the next layout occurs.
*
* @see #setDataSetChangedAfterLayout()
*/
boolean mDataSetHasChangedAfterLayout = false;
上面這個(gè)值會(huì)在adapter數(shù)據(jù)改變時(shí)設(shè)置為true ,dispatchLayoutStep3后設(shè)置為false
->RecyclerView.markKnownViewsInvalid() 所有子view的holder會(huì)添加
holder.addFlags(ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID);
requestLayout被調(diào)用后走到LinearLayoutManager onLayoutChildren->RecyclerView.scrapOrRecycleView 中viewHolder.isInvalid()成立
recycleViewHolderInternal->putRecycledView ->ViewHolder.resetInternal()
這里將ViewHolder的mFlags置為0. 后續(xù)走到tryGetViewHolderForPositionByDeadline方法中
!holder.isBound()成立后重新走bindViewHolder方法慕的。所以notifyDataSetChanged會(huì)將可見(jiàn)的子view重新bind一次挡鞍。
notifyItemChanged 調(diào)用到 RecyclerViewDataObserve.onItemRangeChanged
AdapterHelper
boolean onItemRangeChanged(int positionStart, int itemCount, Object payload) {
if (itemCount < 1) {
return false;
}
mPendingUpdates.add(obtainUpdateOp(UpdateOp.UPDATE, positionStart, itemCount, payload));
mExistingUpdateTypes |= UpdateOp.UPDATE;
return mPendingUpdates.size() == 1;
}
調(diào)用requestLayout后
在processAdapterUpdatesAndSetAnimationFlags方法中AdpterHelper.preProcess,applyUpdate。調(diào)用Rv.findViewHolderForPosition找到當(dāng)前position的viewHolder,AdpterHelper.postponeAndUpdateViewHolders
范圍內(nèi)的holder 執(zhí)行 holder.addFlags(ViewHolder.FLAG_UPDATE);
void viewRangeUpdate(int positionStart, int itemCount, Object payload) {
final int childCount = mChildHelper.getUnfilteredChildCount();
final int positionEnd = positionStart + itemCount;
for (int i = 0; i < childCount; i++) {
final View child = mChildHelper.getUnfilteredChildAt(i);
final ViewHolder holder = getChildViewHolderInt(child);
if (holder == null || holder.shouldIgnore()) {
continue;
}
if (holder.mPosition >= positionStart && holder.mPosition < positionEnd) {
// We re-bind these view holders after pre-processing is complete so that
// ViewHolders have their final positions assigned.
holder.addFlags(ViewHolder.FLAG_UPDATE);
holder.addChangePayload(payload);
// lp cannot be null since we get ViewHolder from it.
((LayoutParams) child.getLayoutParams()).mInsetsDirty = true;
}
}
mRecycler.viewRangeUpdate(positionStart, itemCount);
}
后續(xù)走到tryGetViewHolderForPositionByDeadline方法中holder.needsUpdate()成立重新走
bindViewHolder方法 所以notifyItemChanged只會(huì)去重新bind指定的view .
連續(xù)調(diào)用多次notifyItemChanged 每次調(diào)用會(huì)
mPendingUpdates.add(obtainUpdateOp(UpdateOp.UPDATE, positionStart, itemCount, payload));
第一次回去調(diào)用requestLayout 缸兔。在preProcess中統(tǒng)一處理mPendingUpdates
holder.addFlags(ViewHolder.FLAG_UPDATE);
void preProcess() {
mOpReorderer.reorderOps(mPendingUpdates);
final int count = mPendingUpdates.size();
for (int i = 0; i < count; i++) {
UpdateOp op = mPendingUpdates.get(i);
switch (op.cmd) {
case UpdateOp.ADD:
applyAdd(op);
break;
case UpdateOp.REMOVE:
applyRemove(op);
break;
case UpdateOp.UPDATE:
applyUpdate(op);
break;
case UpdateOp.MOVE:
applyMove(op);
break;
}
if (mOnItemProcessedCallback != null) {
mOnItemProcessedCallback.run();
}
}
mPendingUpdates.clear();
}
后續(xù)流程和上面一樣陨囊。
最近有個(gè)日歷加流水的需求爷怀,日歷可能本身就不太適合用RecyclerView寫,因?yàn)槭侨空故驹诮缑嫔系脑确W约和祽杏肦ecyclerView寫了 發(fā)現(xiàn)點(diǎn)擊反應(yīng)比較慢洒忧。排除了點(diǎn)擊事件分發(fā)問(wèn)題,發(fā)現(xiàn)每個(gè)子view bind一下還是要花個(gè)5-6ms帘撰。
一個(gè)android.support.v7.widget.GridLayoutManager.layoutChunk耗時(shí)132ms 簡(jiǎn)直慘
優(yōu)化后耗時(shí)需要變的耗時(shí)17ms跑慕,不需要改變的6ms。看來(lái)rv還是不適合這種場(chǎng)景