美文网首页
Android中RecyclerView源码解析一(Adapte

Android中RecyclerView源码解析一(Adapte

作者: v587的毅哥 | 来源:发表于2018-08-01 17:32 被阅读34次

    小白也能看懂源码系列

    第一步

    先看下面这个普通的抽象类:Observable它其实里面就一个ArrayList,只做了一个addremove的操作而已。

    Observable.java
    
    public abstract class Observable<T>{
    
        protected final ArrayList<T> mObservers = new ArrayList<T>();
    
        public void registerObserver(T observer) {
            //④
            mObservers.add(observer);
        }
        public void unregisterObserver(T observer) {
            mObservers.remove(index);
        }
        public void unregisterAll(T observer) {
            mObservers.clear();
        }
    }
    

    第二步

    直接上精简版的RecyclerView

    RecyclerView.java
    public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2 {
    
        Adapter mAdapter;
        private final RecyclerViewDataObserver mObserver = new RecyclerViewDataObserver();
    
        public void setAdapter(Adapter adapter) {
            setAdapterInternal(adapter, false, true);
        }
        private void setAdapterInternal(Adapter adapter, boolean compatibleWithPrevious, boolean removeAndRecycleViews) {
            if (mAdapter != null) {
                    mAdapter.unregisterAdapterDataObserver(mObserver);
            }
            final Adapter oldAdapter = mAdapter;
            mAdapter = adapter;
            if (adapter != null) {
                   //①注意参数哈,其实就是RecyclerView里的成员变量RecyclerViewDataObserver mObserver
                    adapter.registerAdapterDataObserver(mObserver);
            }
        }
    
        //内部类:AdapterDataObservable
        static class AdapterDataObservable extends Observable<AdapterDataObserver>{
    
            //③其实就是调用的super.registerObserver(RecyclerViewDataObserver mObserver),super就是指第一步那个Observable类
            //此时:RecyclerView.mAdapter.mObservable.mObservers.get(0) = mObserver
    
    
            //mObservers指的是其父类的ArrayList而已
            public void notifyChanged() {
                for (int i = mObservers.size() - 1; i >= 0; i--) {
                        mObservers.get(i).onChanged();
                }
            }
            public void notifyItemRangeChanged(int positionStart, int itemCount) {
                notifyItemRangeChanged(positionStart, itemCount, null);
            }
            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 {
            RecyclerViewDataObserver() {}
        
            @Override
            public void onChanged() {
                mAdapterHelper.hasPendingUpdates();
            }
    
            @Override
            public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
                mAdapterHelper.onItemRangeChanged(positionStart, itemCount, payload)        
            }
            ……
        }
        //内部类:Adapter
        public abstract static class Adapter<VH extends ViewHolder> {
            private final AdapterDataObservable mObservable = new AdapterDataObservable();
    
            public abstract VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType);
            public abstract void onBindViewHolder(@NonNull VH holder, int position);
            public abstract int getItemCount();
    
            public int getItemViewType(int position) {
                    return 0;
            }
            public long getItemId(int position) {
                    return NO_ID;
            }
            public void onViewRecycled(@NonNull VH holder) {
                //空实现
            }
    
            public void registerAdapterDataObserver(@NonNull AdapterDataObserver observer) {
                    //②
                    mObservable.registerObserver(observer);
            }
    
            public void unregisterAdapterDataObserver(@NonNull AdapterDataObserver observer) {
                    mObservable.unregisterObserver(observer);
            }
    
            //当用户主动调用这个方法的时候(通常是:yourAdapterWhichExtendsRecyclerViewAdapter.notifyDataSetChanged())
            //此时:相当于调用的是mObservable(即: AdapterDataObservable)的notifyChanged()方法。
            //而这个方法的作用是:把RecyclerView.mAdapter.mObservable.mObservers里list存放的RecyclerViewDataObserver些循环同时调用其onChanged方法。
            public final void notifyDataSetChanged() {
                    mObservable.notifyChanged();
            }
        
            ……
        }
    }
    

    第三步

    模拟debug方式一步一步运行。当调用RecyclerView#setAdapter()后的运行步骤如上①②③④所示:在setAdapter的执行完成后就实现了RecyclerView.mAdapter.mObservable.mObservers.get(0) = mObserver,而这个mObserver就是RecyclerView里的成员变量private final RecyclerViewDataObserver mObserver = new RecyclerViewDataObserver()

    当用户手动调用adapter.notifyDataSetChanged()adapter.notifyItemChanged(int position)之类的方法的时候其实就是调用了mObserver里的onChanged()onItemRangeChanged(int positionStart, int itemCount)。而具体的RecyclerView在界面上体现出来的变化则是通过mAdapterHelper.hasPendingUpdates()mAdapterHelper.onItemRangeChanged(positionStart, itemCount, payload)之类来实现的。具体的得进入到mAdapterHelper里再看。下回分解~~~

    相关文章

      网友评论

          本文标题:Android中RecyclerView源码解析一(Adapte

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