美文网首页
优化 :Payloads

优化 :Payloads

作者: 緦菍亭芷 | 来源:发表于2022-02-08 18:39 被阅读0次

Adapter.notifyItemChanged(int position, @Nullable Object payload)
场景:更新某一部分数据

Adapter

    public abstract static class Adapter<VH extends ViewHolder> {
        private final AdapterDataObservable mObservable = new AdapterDataObservable();
    
        public void registerAdapterDataObserver(@NonNull AdapterDataObserver observer) {
            mObservable.registerObserver(observer);
        }

        public final void notifyItemChanged(int position, @Nullable Object payload) {
            mObservable.notifyItemRangeChanged(position, 1, payload);
        }


        public final void bindViewHolder(@NonNull VH holder, int position) {
            ...
            onBindViewHolder(holder, position, holder.getUnmodifiedPayloads());
            ...
        }

        public void onBindViewHolder(@NonNull VH holder, int position,
                                     @NonNull List<Object> payloads) {
            onBindViewHolder(holder, position);
        }

    }

AdapterDataObservable

    static class AdapterDataObservable extends Observable<AdapterDataObserver> {
        public void notifyItemRangeChanged(int positionStart, int itemCount,
                                           @Nullable Object payload) {
            ...
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemRangeChanged(positionStart, itemCount, payload);
            }
        }
    }

RecyclerViewDataObserver

    private class RecyclerViewDataObserver extends AdapterDataObserver {

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            ...
            if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount, payload)) {
                triggerUpdateProcessor();
            }
        }

        void triggerUpdateProcessor() {
            if (POST_UPDATES_ON_ANIMATION && mHasFixedSize && mIsAttached) {
                ViewCompat.postOnAnimation(RecyclerView.this, mUpdateChildViewsRunnable);
            } else {
                mAdapterUpdateDuringMeasure = true;
                requestLayout();
            }
        }

    }

AdapterHelper

final class AdapterHelper implements OpReorderer.Callback {
    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;
    }

    //自身方法调用 applyUpdate
    void dispatchFirstPassAndUpdateViewHolders(UpdateOp op, int offsetStart) {
        mCallback.onDispatchFirstPass(op);
        switch (op.cmd) {
            case UpdateOp.REMOVE:
                mCallback.offsetPositionsForRemovingInvisible(offsetStart, op.itemCount);
                break;
            case UpdateOp.UPDATE:
                mCallback.markViewHoldersUpdated(offsetStart, op.itemCount, op.payload);
                break;
            default:
                throw new IllegalArgumentException("only remove and update ops can be dispatched"
                        + " in first pass");
        }
    }

    //自身方法调用 preProcess()
   private void postponeAndUpdateViewHolders(UpdateOp op) {
        if (DEBUG) {
            Log.d(TAG, "postponing " + op);
        }
        L.INSTANCE.i(TAG +" postponeAndUpdateViewHolders");
        mPostponedList.add(op);
        switch (op.cmd) {
            case UpdateOp.ADD:
                mCallback.offsetPositionsForAdd(op.positionStart, op.itemCount);
                break;
            case UpdateOp.MOVE:
                mCallback.offsetPositionsForMove(op.positionStart, op.itemCount);
                break;
            case UpdateOp.REMOVE:
                mCallback.offsetPositionsForRemovingLaidOutOrNewView(op.positionStart,
                        op.itemCount);
                break;
            case UpdateOp.UPDATE:
                mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload);
                break;
            default:
                throw new IllegalArgumentException("Unknown update op type for " + op);
        }
    }

    //RecyclerView.onMeasure
    //RecyclerView.processAdapterUpdatesAndSetAnimationFlags
    //RecyclerView.dispatchLayoutStep2
    void consumeUpdatesInOnePass() {
        // we still consume postponed updates (if there is) in case there was a pre-process call
        // w/o a matching consumePostponedUpdates.
        consumePostponedUpdates();
        final int count = mPendingUpdates.size();
        for (int i = 0; i < count; i++) {
            UpdateOp op = mPendingUpdates.get(i);
            switch (op.cmd) {
                case UpdateOp.ADD:
                    mCallback.onDispatchSecondPass(op);
                    mCallback.offsetPositionsForAdd(op.positionStart, op.itemCount);
                    break;
                case UpdateOp.REMOVE:
                    mCallback.onDispatchSecondPass(op);
                    mCallback.offsetPositionsForRemovingInvisible(op.positionStart, op.itemCount);
                    break;
                case UpdateOp.UPDATE:
                    mCallback.onDispatchSecondPass(op);
                    mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload);
                    break;
                case UpdateOp.MOVE:
                    mCallback.onDispatchSecondPass(op);
                    mCallback.offsetPositionsForMove(op.positionStart, op.itemCount);
                    break;
            }
            if (mOnItemProcessedCallback != null) {
                mOnItemProcessedCallback.run();
            }
        }
        recycleUpdateOpsAndClearList(mPendingUpdates);
        mExistingUpdateTypes = 0;
    }


    interface Callback {

        RecyclerView.ViewHolder findViewHolder(int position);

        void offsetPositionsForRemovingInvisible(int positionStart, int itemCount);

        void offsetPositionsForRemovingLaidOutOrNewView(int positionStart, int itemCount);

        void markViewHoldersUpdated(int positionStart, int itemCount, Object payloads);

        void onDispatchFirstPass(UpdateOp updateOp);

        void onDispatchSecondPass(UpdateOp updateOp);

        void offsetPositionsForAdd(int positionStart, int itemCount);

        void offsetPositionsForMove(int from, int to);
    }

}

RecyclerView

public class RecyclerView extends ViewGroup implements ScrollingView,
        NestedScrollingChild2, NestedScrollingChild3 {

    private final RecyclerViewDataObserver mObserver = new RecyclerViewDataObserver();

    public void setAdapter(@Nullable Adapter adapter) {
        ...
        setAdapterInternal(adapter, false, true);
        ...
    }

    private void setAdapterInternal(@Nullable Adapter adapter, boolean compatibleWithPrevious,
                                    boolean removeAndRecycleViews) {
        ...
        if (adapter != null) {
            adapter.registerAdapterDataObserver(mObserver);
            adapter.onAttachedToRecyclerView(this);
        }
        ...
    }

    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 dispatchUpdate(AdapterHelper.UpdateOp op) {
                switch (op.cmd) {
                    case AdapterHelper.UpdateOp.ADD:
                        mLayout.onItemsAdded(RecyclerView.this, op.positionStart, op.itemCount);
                        break;
                    case AdapterHelper.UpdateOp.REMOVE:
                        mLayout.onItemsRemoved(RecyclerView.this, op.positionStart, op.itemCount);
                        break;
                    case AdapterHelper.UpdateOp.UPDATE:
                        mLayout.onItemsUpdated(RecyclerView.this, op.positionStart, op.itemCount,
                                op.payload);
                        break;
                    case AdapterHelper.UpdateOp.MOVE:
                        mLayout.onItemsMoved(RecyclerView.this, op.positionStart, op.itemCount, 1);
                        break;
                }
            }

           
        });
    }

    void viewRangeUpdate(int positionStart, int itemCount, Object payload) {
      
        for (int i = 0; i < childCount; i++) {
            final View child = mChildHelper.getUnfilteredChildAt(i);
            final ViewHolder holder = getChildViewHolderInt(child);
          
            if (holder.mPosition >= positionStart && holder.mPosition < positionEnd) {
                holder.addFlags(ViewHolder.FLAG_UPDATE);
                holder.addChangePayload(payload);
            }
        }
    }

}
image.png

相关文章

  • 优化 :Payloads

    Adapter.notifyItemChanged(int position, @Nullable Object ...

  • Metasploit Payloads

    List payloads Binaries Linux Windows Mac Web Payloads PHP...

  • redux学习

    一、Actions 1.Actions are payloads of information that send...

  • Redux学习

    redux组件的构成 Action Actions are payloads of information tha...

  • 8.5 Fuzz

    8.5.1. Web Fuzz wfuzz SecLists fuzzdb foospidy payloads 8...

  • msfconsole常用命令

    命令: show exploits 命令:show payloads 命令:show auxiliary 命令:s...

  • 3-payloads

    步骤一: 打开kali和Metasploit 当我们打开Metasploit, 我们会看到如下的截图: 这里显示了...

  • msfvenom使用

    --list 列出所有可用的payloads,encoders,nops --help-formats 查看支持的...

  • msfconsole

    Exploit 模块 --》漏洞利用 payloads --》shellcode 漏洞利用后执行的代码 use e...

  • Metasploit 常用渗透命令大全【转】

    show exploits列出metasploit框架中的所有渗透攻击模块。show payloads列出meta...

网友评论

      本文标题:优化 :Payloads

      本文链接:https://www.haomeiwen.com/subject/cjnbkrtx.html