android recyclerview局部刷新,Android RecyclerView 局部刷新分析

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

dispatchLayout();

mFirstLayoutComplete = true;

}

//2.mInPreLayout 设置为 true 后面有用

mState.mInPreLayout = mState.mRunPredictiveAnimations;

//5.保存动画信息相关

mViewInfoStore.addToPreLayout(holder, animationInfo);

//3.如果holder确定要更新,就把它添加到 oldChangeHolders 集合中

long key = getChangedHolderKey(holder);

mViewInfoStore.addToOldChangeHolders(key, holder);

@VisibleForTesting

final LongSparseArray mOldChangedHolders = new LongSparseArray<>();

public void onLayoutChildren(Recycler recycler, State state) {

Log.e(TAG, "You must override onLayoutChildren(Recycler recycler, State state) ");

}

private void processAdapterUpdatesAndSetAnimationFlags() {

// 通常情况就是 ture

mState.mRunSimpleAnimations = true

// 通常情况就是 ture

mState.mRunPredictiveAnimations = true

mAdapterHelper.preProcess();

for (int i = 0; i < count; i++) {

UpdateOp op = mPendingUpdates.get(i);

case UpdateOp.UPDATE:

applyUpdate(op);

break;

UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount,

op.payload);

postponeAndUpdateViewHolders(newOp);

case UpdateOp.UPDATE:

mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload);

break;

void initAdapterManager() {

mAdapterHelper = new AdapterHelper(new AdapterHelper.Callback() {

@Override

public void markViewHoldersUpdated(int positionStart, int itemCount, Object payload) {

viewRangeUpdate(positionStart, itemCount, payload);

mItemsChanged = true;

}

void viewRangeUpdate(int positionStart, int itemCount, Object payload) {

final int childCount = mChildHelper.getUnfilteredChildCount();

final int positionEnd = positionStart + itemCount;

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;

}

processAdapterUpdatesAndSetAnimationFlags();

if (mState.mTrackOldChangeHolders && holder.isUpdated() && !holder.isRemoved()

&& !holder.shouldIgnore() && !holder.isInvalid()) {

long key = getChangedHolderKey(holder);

// This is NOT the only place where a ViewHolder is added to old change holders

// list. There is another case where:

//    * A VH is currently hidden but not deleted

//    * The hidden item is changed in the adapter

//    * Layout manager decides to layout the item in the pre-Layout pass (step1)

// When this case is detected, RV will un-hide that view and add to the old

// change holders list.

mViewInfoStore.addToOldChangeHolders(key, holder);

}

@VisibleForTesting

final LongSparseArray mOldChangedHolders = new LongSparseArray<>();

//4.很重要,LayoutManager 开始工作

mLayout.onLayoutChildren(mRecycler, mState);

for (int i = childCount - 1; i >= 0; i--) {

final View v = getChildAt(i);

scrapOrRecycleView(recycler, i, v);

}

return ((LayoutParams) child.getLayoutParams()).mViewHolder;

recycler.scrapView(view);

不更新

mAttachedScrap.add(holder);

public final class Recycler {

final ArrayList mAttachedScrap = new ArrayList<>();

更新的话

mChangedScrap.add(holder);

ArrayList mChangedScrap = null;

ViewHolder tryGetViewHolderForPositionByDeadline(int position,

boolean dryRun, long deadlineNs) {

if (mState.isPreLayout()) {

holder = getChangedScrapViewForPosition(position);

final ViewHolder holder = mChangedScrap.get(i);

holder = getScrapOrHiddenOrCachedHolderForPosition(position, dryRun);

final ViewHolder holder = mAttachedScrap.get(i);

Recycler

final ViewHolder holder = mCachedViews.get(i);

final ArrayList mCachedViews = new ArrayList();

final int offsetPosition = mAdapterHelper.findPositionOffset(position);

...

final int type = mAdapter.getItemViewType(offsetPosition);

final View view = mViewCacheExtension

.getViewForPositionAndType(this, position, type);

//4. 从 RecycledViewPool 中查找

holder = getRecycledViewPool().getRecycledView(type);

...

//5. 老实创建

holder = mAdapter.createViewHolder(RecyclerView.this, type);

...

dispatchLayoutStep2

// Step 2: Run layout

mState.mInPreLayout = false;

mLayout.onLayoutChildren(mRecycler, mState);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值