RecyclerView.java revision 5e79cc4e2c96b0dd1d4de6f3208f9da3bcb69d26
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.support.v7.widget;
19
20import android.content.Context;
21import android.database.Observable;
22import android.graphics.Canvas;
23import android.graphics.PointF;
24import android.graphics.Rect;
25import android.os.Build;
26import android.os.Parcel;
27import android.os.Parcelable;
28import android.support.annotation.Nullable;
29import android.support.v4.util.ArrayMap;
30import android.support.v4.view.MotionEventCompat;
31import android.support.v4.view.VelocityTrackerCompat;
32import android.support.v4.view.ViewCompat;
33import android.support.v4.widget.EdgeEffectCompat;
34import android.support.v4.widget.ScrollerCompat;
35import static android.support.v7.widget.AdapterHelper.UpdateOp;
36import static android.support.v7.widget.AdapterHelper.Callback;
37import android.util.AttributeSet;
38import android.util.Log;
39import android.util.SparseArray;
40import android.util.SparseIntArray;
41import android.view.FocusFinder;
42import android.view.MotionEvent;
43import android.view.VelocityTracker;
44import android.view.View;
45import android.view.ViewConfiguration;
46import android.view.ViewGroup;
47import android.view.ViewParent;
48import android.view.animation.Interpolator;
49
50import java.util.ArrayList;
51import java.util.Collections;
52import java.util.List;
53
54/**
55 * A flexible view for providing a limited window into a large data set.
56 *
57 * <h3>Glossary of terms:</h3>
58 *
59 * <ul>
60 *     <li><em>Adapter:</em> A subclass of {@link Adapter} responsible for providing views
61 *     that represent items in a data set.</li>
62 *     <li><em>Position:</em> The position of a data item within an <em>Adapter</em>.</li>
63 *     <li><em>Index:</em> The index of an attached child view as used in a call to
64 *     {@link ViewGroup#getChildAt}. Contrast with <em>Position.</em></li>
65 *     <li><em>Binding:</em> The process of preparing a child view to display data corresponding
66 *     to a <em>position</em> within the adapter.</li>
67 *     <li><em>Recycle (view):</em> A view previously used to display data for a specific adapter
68 *     position may be placed in a cache for later reuse to display the same type of data again
69 *     later. This can drastically improve performance by skipping initial layout inflation
70 *     or construction.</li>
71 *     <li><em>Scrap (view):</em> A child view that has entered into a temporarily detached
72 *     state during layout. Scrap views may be reused without becoming fully detached
73 *     from the parent RecyclerView, either unmodified if no rebinding is required or modified
74 *     by the adapter if the view was considered <em>dirty</em>.</li>
75 *     <li><em>Dirty (view):</em> A child view that must be rebound by the adapter before
76 *     being displayed.</li>
77 * </ul>
78 */
79public class RecyclerView extends ViewGroup {
80    private static final String TAG = "RecyclerView";
81
82    private static final boolean DEBUG = false;
83
84    /**
85     * On Kitkat, there is a bug which prevents DisplayList from being invalidated if a View is two
86     * levels deep(wrt to ViewHolder.itemView). DisplayList can be invalidated by setting
87     * View's visibility to INVISIBLE when View is detached. On Kitkat, Recycler recursively
88     * traverses itemView and invalidates display list for each ViewGroup that matches this
89     * criteria.
90     */
91    private static final boolean FORCE_INVALIDATE_DISPLAY_LIST = Build.VERSION.SDK_INT == 19 ||
92            Build.VERSION.SDK_INT == 20;
93
94    private static final boolean DISPATCH_TEMP_DETACH = false;
95    public static final int HORIZONTAL = 0;
96    public static final int VERTICAL = 1;
97
98    public static final int NO_POSITION = -1;
99    public static final long NO_ID = -1;
100    public static final int INVALID_TYPE = -1;
101
102    private static final int MAX_SCROLL_DURATION = 2000;
103
104    private final RecyclerViewDataObserver mObserver = new RecyclerViewDataObserver();
105
106    final Recycler mRecycler = new Recycler();
107
108    private SavedState mPendingSavedState;
109
110    AdapterHelper mAdapterHelper;
111
112    ChildHelper mChildHelper;
113
114    final List<View> mDisappearingViewsInLayoutPass = new ArrayList<View>();
115
116    /**
117     * Prior to L, there is no way to query this variable which is why we override the setter and
118     * track it here.
119     */
120    private boolean mClipToPadding;
121
122    /**
123     * Note: this Runnable is only ever posted if:
124     * 1) We've been through first layout
125     * 2) We know we have a fixed size (mHasFixedSize)
126     * 3) We're attached
127     */
128    private final Runnable mUpdateChildViewsRunnable = new Runnable() {
129        public void run() {
130            if (!mAdapterHelper.hasPendingUpdates()) {
131                return;
132            }
133            if (!mFirstLayoutComplete) {
134                // a layout request will happen, we should not do layout here.
135                return;
136            }
137            if (mDataSetHasChangedAfterLayout) {
138                dispatchLayout();
139            } else {
140                eatRequestLayout();
141                mAdapterHelper.preProcess();
142                if (!mLayoutRequestEaten) {
143                    // We run this after pre-processing is complete so that ViewHolders have their
144                    // final adapter positions. No need to run it if a layout is already requested.
145                    rebindUpdatedViewHolders();
146                }
147                resumeRequestLayout(true);
148            }
149        }
150    };
151
152    private final Rect mTempRect = new Rect();
153    private Adapter mAdapter;
154    private LayoutManager mLayout;
155    private RecyclerListener mRecyclerListener;
156    private final ArrayList<ItemDecoration> mItemDecorations = new ArrayList<ItemDecoration>();
157    private final ArrayList<OnItemTouchListener> mOnItemTouchListeners =
158            new ArrayList<OnItemTouchListener>();
159    private OnItemTouchListener mActiveOnItemTouchListener;
160    private boolean mIsAttached;
161    private boolean mHasFixedSize;
162    private boolean mFirstLayoutComplete;
163    private boolean mEatRequestLayout;
164    private boolean mLayoutRequestEaten;
165    private boolean mAdapterUpdateDuringMeasure;
166    private final boolean mPostUpdatesOnAnimation;
167
168    /**
169     * Set to true when an adapter data set changed notification is received.
170     * In that case, we cannot run any animations since we don't know what happened.
171     */
172    private boolean mDataSetHasChangedAfterLayout = false;
173
174    /**
175     * This variable is set to true during a dispatchLayout and/or scroll.
176     * Some methods should not be called during these periods (e.g. adapter data change).
177     * Doing so will create hard to find bugs so we better check it and throw an exception.
178     *
179     * @see #assertInLayoutOrScroll(String)
180     * @see #assertNotInLayoutOrScroll(String)
181     */
182    private boolean mRunningLayoutOrScroll = false;
183
184    private EdgeEffectCompat mLeftGlow, mTopGlow, mRightGlow, mBottomGlow;
185
186    ItemAnimator mItemAnimator = new DefaultItemAnimator();
187
188    private static final int INVALID_POINTER = -1;
189
190    /**
191     * The RecyclerView is not currently scrolling.
192     * @see #getScrollState()
193     */
194    public static final int SCROLL_STATE_IDLE = 0;
195
196    /**
197     * The RecyclerView is currently being dragged by outside input such as user touch input.
198     * @see #getScrollState()
199     */
200    public static final int SCROLL_STATE_DRAGGING = 1;
201
202    /**
203     * The RecyclerView is currently animating to a final position while not under
204     * outside control.
205     * @see #getScrollState()
206     */
207    public static final int SCROLL_STATE_SETTLING = 2;
208
209    // Touch/scrolling handling
210
211    private int mScrollState = SCROLL_STATE_IDLE;
212    private int mScrollPointerId = INVALID_POINTER;
213    private VelocityTracker mVelocityTracker;
214    private int mInitialTouchX;
215    private int mInitialTouchY;
216    private int mLastTouchX;
217    private int mLastTouchY;
218    private final int mTouchSlop;
219    private final int mMinFlingVelocity;
220    private final int mMaxFlingVelocity;
221
222    private final ViewFlinger mViewFlinger = new ViewFlinger();
223
224    final State mState = new State();
225
226    private OnScrollListener mScrollListener;
227
228    // For use in item animations
229    boolean mItemsAddedOrRemoved = false;
230    boolean mItemsChanged = false;
231    private ItemAnimator.ItemAnimatorListener mItemAnimatorListener =
232            new ItemAnimatorRestoreListener();
233    private boolean mPostedAnimatorRunner = false;
234    private Runnable mItemAnimatorRunner = new Runnable() {
235        @Override
236        public void run() {
237            if (mItemAnimator != null) {
238                mItemAnimator.runPendingAnimations();
239            }
240            mPostedAnimatorRunner = false;
241        }
242    };
243
244    private static final Interpolator sQuinticInterpolator = new Interpolator() {
245        public float getInterpolation(float t) {
246            t -= 1.0f;
247            return t * t * t * t * t + 1.0f;
248        }
249    };
250
251    public RecyclerView(Context context) {
252        this(context, null);
253    }
254
255    public RecyclerView(Context context, AttributeSet attrs) {
256        this(context, attrs, 0);
257    }
258
259    public RecyclerView(Context context, AttributeSet attrs, int defStyle) {
260        super(context, attrs, defStyle);
261
262        final int version = Build.VERSION.SDK_INT;
263        mPostUpdatesOnAnimation = version >= 16;
264
265        final ViewConfiguration vc = ViewConfiguration.get(context);
266        mTouchSlop = vc.getScaledTouchSlop();
267        mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();
268        mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
269        setWillNotDraw(ViewCompat.getOverScrollMode(this) == ViewCompat.OVER_SCROLL_NEVER);
270
271        mItemAnimator.setListener(mItemAnimatorListener);
272        initAdapterManager();
273        initChildrenHelper();
274    }
275
276    private void initChildrenHelper() {
277        mChildHelper = new ChildHelper(new ChildHelper.Callback() {
278            @Override
279            public int getChildCount() {
280                return RecyclerView.this.getChildCount();
281            }
282
283            @Override
284            public void addView(View child, int index) {
285                RecyclerView.this.addView(child, index);
286            }
287
288            @Override
289            public int indexOfChild(View view) {
290                return RecyclerView.this.indexOfChild(view);
291            }
292
293            @Override
294            public void removeViewAt(int index) {
295                RecyclerView.this.removeViewAt(index);
296            }
297
298            @Override
299            public View getChildAt(int offset) {
300                return RecyclerView.this.getChildAt(offset);
301            }
302
303            @Override
304            public void removeAllViews() {
305                RecyclerView.this.removeAllViews();
306            }
307
308            @Override
309            public ViewHolder getChildViewHolder(View view) {
310                return getChildViewHolderInt(view);
311            }
312
313            @Override
314            public void attachViewToParent(View child, int index,
315                    ViewGroup.LayoutParams layoutParams) {
316                RecyclerView.this.attachViewToParent(child, index, layoutParams);
317            }
318
319            @Override
320            public void detachViewFromParent(int offset) {
321                RecyclerView.this.detachViewFromParent(offset);
322            }
323        });
324    }
325
326    void initAdapterManager() {
327        mAdapterHelper = new AdapterHelper(new Callback() {
328            @Override
329            public ViewHolder findViewHolder(int position) {
330                return findViewHolderForPosition(position, true);
331            }
332
333            @Override
334            public void offsetPositionsForRemovingInvisible(int start, int count) {
335                offsetPositionRecordsForRemove(start, count, true);
336                mItemsAddedOrRemoved = true;
337                mState.mDeletedInvisibleItemCountSincePreviousLayout += count;
338            }
339
340            @Override
341            public void offsetPositionsForRemovingLaidOutOrNewView(int positionStart, int itemCount) {
342                offsetPositionRecordsForRemove(positionStart, itemCount, false);
343                mItemsAddedOrRemoved = true;
344            }
345
346            @Override
347            public void markViewHoldersUpdated(int positionStart, int itemCount) {
348                viewRangeUpdate(positionStart, itemCount);
349                mItemsChanged = true;
350            }
351
352            @Override
353            public void onDispatchFirstPass(UpdateOp op) {
354                dispatchUpdate(op);
355            }
356
357            void dispatchUpdate(UpdateOp op) {
358                switch (op.cmd) {
359                    case UpdateOp.ADD:
360                        mLayout.onItemsAdded(RecyclerView.this, op.positionStart, op.itemCount);
361                        break;
362                    case UpdateOp.REMOVE:
363                        mLayout.onItemsRemoved(RecyclerView.this, op.positionStart, op.itemCount);
364                        break;
365                    case UpdateOp.UPDATE:
366                        mLayout.onItemsUpdated(RecyclerView.this, op.positionStart, op.itemCount);
367                        break;
368                    case UpdateOp.MOVE:
369                        mLayout.onItemsMoved(RecyclerView.this, op.positionStart, op.itemCount, 1);
370                        break;
371                }
372            }
373
374            @Override
375            public void onDispatchSecondPass(UpdateOp op) {
376                dispatchUpdate(op);
377            }
378
379            @Override
380            public void offsetPositionsForAdd(int positionStart, int itemCount) {
381                offsetPositionRecordsForInsert(positionStart, itemCount);
382                mItemsAddedOrRemoved = true;
383            }
384
385            @Override
386            public void offsetPositionsForMove(int from, int to) {
387                offsetPositionRecordsForMove(from, to);
388                // should we create mItemsMoved ?
389                mItemsAddedOrRemoved = true;
390            }
391        });
392    }
393
394    /**
395     * RecyclerView can perform several optimizations if it can know in advance that changes in
396     * adapter content cannot change the size of the RecyclerView itself.
397     * If your use of RecyclerView falls into this category, set this to true.
398     *
399     * @param hasFixedSize true if adapter changes cannot affect the size of the RecyclerView.
400     */
401    public void setHasFixedSize(boolean hasFixedSize) {
402        mHasFixedSize = hasFixedSize;
403    }
404
405    /**
406     * @return true if the app has specified that changes in adapter content cannot change
407     * the size of the RecyclerView itself.
408     */
409    public boolean hasFixedSize() {
410        return mHasFixedSize;
411    }
412
413    @Override
414    public void setClipToPadding(boolean clipToPadding) {
415        if (clipToPadding != mClipToPadding) {
416            invalidateGlows();
417        }
418        mClipToPadding = clipToPadding;
419        super.setClipToPadding(clipToPadding);
420        if (mFirstLayoutComplete) {
421            requestLayout();
422        }
423    }
424
425    /**
426     * Swaps the current adapter with the provided one. It is similar to
427     * {@link #setAdapter(Adapter)} but assumes existing adapter and the new adapter uses the same
428     * {@link ViewHolder} and does not clear the RecycledViewPool.
429     * <p>
430     * Note that it still calls onAdapterChanged callbacks.
431     *
432     * @param adapter The new adapter to set, or null to set no adapter.
433     * @param removeAndRecycleExistingViews If set to true, RecyclerView will recycle all existing
434     *                                      Views. If adapters have stable ids and/or you want to
435     *                                      animate the disappearing views, you may prefer to set
436     *                                      this to false.
437     * @see #setAdapter(Adapter)
438     */
439    public void swapAdapter(Adapter adapter, boolean removeAndRecycleExistingViews) {
440        setAdapterInternal(adapter, true, removeAndRecycleExistingViews);
441        mDataSetHasChangedAfterLayout = true;
442        requestLayout();
443    }
444    /**
445     * Set a new adapter to provide child views on demand.
446     * <p>
447     * When adapter is changed, all existing views are recycled back to the pool. If the pool has
448     * only one adapter, it will be cleared.
449     *
450     * @param adapter The new adapter to set, or null to set no adapter.
451     * @see #swapAdapter(Adapter, boolean)
452     */
453    public void setAdapter(Adapter adapter) {
454        setAdapterInternal(adapter, false, true);
455        requestLayout();
456    }
457
458    /**
459     * Replaces the current adapter with the new one and triggers listeners.
460     * @param adapter The new adapter
461     * @param compatibleWithPrevious If true, the new adapter is using the same View Holders and
462     *                               item types with the current adapter (helps us avoid cache
463     *                               invalidation).
464     * @param removeAndRecycleViews  If true, we'll remove and recycle all existing views. If
465     *                               compatibleWithPrevious is false, this parameter is ignored.
466     */
467    private void setAdapterInternal(Adapter adapter, boolean compatibleWithPrevious,
468            boolean removeAndRecycleViews) {
469        if (mAdapter != null) {
470            mAdapter.unregisterAdapterDataObserver(mObserver);
471        }
472        if (!compatibleWithPrevious || removeAndRecycleViews) {
473            // end all running animations
474            if (mItemAnimator != null) {
475                mItemAnimator.endAnimations();
476            }
477            // Since animations are ended, mLayout.children should be equal to
478            // recyclerView.children. This may not be true if item animator's end does not work as
479            // expected. (e.g. not release children instantly). It is safer to use mLayout's child
480            // count.
481            if (mLayout != null) {
482                mLayout.removeAndRecycleAllViews(mRecycler);
483                mLayout.removeAndRecycleScrapInt(mRecycler, true);
484            }
485        }
486        mAdapterHelper.reset();
487        final Adapter oldAdapter = mAdapter;
488        mAdapter = adapter;
489        if (adapter != null) {
490            adapter.registerAdapterDataObserver(mObserver);
491        }
492        if (mLayout != null) {
493            mLayout.onAdapterChanged(oldAdapter, mAdapter);
494        }
495        mRecycler.onAdapterChanged(oldAdapter, mAdapter, compatibleWithPrevious);
496        mState.mStructureChanged = true;
497        markKnownViewsInvalid();
498    }
499
500    /**
501     * Retrieves the previously set adapter or null if no adapter is set.
502     *
503     * @return The previously set adapter
504     * @see #setAdapter(Adapter)
505     */
506    public Adapter getAdapter() {
507        return mAdapter;
508    }
509
510    /**
511     * Register a listener that will be notified whenever a child view is recycled.
512     *
513     * <p>This listener will be called when a LayoutManager or the RecyclerView decides
514     * that a child view is no longer needed. If an application associates expensive
515     * or heavyweight data with item views, this may be a good place to release
516     * or free those resources.</p>
517     *
518     * @param listener Listener to register, or null to clear
519     */
520    public void setRecyclerListener(RecyclerListener listener) {
521        mRecyclerListener = listener;
522    }
523
524    /**
525     * Set the {@link LayoutManager} that this RecyclerView will use.
526     *
527     * <p>In contrast to other adapter-backed views such as {@link android.widget.ListView}
528     * or {@link android.widget.GridView}, RecyclerView allows client code to provide custom
529     * layout arrangements for child views. These arrangements are controlled by the
530     * {@link LayoutManager}. A LayoutManager must be provided for RecyclerView to function.</p>
531     *
532     * <p>Several default strategies are provided for common uses such as lists and grids.</p>
533     *
534     * @param layout LayoutManager to use
535     */
536    public void setLayoutManager(LayoutManager layout) {
537        if (layout == mLayout) {
538            return;
539        }
540        // TODO We should do this switch a dispachLayout pass and animate children. There is a good
541        // chance that LayoutManagers will re-use views.
542        if (mLayout != null) {
543            if (mIsAttached) {
544                mLayout.onDetachedFromWindow(this, mRecycler);
545            }
546            mLayout.setRecyclerView(null);
547        }
548        mRecycler.clear();
549        mChildHelper.removeAllViewsUnfiltered();
550        mLayout = layout;
551        if (layout != null) {
552            if (layout.mRecyclerView != null) {
553                throw new IllegalArgumentException("LayoutManager " + layout +
554                        " is already attached to a RecyclerView: " + layout.mRecyclerView);
555            }
556            mLayout.setRecyclerView(this);
557            if (mIsAttached) {
558                mLayout.onAttachedToWindow(this);
559            }
560        }
561        requestLayout();
562    }
563
564    @Override
565    protected Parcelable onSaveInstanceState() {
566        SavedState state = new SavedState(super.onSaveInstanceState());
567        if (mPendingSavedState != null) {
568            state.copyFrom(mPendingSavedState);
569        } else if (mLayout != null) {
570            state.mLayoutState = mLayout.onSaveInstanceState();
571        } else {
572            state.mLayoutState = null;
573        }
574
575        return state;
576    }
577
578    @Override
579    protected void onRestoreInstanceState(Parcelable state) {
580        mPendingSavedState = (SavedState) state;
581        super.onRestoreInstanceState(mPendingSavedState.getSuperState());
582        if (mLayout != null && mPendingSavedState.mLayoutState != null) {
583            mLayout.onRestoreInstanceState(mPendingSavedState.mLayoutState);
584        }
585    }
586
587    /**
588     * Adds a view to the animatingViews list.
589     * mAnimatingViews holds the child views that are currently being kept around
590     * purely for the purpose of being animated out of view. They are drawn as a regular
591     * part of the child list of the RecyclerView, but they are invisible to the LayoutManager
592     * as they are managed separately from the regular child views.
593     * @param view The view to be removed
594     */
595    private void addAnimatingView(View view) {
596        final boolean alreadyParented = view.getParent() == this;
597        mRecycler.unscrapView(getChildViewHolder(view));
598        if (!alreadyParented) {
599            mChildHelper.addView(view, true);
600        } else {
601            mChildHelper.hide(view);
602        }
603    }
604
605    /**
606     * Removes a view from the animatingViews list.
607     * @param view The view to be removed
608     * @see #addAnimatingView(View)
609     */
610    private void removeAnimatingView(View view) {
611        eatRequestLayout();
612        if (mChildHelper.removeViewIfHidden(view)) {
613            final ViewHolder viewHolder = getChildViewHolderInt(view);
614            mRecycler.unscrapView(viewHolder);
615            mRecycler.recycleViewHolderInternal(viewHolder);
616            if (DEBUG) {
617                Log.d(TAG, "after removing animated view: " + view + ", " + this);
618            }
619        }
620        resumeRequestLayout(false);
621    }
622
623    /**
624     * Return the {@link LayoutManager} currently responsible for
625     * layout policy for this RecyclerView.
626     *
627     * @return The currently bound LayoutManager
628     */
629    public LayoutManager getLayoutManager() {
630        return mLayout;
631    }
632
633    /**
634     * Retrieve this RecyclerView's {@link RecycledViewPool}. This method will never return null;
635     * if no pool is set for this view a new one will be created. See
636     * {@link #setRecycledViewPool(RecycledViewPool) setRecycledViewPool} for more information.
637     *
638     * @return The pool used to store recycled item views for reuse.
639     * @see #setRecycledViewPool(RecycledViewPool)
640     */
641    public RecycledViewPool getRecycledViewPool() {
642        return mRecycler.getRecycledViewPool();
643    }
644
645    /**
646     * Recycled view pools allow multiple RecyclerViews to share a common pool of scrap views.
647     * This can be useful if you have multiple RecyclerViews with adapters that use the same
648     * view types, for example if you have several data sets with the same kinds of item views
649     * displayed by a {@link android.support.v4.view.ViewPager ViewPager}.
650     *
651     * @param pool Pool to set. If this parameter is null a new pool will be created and used.
652     */
653    public void setRecycledViewPool(RecycledViewPool pool) {
654        mRecycler.setRecycledViewPool(pool);
655    }
656
657    /**
658     * Sets a new {@link ViewCacheExtension} to be used by the Recycler.
659     *
660     * @param extension ViewCacheExtension to be used or null if you want to clear the existing one.
661     *
662     * @see {@link ViewCacheExtension#getViewForPositionAndType(Recycler, int, int)}
663     */
664    public void setViewCacheExtension(ViewCacheExtension extension) {
665        mRecycler.setViewCacheExtension(extension);
666    }
667
668    /**
669     * Set the number of offscreen views to retain before adding them to the potentially shared
670     * {@link #getRecycledViewPool() recycled view pool}.
671     *
672     * <p>The offscreen view cache stays aware of changes in the attached adapter, allowing
673     * a LayoutManager to reuse those views unmodified without needing to return to the adapter
674     * to rebind them.</p>
675     *
676     * @param size Number of views to cache offscreen before returning them to the general
677     *             recycled view pool
678     */
679    public void setItemViewCacheSize(int size) {
680        mRecycler.setViewCacheSize(size);
681    }
682
683    /**
684     * Return the current scrolling state of the RecyclerView.
685     *
686     * @return {@link #SCROLL_STATE_IDLE}, {@link #SCROLL_STATE_DRAGGING} or
687     * {@link #SCROLL_STATE_SETTLING}
688     */
689    public int getScrollState() {
690        return mScrollState;
691    }
692
693    private void setScrollState(int state) {
694        if (state == mScrollState) {
695            return;
696        }
697        mScrollState = state;
698        if (state != SCROLL_STATE_SETTLING) {
699            stopScroll();
700        }
701        if (mScrollListener != null) {
702            mScrollListener.onScrollStateChanged(this, state);
703        }
704        mLayout.onScrollStateChanged(state);
705    }
706
707    /**
708     * Add an {@link ItemDecoration} to this RecyclerView. Item decorations can
709     * affect both measurement and drawing of individual item views.
710     *
711     * <p>Item decorations are ordered. Decorations placed earlier in the list will
712     * be run/queried/drawn first for their effects on item views. Padding added to views
713     * will be nested; a padding added by an earlier decoration will mean further
714     * item decorations in the list will be asked to draw/pad within the previous decoration's
715     * given area.</p>
716     *
717     * @param decor Decoration to add
718     * @param index Position in the decoration chain to insert this decoration at. If this value
719     *              is negative the decoration will be added at the end.
720     */
721    public void addItemDecoration(ItemDecoration decor, int index) {
722        if (mLayout != null) {
723            mLayout.assertNotInLayoutOrScroll("Cannot add item decoration during a scroll  or"
724                    + " layout");
725        }
726        if (mItemDecorations.isEmpty()) {
727            setWillNotDraw(false);
728        }
729        if (index < 0) {
730            mItemDecorations.add(decor);
731        } else {
732            mItemDecorations.add(index, decor);
733        }
734        markItemDecorInsetsDirty();
735        requestLayout();
736    }
737
738    /**
739     * Add an {@link ItemDecoration} to this RecyclerView. Item decorations can
740     * affect both measurement and drawing of individual item views.
741     *
742     * <p>Item decorations are ordered. Decorations placed earlier in the list will
743     * be run/queried/drawn first for their effects on item views. Padding added to views
744     * will be nested; a padding added by an earlier decoration will mean further
745     * item decorations in the list will be asked to draw/pad within the previous decoration's
746     * given area.</p>
747     *
748     * @param decor Decoration to add
749     */
750    public void addItemDecoration(ItemDecoration decor) {
751        addItemDecoration(decor, -1);
752    }
753
754    /**
755     * Remove an {@link ItemDecoration} from this RecyclerView.
756     *
757     * <p>The given decoration will no longer impact the measurement and drawing of
758     * item views.</p>
759     *
760     * @param decor Decoration to remove
761     * @see #addItemDecoration(ItemDecoration)
762     */
763    public void removeItemDecoration(ItemDecoration decor) {
764        if (mLayout != null) {
765            mLayout.assertNotInLayoutOrScroll("Cannot remove item decoration during a scroll  or"
766                    + " layout");
767        }
768        mItemDecorations.remove(decor);
769        if (mItemDecorations.isEmpty()) {
770            setWillNotDraw(ViewCompat.getOverScrollMode(this) == ViewCompat.OVER_SCROLL_NEVER);
771        }
772        markItemDecorInsetsDirty();
773        requestLayout();
774    }
775
776    /**
777     * Set a listener that will be notified of any changes in scroll state or position.
778     *
779     * @param listener Listener to set or null to clear
780     */
781    public void setOnScrollListener(OnScrollListener listener) {
782        mScrollListener = listener;
783    }
784
785    /**
786     * Convenience method to scroll to a certain position.
787     *
788     * RecyclerView does not implement scrolling logic, rather forwards the call to
789     * {@link android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int)}
790     * @param position Scroll to this adapter position
791     * @see android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int)
792     */
793    public void scrollToPosition(int position) {
794        stopScroll();
795        mLayout.scrollToPosition(position);
796        awakenScrollBars();
797    }
798
799    /**
800     * Starts a smooth scroll to an adapter position.
801     * <p>
802     * To support smooth scrolling, you must override
803     * {@link LayoutManager#smoothScrollToPosition(RecyclerView, State, int)} and create a
804     * {@link SmoothScroller}.
805     * <p>
806     * {@link LayoutManager} is responsible for creating the actual scroll action. If you want to
807     * provide a custom smooth scroll logic, override
808     * {@link LayoutManager#smoothScrollToPosition(RecyclerView, State, int)} in your
809     * LayoutManager.
810     *
811     * @param position The adapter position to scroll to
812     * @see LayoutManager#smoothScrollToPosition(RecyclerView, State, int)
813     */
814    public void smoothScrollToPosition(int position) {
815        mLayout.smoothScrollToPosition(this, mState, position);
816    }
817
818    @Override
819    public void scrollTo(int x, int y) {
820        throw new UnsupportedOperationException(
821                "RecyclerView does not support scrolling to an absolute position.");
822    }
823
824    @Override
825    public void scrollBy(int x, int y) {
826        if (mLayout == null) {
827            throw new IllegalStateException("Cannot scroll without a LayoutManager set. " +
828                    "Call setLayoutManager with a non-null argument.");
829        }
830        final boolean canScrollHorizontal = mLayout.canScrollHorizontally();
831        final boolean canScrollVertical = mLayout.canScrollVertically();
832        if (canScrollHorizontal || canScrollVertical) {
833            scrollByInternal(canScrollHorizontal ? x : 0, canScrollVertical ? y : 0);
834        }
835    }
836
837    /**
838     * Helper method reflect data changes to the state.
839     * <p>
840     * Adapter changes during a scroll may trigger a crash because scroll assumes no data change
841     * but data actually changed.
842     * <p>
843     * This method consumes all deferred changes to avoid that case.
844     */
845    private void consumePendingUpdateOperations() {
846        if (mAdapterHelper.hasPendingUpdates()) {
847            mUpdateChildViewsRunnable.run();
848        }
849    }
850
851    /**
852     * Does not perform bounds checking. Used by internal methods that have already validated input.
853     */
854    void scrollByInternal(int x, int y) {
855        int overscrollX = 0, overscrollY = 0;
856        int hresult = 0, vresult = 0;
857        consumePendingUpdateOperations();
858        if (mAdapter != null) {
859            eatRequestLayout();
860            mRunningLayoutOrScroll = true;
861            if (x != 0) {
862                hresult = mLayout.scrollHorizontallyBy(x, mRecycler, mState);
863                overscrollX = x - hresult;
864            }
865            if (y != 0) {
866                vresult = mLayout.scrollVerticallyBy(y, mRecycler, mState);
867                overscrollY = y - vresult;
868            }
869            if (supportsChangeAnimations()) {
870                // Fix up shadow views used by changing animations
871                int count = mChildHelper.getChildCount();
872                for (int i = 0; i < count; i++) {
873                    View view = mChildHelper.getChildAt(i);
874                    ViewHolder holder = getChildViewHolder(view);
875                    if (holder != null && holder.mShadowingHolder != null) {
876                        ViewHolder shadowingHolder = holder.mShadowingHolder;
877                        View shadowingView = shadowingHolder != null ? shadowingHolder.itemView : null;
878                        if (shadowingView != null) {
879                            int left = view.getLeft();
880                            int top = view.getTop();
881                            if (left != shadowingView.getLeft() || top != shadowingView.getTop()) {
882                                shadowingView.layout(left, top,
883                                        left + shadowingView.getWidth(),
884                                        top + shadowingView.getHeight());
885                            }
886                        }
887                    }
888                }
889            }
890            mRunningLayoutOrScroll = false;
891            resumeRequestLayout(false);
892        }
893        if (!mItemDecorations.isEmpty()) {
894            invalidate();
895        }
896        if (ViewCompat.getOverScrollMode(this) != ViewCompat.OVER_SCROLL_NEVER) {
897            considerReleasingGlowsOnScroll(x, y);
898            pullGlows(overscrollX, overscrollY);
899        }
900        if (mScrollListener != null && (hresult != 0 || vresult != 0)) {
901            mScrollListener.onScrolled(this, hresult, vresult);
902        }
903        if (!awakenScrollBars()) {
904            invalidate();
905        }
906    }
907
908    /**
909     * <p>Compute the horizontal offset of the horizontal scrollbar's thumb within the horizontal
910     * range. This value is used to compute the length of the thumb within the scrollbar's track.
911     * </p>
912     *
913     * <p>The range is expressed in arbitrary units that must be the same as the units used by
914     * {@link #computeHorizontalScrollRange()} and {@link #computeHorizontalScrollExtent()}.</p>
915     *
916     * <p>Default implementation returns 0.</p>
917     *
918     * <p>If you want to support scroll bars, override
919     * {@link RecyclerView.LayoutManager#computeHorizontalScrollOffset(RecyclerView.State)} in your
920     * LayoutManager. </p>
921     *
922     * @return The horizontal offset of the scrollbar's thumb
923     * @see android.support.v7.widget.RecyclerView.LayoutManager#computeHorizontalScrollOffset
924     * (RecyclerView.Adapter)
925     */
926    @Override
927    protected int computeHorizontalScrollOffset() {
928        return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollOffset(mState)
929                : 0;
930    }
931
932    /**
933     * <p>Compute the horizontal extent of the horizontal scrollbar's thumb within the
934     * horizontal range. This value is used to compute the length of the thumb within the
935     * scrollbar's track.</p>
936     *
937     * <p>The range is expressed in arbitrary units that must be the same as the units used by
938     * {@link #computeHorizontalScrollRange()} and {@link #computeHorizontalScrollOffset()}.</p>
939     *
940     * <p>Default implementation returns 0.</p>
941     *
942     * <p>If you want to support scroll bars, override
943     * {@link RecyclerView.LayoutManager#computeHorizontalScrollExtent(RecyclerView.State)} in your
944     * LayoutManager.</p>
945     *
946     * @return The horizontal extent of the scrollbar's thumb
947     * @see RecyclerView.LayoutManager#computeHorizontalScrollExtent(RecyclerView.State)
948     */
949    @Override
950    protected int computeHorizontalScrollExtent() {
951        return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollExtent(mState) : 0;
952    }
953
954    /**
955     * <p>Compute the horizontal range that the horizontal scrollbar represents.</p>
956     *
957     * <p>The range is expressed in arbitrary units that must be the same as the units used by
958     * {@link #computeHorizontalScrollExtent()} and {@link #computeHorizontalScrollOffset()}.</p>
959     *
960     * <p>Default implementation returns 0.</p>
961     *
962     * <p>If you want to support scroll bars, override
963     * {@link RecyclerView.LayoutManager#computeHorizontalScrollRange(RecyclerView.State)} in your
964     * LayoutManager.</p>
965     *
966     * @return The total horizontal range represented by the vertical scrollbar
967     * @see RecyclerView.LayoutManager#computeHorizontalScrollRange(RecyclerView.State)
968     */
969    @Override
970    protected int computeHorizontalScrollRange() {
971        return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollRange(mState) : 0;
972    }
973
974    /**
975     * <p>Compute the vertical offset of the vertical scrollbar's thumb within the vertical range.
976     * This value is used to compute the length of the thumb within the scrollbar's track. </p>
977     *
978     * <p>The range is expressed in arbitrary units that must be the same as the units used by
979     * {@link #computeVerticalScrollRange()} and {@link #computeVerticalScrollExtent()}.</p>
980     *
981     * <p>Default implementation returns 0.</p>
982     *
983     * <p>If you want to support scroll bars, override
984     * {@link RecyclerView.LayoutManager#computeVerticalScrollOffset(RecyclerView.State)} in your
985     * LayoutManager.</p>
986     *
987     * @return The vertical offset of the scrollbar's thumb
988     * @see android.support.v7.widget.RecyclerView.LayoutManager#computeVerticalScrollOffset
989     * (RecyclerView.Adapter)
990     */
991    @Override
992    protected int computeVerticalScrollOffset() {
993        return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollOffset(mState) : 0;
994    }
995
996    /**
997     * <p>Compute the vertical extent of the vertical scrollbar's thumb within the vertical range.
998     * This value is used to compute the length of the thumb within the scrollbar's track.</p>
999     *
1000     * <p>The range is expressed in arbitrary units that must be the same as the units used by
1001     * {@link #computeVerticalScrollRange()} and {@link #computeVerticalScrollOffset()}.</p>
1002     *
1003     * <p>Default implementation returns 0.</p>
1004     *
1005     * <p>If you want to support scroll bars, override
1006     * {@link RecyclerView.LayoutManager#computeVerticalScrollExtent(RecyclerView.State)} in your
1007     * LayoutManager.</p>
1008     *
1009     * @return The vertical extent of the scrollbar's thumb
1010     * @see RecyclerView.LayoutManager#computeVerticalScrollExtent(RecyclerView.State)
1011     */
1012    @Override
1013    protected int computeVerticalScrollExtent() {
1014        return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollExtent(mState) : 0;
1015    }
1016
1017    /**
1018     * <p>Compute the vertical range that the vertical scrollbar represents.</p>
1019     *
1020     * <p>The range is expressed in arbitrary units that must be the same as the units used by
1021     * {@link #computeVerticalScrollExtent()} and {@link #computeVerticalScrollOffset()}.</p>
1022     *
1023     * <p>Default implementation returns 0.</p>
1024     *
1025     * <p>If you want to support scroll bars, override
1026     * {@link RecyclerView.LayoutManager#computeVerticalScrollRange(RecyclerView.State)} in your
1027     * LayoutManager.</p>
1028     *
1029     * @return The total vertical range represented by the vertical scrollbar
1030     * @see RecyclerView.LayoutManager#computeVerticalScrollRange(RecyclerView.State)
1031     */
1032    @Override
1033    protected int computeVerticalScrollRange() {
1034        return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollRange(mState) : 0;
1035    }
1036
1037
1038    void eatRequestLayout() {
1039        if (!mEatRequestLayout) {
1040            mEatRequestLayout = true;
1041            mLayoutRequestEaten = false;
1042        }
1043    }
1044
1045    void resumeRequestLayout(boolean performLayoutChildren) {
1046        if (mEatRequestLayout) {
1047            if (performLayoutChildren && mLayoutRequestEaten &&
1048                    mLayout != null && mAdapter != null) {
1049                dispatchLayout();
1050            }
1051            mEatRequestLayout = false;
1052            mLayoutRequestEaten = false;
1053        }
1054    }
1055
1056    /**
1057     * Animate a scroll by the given amount of pixels along either axis.
1058     *
1059     * @param dx Pixels to scroll horizontally
1060     * @param dy Pixels to scroll vertically
1061     */
1062    public void smoothScrollBy(int dx, int dy) {
1063        if (dx != 0 || dy != 0) {
1064            mViewFlinger.smoothScrollBy(dx, dy);
1065        }
1066    }
1067
1068    /**
1069     * Begin a standard fling with an initial velocity along each axis in pixels per second.
1070     * If the velocity given is below the system-defined minimum this method will return false
1071     * and no fling will occur.
1072     *
1073     * @param velocityX Initial horizontal velocity in pixels per second
1074     * @param velocityY Initial vertical velocity in pixels per second
1075     * @return true if the fling was started, false if the velocity was too low to fling
1076     */
1077    public boolean fling(int velocityX, int velocityY) {
1078        if (Math.abs(velocityX) < mMinFlingVelocity) {
1079            velocityX = 0;
1080        }
1081        if (Math.abs(velocityY) < mMinFlingVelocity) {
1082            velocityY = 0;
1083        }
1084        velocityX = Math.max(-mMaxFlingVelocity, Math.min(velocityX, mMaxFlingVelocity));
1085        velocityY = Math.max(-mMaxFlingVelocity, Math.min(velocityY, mMaxFlingVelocity));
1086        if (velocityX != 0 || velocityY != 0) {
1087            mViewFlinger.fling(velocityX, velocityY);
1088            return true;
1089        }
1090        return false;
1091    }
1092
1093    /**
1094     * Stop any current scroll in progress, such as one started by
1095     * {@link #smoothScrollBy(int, int)}, {@link #fling(int, int)} or a touch-initiated fling.
1096     */
1097    public void stopScroll() {
1098        mViewFlinger.stop();
1099        mLayout.stopSmoothScroller();
1100    }
1101
1102    /**
1103     * Apply a pull to relevant overscroll glow effects
1104     */
1105    private void pullGlows(int overscrollX, int overscrollY) {
1106        if (overscrollX < 0) {
1107            ensureLeftGlow();
1108            mLeftGlow.onPull(-overscrollX / (float) getWidth());
1109        } else if (overscrollX > 0) {
1110            ensureRightGlow();
1111            mRightGlow.onPull(overscrollX / (float) getWidth());
1112        }
1113
1114        if (overscrollY < 0) {
1115            ensureTopGlow();
1116            mTopGlow.onPull(-overscrollY / (float) getHeight());
1117        } else if (overscrollY > 0) {
1118            ensureBottomGlow();
1119            mBottomGlow.onPull(overscrollY / (float) getHeight());
1120        }
1121
1122        if (overscrollX != 0 || overscrollY != 0) {
1123            ViewCompat.postInvalidateOnAnimation(this);
1124        }
1125    }
1126
1127    private void releaseGlows() {
1128        boolean needsInvalidate = false;
1129        if (mLeftGlow != null) needsInvalidate = mLeftGlow.onRelease();
1130        if (mTopGlow != null) needsInvalidate |= mTopGlow.onRelease();
1131        if (mRightGlow != null) needsInvalidate |= mRightGlow.onRelease();
1132        if (mBottomGlow != null) needsInvalidate |= mBottomGlow.onRelease();
1133        if (needsInvalidate) {
1134            ViewCompat.postInvalidateOnAnimation(this);
1135        }
1136    }
1137
1138    private void considerReleasingGlowsOnScroll(int dx, int dy) {
1139        boolean needsInvalidate = false;
1140        if (mLeftGlow != null && !mLeftGlow.isFinished() && dx > 0) {
1141            needsInvalidate = mLeftGlow.onRelease();
1142        }
1143        if (mRightGlow != null && !mRightGlow.isFinished() && dx < 0) {
1144            needsInvalidate |= mRightGlow.onRelease();
1145        }
1146        if (mTopGlow != null && !mTopGlow.isFinished() && dy > 0) {
1147            needsInvalidate |= mTopGlow.onRelease();
1148        }
1149        if (mBottomGlow != null && !mBottomGlow.isFinished() && dy < 0) {
1150            needsInvalidate |= mBottomGlow.onRelease();
1151        }
1152        if (needsInvalidate) {
1153            ViewCompat.postInvalidateOnAnimation(this);
1154        }
1155    }
1156
1157    void absorbGlows(int velocityX, int velocityY) {
1158        if (velocityX < 0) {
1159            ensureLeftGlow();
1160            mLeftGlow.onAbsorb(-velocityX);
1161        } else if (velocityX > 0) {
1162            ensureRightGlow();
1163            mRightGlow.onAbsorb(velocityX);
1164        }
1165
1166        if (velocityY < 0) {
1167            ensureTopGlow();
1168            mTopGlow.onAbsorb(-velocityY);
1169        } else if (velocityY > 0) {
1170            ensureBottomGlow();
1171            mBottomGlow.onAbsorb(velocityY);
1172        }
1173
1174        if (velocityX != 0 || velocityY != 0) {
1175            ViewCompat.postInvalidateOnAnimation(this);
1176        }
1177    }
1178
1179    void ensureLeftGlow() {
1180        if (mLeftGlow != null) {
1181            return;
1182        }
1183        mLeftGlow = new EdgeEffectCompat(getContext());
1184        if (mClipToPadding) {
1185            mLeftGlow.setSize(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(),
1186                    getMeasuredWidth() - getPaddingLeft() - getPaddingRight());
1187        } else {
1188            mLeftGlow.setSize(getMeasuredHeight(), getMeasuredWidth());
1189        }
1190    }
1191
1192    void ensureRightGlow() {
1193        if (mRightGlow != null) {
1194            return;
1195        }
1196        mRightGlow = new EdgeEffectCompat(getContext());
1197        if (mClipToPadding) {
1198            mRightGlow.setSize(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(),
1199                    getMeasuredWidth() - getPaddingLeft() - getPaddingRight());
1200        } else {
1201            mRightGlow.setSize(getMeasuredHeight(), getMeasuredWidth());
1202        }
1203    }
1204
1205    void ensureTopGlow() {
1206        if (mTopGlow != null) {
1207            return;
1208        }
1209        mTopGlow = new EdgeEffectCompat(getContext());
1210        if (mClipToPadding) {
1211            mTopGlow.setSize(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
1212                    getMeasuredHeight() - getPaddingTop() - getPaddingBottom());
1213        } else {
1214            mTopGlow.setSize(getMeasuredWidth(), getMeasuredHeight());
1215        }
1216
1217    }
1218
1219    void ensureBottomGlow() {
1220        if (mBottomGlow != null) {
1221            return;
1222        }
1223        mBottomGlow = new EdgeEffectCompat(getContext());
1224        if (mClipToPadding) {
1225            mBottomGlow.setSize(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
1226                    getMeasuredHeight() - getPaddingTop() - getPaddingBottom());
1227        } else {
1228            mBottomGlow.setSize(getMeasuredWidth(), getMeasuredHeight());
1229        }
1230    }
1231
1232    void invalidateGlows() {
1233        mLeftGlow = mRightGlow = mTopGlow = mBottomGlow = null;
1234    }
1235
1236    // Focus handling
1237
1238    @Override
1239    public View focusSearch(View focused, int direction) {
1240        View result = mLayout.onInterceptFocusSearch(focused, direction);
1241        if (result != null) {
1242            return result;
1243        }
1244        final FocusFinder ff = FocusFinder.getInstance();
1245        result = ff.findNextFocus(this, focused, direction);
1246        if (result == null && mAdapter != null) {
1247            eatRequestLayout();
1248            result = mLayout.onFocusSearchFailed(focused, direction, mRecycler, mState);
1249            resumeRequestLayout(false);
1250        }
1251        return result != null ? result : super.focusSearch(focused, direction);
1252    }
1253
1254    @Override
1255    public void requestChildFocus(View child, View focused) {
1256        if (!mLayout.onRequestChildFocus(this, mState, child, focused) && focused != null) {
1257            mTempRect.set(0, 0, focused.getWidth(), focused.getHeight());
1258            offsetDescendantRectToMyCoords(focused, mTempRect);
1259            offsetRectIntoDescendantCoords(child, mTempRect);
1260            requestChildRectangleOnScreen(child, mTempRect, !mFirstLayoutComplete);
1261        }
1262        super.requestChildFocus(child, focused);
1263    }
1264
1265    @Override
1266    public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
1267        return mLayout.requestChildRectangleOnScreen(this, child, rect, immediate);
1268    }
1269
1270    @Override
1271    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
1272        if (!mLayout.onAddFocusables(this, views, direction, focusableMode)) {
1273            super.addFocusables(views, direction, focusableMode);
1274        }
1275    }
1276
1277    @Override
1278    protected void onAttachedToWindow() {
1279        super.onAttachedToWindow();
1280        mIsAttached = true;
1281        mFirstLayoutComplete = false;
1282        if (mLayout != null) {
1283            mLayout.onAttachedToWindow(this);
1284        }
1285        mPostedAnimatorRunner = false;
1286    }
1287
1288    @Override
1289    protected void onDetachedFromWindow() {
1290        super.onDetachedFromWindow();
1291        mFirstLayoutComplete = false;
1292
1293        stopScroll();
1294        mIsAttached = false;
1295        if (mLayout != null) {
1296            mLayout.onDetachedFromWindow(this, mRecycler);
1297        }
1298        removeCallbacks(mItemAnimatorRunner);
1299    }
1300
1301    /**
1302     * Checks if RecyclerView is in the middle of a layout or scroll and throws an
1303     * {@link IllegalStateException} if it <b>is not</b>.
1304     *
1305     * @param message The message for the exception. Can be null.
1306     * @see #assertNotInLayoutOrScroll(String)
1307     */
1308    void assertInLayoutOrScroll(String message) {
1309        if (!mRunningLayoutOrScroll) {
1310            if (message == null) {
1311                throw new IllegalStateException("Cannot call this method unless RecyclerView is "
1312                        + "computing a layout or scrolling");
1313            }
1314            throw new IllegalStateException(message);
1315
1316        }
1317    }
1318
1319    /**
1320     * Checks if RecyclerView is in the middle of a layout or scroll and throws an
1321     * {@link IllegalStateException} if it <b>is</b>.
1322     *
1323     * @param message The message for the exception. Can be null.
1324     * @see #assertInLayoutOrScroll(String)
1325     */
1326    void assertNotInLayoutOrScroll(String message) {
1327        if (mRunningLayoutOrScroll) {
1328            if (message == null) {
1329                throw new IllegalStateException("Cannot call this method while RecyclerView is "
1330                        + "computing a layout or scrolling");
1331            }
1332            throw new IllegalStateException(message);
1333        }
1334    }
1335
1336    /**
1337     * Add an {@link OnItemTouchListener} to intercept touch events before they are dispatched
1338     * to child views or this view's standard scrolling behavior.
1339     *
1340     * <p>Client code may use listeners to implement item manipulation behavior. Once a listener
1341     * returns true from
1342     * {@link OnItemTouchListener#onInterceptTouchEvent(RecyclerView, MotionEvent)} its
1343     * {@link OnItemTouchListener#onTouchEvent(RecyclerView, MotionEvent)} method will be called
1344     * for each incoming MotionEvent until the end of the gesture.</p>
1345     *
1346     * @param listener Listener to add
1347     */
1348    public void addOnItemTouchListener(OnItemTouchListener listener) {
1349        mOnItemTouchListeners.add(listener);
1350    }
1351
1352    /**
1353     * Remove an {@link OnItemTouchListener}. It will no longer be able to intercept touch events.
1354     *
1355     * @param listener Listener to remove
1356     */
1357    public void removeOnItemTouchListener(OnItemTouchListener listener) {
1358        mOnItemTouchListeners.remove(listener);
1359        if (mActiveOnItemTouchListener == listener) {
1360            mActiveOnItemTouchListener = null;
1361        }
1362    }
1363
1364    private boolean dispatchOnItemTouchIntercept(MotionEvent e) {
1365        final int action = e.getAction();
1366        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_DOWN) {
1367            mActiveOnItemTouchListener = null;
1368        }
1369
1370        final int listenerCount = mOnItemTouchListeners.size();
1371        for (int i = 0; i < listenerCount; i++) {
1372            final OnItemTouchListener listener = mOnItemTouchListeners.get(i);
1373            if (listener.onInterceptTouchEvent(this, e) && action != MotionEvent.ACTION_CANCEL) {
1374                mActiveOnItemTouchListener = listener;
1375                return true;
1376            }
1377        }
1378        return false;
1379    }
1380
1381    private boolean dispatchOnItemTouch(MotionEvent e) {
1382        final int action = e.getAction();
1383        if (mActiveOnItemTouchListener != null) {
1384            if (action == MotionEvent.ACTION_DOWN) {
1385                // Stale state from a previous gesture, we're starting a new one. Clear it.
1386                mActiveOnItemTouchListener = null;
1387            } else {
1388                mActiveOnItemTouchListener.onTouchEvent(this, e);
1389                if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
1390                    // Clean up for the next gesture.
1391                    mActiveOnItemTouchListener = null;
1392                }
1393                return true;
1394            }
1395        }
1396
1397        // Listeners will have already received the ACTION_DOWN via dispatchOnItemTouchIntercept
1398        // as called from onInterceptTouchEvent; skip it.
1399        if (action != MotionEvent.ACTION_DOWN) {
1400            final int listenerCount = mOnItemTouchListeners.size();
1401            for (int i = 0; i < listenerCount; i++) {
1402                final OnItemTouchListener listener = mOnItemTouchListeners.get(i);
1403                if (listener.onInterceptTouchEvent(this, e)) {
1404                    mActiveOnItemTouchListener = listener;
1405                    return true;
1406                }
1407            }
1408        }
1409        return false;
1410    }
1411
1412    @Override
1413    public boolean onInterceptTouchEvent(MotionEvent e) {
1414        if (dispatchOnItemTouchIntercept(e)) {
1415            cancelTouch();
1416            return true;
1417        }
1418
1419        final boolean canScrollHorizontally = mLayout.canScrollHorizontally();
1420        final boolean canScrollVertically = mLayout.canScrollVertically();
1421
1422        if (mVelocityTracker == null) {
1423            mVelocityTracker = VelocityTracker.obtain();
1424        }
1425        mVelocityTracker.addMovement(e);
1426
1427        final int action = MotionEventCompat.getActionMasked(e);
1428        final int actionIndex = MotionEventCompat.getActionIndex(e);
1429
1430        switch (action) {
1431            case MotionEvent.ACTION_DOWN:
1432                mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
1433                mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);
1434                mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);
1435
1436                if (mScrollState == SCROLL_STATE_SETTLING) {
1437                    getParent().requestDisallowInterceptTouchEvent(true);
1438                    setScrollState(SCROLL_STATE_DRAGGING);
1439                }
1440                break;
1441
1442            case MotionEventCompat.ACTION_POINTER_DOWN:
1443                mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
1444                mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
1445                mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
1446                break;
1447
1448            case MotionEvent.ACTION_MOVE: {
1449                final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
1450                if (index < 0) {
1451                    Log.e(TAG, "Error processing scroll; pointer index for id " +
1452                            mScrollPointerId + " not found. Did any MotionEvents get skipped?");
1453                    return false;
1454                }
1455
1456                final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
1457                final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
1458                if (mScrollState != SCROLL_STATE_DRAGGING) {
1459                    final int dx = x - mInitialTouchX;
1460                    final int dy = y - mInitialTouchY;
1461                    boolean startScroll = false;
1462                    if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) {
1463                        mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1);
1464                        startScroll = true;
1465                    }
1466                    if (canScrollVertically && Math.abs(dy) > mTouchSlop) {
1467                        mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1);
1468                        startScroll = true;
1469                    }
1470                    if (startScroll) {
1471                        getParent().requestDisallowInterceptTouchEvent(true);
1472                        setScrollState(SCROLL_STATE_DRAGGING);
1473                    }
1474                }
1475            } break;
1476
1477            case MotionEventCompat.ACTION_POINTER_UP: {
1478                onPointerUp(e);
1479            } break;
1480
1481            case MotionEvent.ACTION_UP: {
1482                mVelocityTracker.clear();
1483            } break;
1484
1485            case MotionEvent.ACTION_CANCEL: {
1486                cancelTouch();
1487            }
1488        }
1489        return mScrollState == SCROLL_STATE_DRAGGING;
1490    }
1491
1492    @Override
1493    public boolean onTouchEvent(MotionEvent e) {
1494        if (dispatchOnItemTouch(e)) {
1495            cancelTouch();
1496            return true;
1497        }
1498
1499        final boolean canScrollHorizontally = mLayout.canScrollHorizontally();
1500        final boolean canScrollVertically = mLayout.canScrollVertically();
1501
1502        if (mVelocityTracker == null) {
1503            mVelocityTracker = VelocityTracker.obtain();
1504        }
1505        mVelocityTracker.addMovement(e);
1506
1507        final int action = MotionEventCompat.getActionMasked(e);
1508        final int actionIndex = MotionEventCompat.getActionIndex(e);
1509
1510        switch (action) {
1511            case MotionEvent.ACTION_DOWN: {
1512                mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
1513                mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);
1514                mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);
1515            } break;
1516
1517            case MotionEventCompat.ACTION_POINTER_DOWN: {
1518                mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
1519                mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
1520                mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
1521            } break;
1522
1523            case MotionEvent.ACTION_MOVE: {
1524                final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
1525                if (index < 0) {
1526                    Log.e(TAG, "Error processing scroll; pointer index for id " +
1527                            mScrollPointerId + " not found. Did any MotionEvents get skipped?");
1528                    return false;
1529                }
1530
1531                final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
1532                final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
1533                if (mScrollState != SCROLL_STATE_DRAGGING) {
1534                    final int dx = x - mInitialTouchX;
1535                    final int dy = y - mInitialTouchY;
1536                    boolean startScroll = false;
1537                    if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) {
1538                        mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1);
1539                        startScroll = true;
1540                    }
1541                    if (canScrollVertically && Math.abs(dy) > mTouchSlop) {
1542                        mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1);
1543                        startScroll = true;
1544                    }
1545                    if (startScroll) {
1546                        getParent().requestDisallowInterceptTouchEvent(true);
1547                        setScrollState(SCROLL_STATE_DRAGGING);
1548                    }
1549                }
1550                if (mScrollState == SCROLL_STATE_DRAGGING) {
1551                    final int dx = x - mLastTouchX;
1552                    final int dy = y - mLastTouchY;
1553                    scrollByInternal(canScrollHorizontally ? -dx : 0,
1554                            canScrollVertically ? -dy : 0);
1555                }
1556                mLastTouchX = x;
1557                mLastTouchY = y;
1558            } break;
1559
1560            case MotionEventCompat.ACTION_POINTER_UP: {
1561                onPointerUp(e);
1562            } break;
1563
1564            case MotionEvent.ACTION_UP: {
1565                mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
1566                final float xvel = canScrollHorizontally ?
1567                        -VelocityTrackerCompat.getXVelocity(mVelocityTracker, mScrollPointerId) : 0;
1568                final float yvel = canScrollVertically ?
1569                        -VelocityTrackerCompat.getYVelocity(mVelocityTracker, mScrollPointerId) : 0;
1570                if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) {
1571                    setScrollState(SCROLL_STATE_IDLE);
1572                }
1573                mVelocityTracker.clear();
1574                releaseGlows();
1575            } break;
1576
1577            case MotionEvent.ACTION_CANCEL: {
1578                cancelTouch();
1579            } break;
1580        }
1581
1582        return true;
1583    }
1584
1585    private void cancelTouch() {
1586        mVelocityTracker.clear();
1587        releaseGlows();
1588        setScrollState(SCROLL_STATE_IDLE);
1589    }
1590
1591    private void onPointerUp(MotionEvent e) {
1592        final int actionIndex = MotionEventCompat.getActionIndex(e);
1593        if (MotionEventCompat.getPointerId(e, actionIndex) == mScrollPointerId) {
1594            // Pick a new pointer to pick up the slack.
1595            final int newIndex = actionIndex == 0 ? 1 : 0;
1596            mScrollPointerId = MotionEventCompat.getPointerId(e, newIndex);
1597            mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, newIndex) + 0.5f);
1598            mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, newIndex) + 0.5f);
1599        }
1600    }
1601
1602    @Override
1603    protected void onMeasure(int widthSpec, int heightSpec) {
1604        if (mAdapterUpdateDuringMeasure) {
1605            eatRequestLayout();
1606            processAdapterUpdatesAndSetAnimationFlags();
1607
1608            if (mState.mRunPredictiveAnimations) {
1609                // TODO: try to provide a better approach.
1610                // When RV decides to run predictive animations, we need to measure in pre-layout
1611                // state so that pre-layout pass results in correct layout.
1612                // On the other hand, this will prevent the layout manager from resizing properly.
1613                mState.mInPreLayout = true;
1614            } else {
1615                // consume remaining updates to provide a consistent state with the layout pass.
1616                mAdapterHelper.consumeUpdatesInOnePass();
1617                mState.mInPreLayout = false;
1618            }
1619            mAdapterUpdateDuringMeasure = false;
1620            resumeRequestLayout(false);
1621        }
1622
1623        if (mAdapter != null) {
1624            mState.mItemCount = mAdapter.getItemCount();
1625        } else {
1626            mState.mItemCount = 0;
1627        }
1628
1629        mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);
1630        mState.mInPreLayout = false; // clear
1631    }
1632
1633    @Override
1634    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1635        super.onSizeChanged(w, h, oldw, oldh);
1636        if (w != oldw || h != oldh) {
1637            invalidateGlows();
1638        }
1639    }
1640
1641    /**
1642     * Sets the {@link ItemAnimator} that will handle animations involving changes
1643     * to the items in this RecyclerView. By default, RecyclerView instantiates and
1644     * uses an instance of {@link DefaultItemAnimator}. Whether item animations are
1645     * enabled for the RecyclerView depends on the ItemAnimator and whether
1646     * the LayoutManager {@link LayoutManager#supportsPredictiveItemAnimations()
1647     * supports item animations}.
1648     *
1649     * @param animator The ItemAnimator being set. If null, no animations will occur
1650     * when changes occur to the items in this RecyclerView.
1651     */
1652    public void setItemAnimator(ItemAnimator animator) {
1653        if (mItemAnimator != null) {
1654            mItemAnimator.endAnimations();
1655            mItemAnimator.setListener(null);
1656        }
1657        mItemAnimator = animator;
1658        if (mItemAnimator != null) {
1659            mItemAnimator.setListener(mItemAnimatorListener);
1660        }
1661    }
1662
1663    /**
1664     * Gets the current ItemAnimator for this RecyclerView. A null return value
1665     * indicates that there is no animator and that item changes will happen without
1666     * any animations. By default, RecyclerView instantiates and
1667     * uses an instance of {@link DefaultItemAnimator}.
1668     *
1669     * @return ItemAnimator The current ItemAnimator. If null, no animations will occur
1670     * when changes occur to the items in this RecyclerView.
1671     */
1672    public ItemAnimator getItemAnimator() {
1673        return mItemAnimator;
1674    }
1675
1676    private boolean supportsChangeAnimations() {
1677        return mItemAnimator != null && mItemAnimator.getSupportsChangeAnimations();
1678    }
1679
1680    /**
1681     * Post a runnable to the next frame to run pending item animations. Only the first such
1682     * request will be posted, governed by the mPostedAnimatorRunner flag.
1683     */
1684    private void postAnimationRunner() {
1685        if (!mPostedAnimatorRunner && mIsAttached) {
1686            ViewCompat.postOnAnimation(this, mItemAnimatorRunner);
1687            mPostedAnimatorRunner = true;
1688        }
1689    }
1690
1691    private boolean predictiveItemAnimationsEnabled() {
1692        return (mItemAnimator != null && mLayout.supportsPredictiveItemAnimations());
1693    }
1694
1695    /**
1696     * Consumes adapter updates and calculates which type of animations we want to run.
1697     * Called in onMeasure and dispatchLayout.
1698     * <p>
1699     * This method may process only the pre-layout state of updates or all of them.
1700     */
1701    private void processAdapterUpdatesAndSetAnimationFlags() {
1702        if (mDataSetHasChangedAfterLayout) {
1703            // Processing these items have no value since data set changed unexpectedly.
1704            // Instead, we just reset it.
1705            mAdapterHelper.reset();
1706            markKnownViewsInvalid();
1707            mLayout.onItemsChanged(this);
1708        }
1709        // simple animations are a subset of advanced animations (which will cause a
1710        // pre-layout step)
1711        // If layout supports predictive animations, pre-process to decide if we want to run them
1712        if (mItemAnimator != null && mLayout.supportsPredictiveItemAnimations()) {
1713            mAdapterHelper.preProcess();
1714        } else {
1715            mAdapterHelper.consumeUpdatesInOnePass();
1716        }
1717        boolean animationTypeSupported = (mItemsAddedOrRemoved && !mItemsChanged) ||
1718                (mItemsAddedOrRemoved || (mItemsChanged && supportsChangeAnimations()));
1719        mState.mRunSimpleAnimations = mFirstLayoutComplete && mItemAnimator != null &&
1720                (mDataSetHasChangedAfterLayout || animationTypeSupported ||
1721                        mLayout.mRequestedSimpleAnimations) &&
1722                (!mDataSetHasChangedAfterLayout || mAdapter.hasStableIds());
1723        mState.mRunPredictiveAnimations = mState.mRunSimpleAnimations &&
1724                animationTypeSupported && !mDataSetHasChangedAfterLayout &&
1725                predictiveItemAnimationsEnabled();
1726    }
1727
1728    /**
1729     * Wrapper around layoutChildren() that handles animating changes caused by layout.
1730     * Animations work on the assumption that there are five different kinds of items
1731     * in play:
1732     * PERSISTENT: items are visible before and after layout
1733     * REMOVED: items were visible before layout and were removed by the app
1734     * ADDED: items did not exist before layout and were added by the app
1735     * DISAPPEARING: items exist in the data set before/after, but changed from
1736     * visible to non-visible in the process of layout (they were moved off
1737     * screen as a side-effect of other changes)
1738     * APPEARING: items exist in the data set before/after, but changed from
1739     * non-visible to visible in the process of layout (they were moved on
1740     * screen as a side-effect of other changes)
1741     * The overall approach figures out what items exist before/after layout and
1742     * infers one of the five above states for each of the items. Then the animations
1743     * are set up accordingly:
1744     * PERSISTENT views are moved ({@link ItemAnimator#animateMove(ViewHolder, int, int, int, int)})
1745     * REMOVED views are removed ({@link ItemAnimator#animateRemove(ViewHolder)})
1746     * ADDED views are added ({@link ItemAnimator#animateAdd(ViewHolder)})
1747     * DISAPPEARING views are moved off screen
1748     * APPEARING views are moved on screen
1749     */
1750    void dispatchLayout() {
1751        if (mAdapter == null) {
1752            Log.e(TAG, "No adapter attached; skipping layout");
1753            return;
1754        }
1755        mDisappearingViewsInLayoutPass.clear();
1756        eatRequestLayout();
1757        mRunningLayoutOrScroll = true;
1758
1759        processAdapterUpdatesAndSetAnimationFlags();
1760
1761        mState.mOldChangedHolders = mState.mRunSimpleAnimations && mItemsChanged
1762                && supportsChangeAnimations() ? new ArrayMap<Long, ViewHolder>() : null;
1763        mItemsAddedOrRemoved = mItemsChanged = false;
1764        ArrayMap<View, Rect> appearingViewInitialBounds = null;
1765        mState.mInPreLayout = mState.mRunPredictiveAnimations;
1766        mState.mItemCount = mAdapter.getItemCount();
1767
1768        if (mState.mRunSimpleAnimations) {
1769            // Step 0: Find out where all non-removed items are, pre-layout
1770            mState.mPreLayoutHolderMap.clear();
1771            mState.mPostLayoutHolderMap.clear();
1772            int count = mChildHelper.getChildCount();
1773            for (int i = 0; i < count; ++i) {
1774                final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i));
1775                if (holder.shouldIgnore() || (holder.isInvalid() && !mAdapter.hasStableIds())) {
1776                    continue;
1777                }
1778                final View view = holder.itemView;
1779                mState.mPreLayoutHolderMap.put(holder, new ItemHolderInfo(holder,
1780                        view.getLeft(), view.getTop(), view.getRight(), view.getBottom()));
1781            }
1782        }
1783        if (mState.mRunPredictiveAnimations) {
1784            // Step 1: run prelayout: This will use the old positions of items. The layout manager
1785            // is expected to layout everything, even removed items (though not to add removed
1786            // items back to the container). This gives the pre-layout position of APPEARING views
1787            // which come into existence as part of the real layout.
1788
1789            // Save old positions so that LayoutManager can run its mapping logic.
1790            saveOldPositions();
1791            // processAdapterUpdatesAndSetAnimationFlags already run pre-layout animations.
1792            if (mState.mOldChangedHolders != null) {
1793                int count = mChildHelper.getChildCount();
1794                for (int i = 0; i < count; ++i) {
1795                    final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i));
1796                    if (holder.isChanged() && !holder.isRemoved() && !holder.shouldIgnore()) {
1797                        long key = getChangedHolderKey(holder);
1798                        mState.mOldChangedHolders.put(key, holder);
1799                        mState.mPreLayoutHolderMap.remove(holder);
1800                    }
1801                }
1802            }
1803
1804            final boolean didStructureChange = mState.mStructureChanged;
1805            mState.mStructureChanged = false;
1806            // temporarily disable flag because we are asking for previous layout
1807            mLayout.onLayoutChildren(mRecycler, mState);
1808            mState.mStructureChanged = didStructureChange;
1809
1810            appearingViewInitialBounds = new ArrayMap<View, Rect>();
1811            for (int i = 0; i < mChildHelper.getChildCount(); ++i) {
1812                boolean found = false;
1813                View child = mChildHelper.getChildAt(i);
1814                if (getChildViewHolderInt(child).shouldIgnore()) {
1815                    continue;
1816                }
1817                for (int j = 0; j < mState.mPreLayoutHolderMap.size(); ++j) {
1818                    ViewHolder holder = mState.mPreLayoutHolderMap.keyAt(j);
1819                    if (holder.itemView == child) {
1820                        found = true;
1821                        continue;
1822                    }
1823                }
1824                if (!found) {
1825                    appearingViewInitialBounds.put(child, new Rect(child.getLeft(), child.getTop(),
1826                            child.getRight(), child.getBottom()));
1827                }
1828            }
1829            // we don't process disappearing list because they may re-appear in post layout pass.
1830            clearOldPositions();
1831            mAdapterHelper.consumePostponedUpdates();
1832        } else {
1833            clearOldPositions();
1834            // in case pre layout did run but we decided not to run predictive animations.
1835            mAdapterHelper.consumeUpdatesInOnePass();
1836            if (mState.mOldChangedHolders != null) {
1837                int count = mChildHelper.getChildCount();
1838                for (int i = 0; i < count; ++i) {
1839                    final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i));
1840                    if (holder.isChanged() && !holder.isRemoved() && !holder.shouldIgnore()) {
1841                        long key = getChangedHolderKey(holder);
1842                        mState.mOldChangedHolders.put(key, holder);
1843                        mState.mPreLayoutHolderMap.remove(holder);
1844                    }
1845                }
1846            }
1847        }
1848        mState.mItemCount = mAdapter.getItemCount();
1849        mState.mDeletedInvisibleItemCountSincePreviousLayout = 0;
1850
1851        // Step 2: Run layout
1852        mState.mInPreLayout = false;
1853        mLayout.onLayoutChildren(mRecycler, mState);
1854
1855        mState.mStructureChanged = false;
1856        mPendingSavedState = null;
1857
1858        // onLayoutChildren may have caused client code to disable item animations; re-check
1859        mState.mRunSimpleAnimations = mState.mRunSimpleAnimations && mItemAnimator != null;
1860
1861        if (mState.mRunSimpleAnimations) {
1862            // Step 3: Find out where things are now, post-layout
1863            ArrayMap<Long, ViewHolder> newChangedHolders = mState.mOldChangedHolders != null ?
1864                    new ArrayMap<Long, ViewHolder>() : null;
1865            int count = mChildHelper.getChildCount();
1866            for (int i = 0; i < count; ++i) {
1867                ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i));
1868                if (holder.shouldIgnore()) {
1869                    continue;
1870                }
1871                final View view = holder.itemView;
1872                long key = getChangedHolderKey(holder);
1873                if (newChangedHolders != null && mState.mOldChangedHolders.get(key) != null) {
1874                    newChangedHolders.put(key, holder);
1875                } else {
1876                    mState.mPostLayoutHolderMap.put(holder, new ItemHolderInfo(holder,
1877                            view.getLeft(), view.getTop(), view.getRight(), view.getBottom()));
1878                }
1879            }
1880            processDisappearingList();
1881            // Step 4: Animate DISAPPEARING and REMOVED items
1882            int preLayoutCount = mState.mPreLayoutHolderMap.size();
1883            for (int i = preLayoutCount - 1; i >= 0; i--) {
1884                ViewHolder itemHolder = mState.mPreLayoutHolderMap.keyAt(i);
1885                if (!mState.mPostLayoutHolderMap.containsKey(itemHolder)) {
1886                    ItemHolderInfo disappearingItem = mState.mPreLayoutHolderMap.valueAt(i);
1887                    mState.mPreLayoutHolderMap.removeAt(i);
1888
1889                    View disappearingItemView = disappearingItem.holder.itemView;
1890                    removeDetachedView(disappearingItemView, false);
1891                    mRecycler.unscrapView(disappearingItem.holder);
1892
1893                    animateDisappearance(disappearingItem);
1894                }
1895            }
1896            // Step 5: Animate APPEARING and ADDED items
1897            int postLayoutCount = mState.mPostLayoutHolderMap.size();
1898            if (postLayoutCount > 0) {
1899                for (int i = postLayoutCount - 1; i >= 0; i--) {
1900                    ViewHolder itemHolder = mState.mPostLayoutHolderMap.keyAt(i);
1901                    ItemHolderInfo info = mState.mPostLayoutHolderMap.valueAt(i);
1902                    if ((mState.mPreLayoutHolderMap.isEmpty() ||
1903                            !mState.mPreLayoutHolderMap.containsKey(itemHolder))) {
1904                        mState.mPostLayoutHolderMap.removeAt(i);
1905                        Rect initialBounds = (appearingViewInitialBounds != null) ?
1906                                appearingViewInitialBounds.get(itemHolder.itemView) : null;
1907                        animateAppearance(itemHolder, initialBounds,
1908                                info.left, info.top);
1909                    }
1910                }
1911            }
1912            // Step 6: Animate PERSISTENT items
1913            count = mState.mPostLayoutHolderMap.size();
1914            for (int i = 0; i < count; ++i) {
1915                ViewHolder postHolder = mState.mPostLayoutHolderMap.keyAt(i);
1916                ItemHolderInfo postInfo = mState.mPostLayoutHolderMap.valueAt(i);
1917                ItemHolderInfo preInfo = mState.mPreLayoutHolderMap.get(postHolder);
1918                if (preInfo != null && postInfo != null) {
1919                    if (preInfo.left != postInfo.left || preInfo.top != postInfo.top) {
1920                        postHolder.setIsRecyclable(false);
1921                        if (DEBUG) {
1922                            Log.d(TAG, "PERSISTENT: " + postHolder +
1923                                    " with view " + postHolder.itemView);
1924                        }
1925                        if (mItemAnimator.animateMove(postHolder,
1926                                preInfo.left, preInfo.top, postInfo.left, postInfo.top)) {
1927                            postAnimationRunner();
1928                        }
1929                    }
1930                }
1931            }
1932            // Step 7: Animate CHANGING items
1933            count = mState.mOldChangedHolders != null ? mState.mOldChangedHolders.size() : 0;
1934            // traverse reverse in case view gets recycled while we are traversing the list.
1935            for (int i = count - 1; i >= 0; i--) {
1936                long key = mState.mOldChangedHolders.keyAt(i);
1937                ViewHolder oldHolder = mState.mOldChangedHolders.get(key);
1938                View oldView = oldHolder.itemView;
1939                if (oldHolder.shouldIgnore()) {
1940                    continue;
1941                }
1942                // We probably don't need this check anymore since these views are removed from
1943                // the list if they are recycled.
1944                if (mRecycler.mChangedScrap != null &&
1945                        mRecycler.mChangedScrap.contains(oldHolder)) {
1946                    animateChange(oldHolder, newChangedHolders.get(key));
1947                } else if (DEBUG) {
1948                    Log.e(TAG, "cannot find old changed holder in changed scrap :/" + oldHolder);
1949                }
1950            }
1951        }
1952        resumeRequestLayout(false);
1953        mLayout.removeAndRecycleScrapInt(mRecycler, !mState.mRunPredictiveAnimations);
1954        mState.mPreviousLayoutItemCount = mState.mItemCount;
1955        mDataSetHasChangedAfterLayout = false;
1956        mState.mRunSimpleAnimations = false;
1957        mState.mRunPredictiveAnimations = false;
1958        mRunningLayoutOrScroll = false;
1959        mLayout.mRequestedSimpleAnimations = false;
1960        if (mRecycler.mChangedScrap != null) {
1961            mRecycler.mChangedScrap.clear();
1962        }
1963        mState.mOldChangedHolders = null;
1964    }
1965
1966    /**
1967     * Returns a unique key to be used while handling change animations.
1968     * It might be child's position or stable id depending on the adapter type.
1969     */
1970    long getChangedHolderKey(ViewHolder holder) {
1971        return mAdapter.hasStableIds() ? holder.getItemId() : holder.mPosition;
1972    }
1973
1974    /**
1975     * A LayoutManager may want to layout a view just to animate disappearance.
1976     * This method handles those views and triggers remove animation on them.
1977     */
1978    private void processDisappearingList() {
1979        final int count = mDisappearingViewsInLayoutPass.size();
1980        for (int i = 0; i < count; i ++) {
1981            View view = mDisappearingViewsInLayoutPass.get(i);
1982            ViewHolder vh = getChildViewHolderInt(view);
1983            final ItemHolderInfo info = mState.mPreLayoutHolderMap.remove(vh);
1984            if (!mState.isPreLayout()) {
1985                mState.mPostLayoutHolderMap.remove(vh);
1986            }
1987            if (info != null) {
1988                animateDisappearance(info);
1989            } else {
1990                // let it disappear from the position it becomes visible
1991                animateDisappearance(new ItemHolderInfo(vh, view.getLeft(), view.getTop(),
1992                        view.getRight(), view.getBottom()));
1993            }
1994        }
1995        mDisappearingViewsInLayoutPass.clear();
1996    }
1997
1998    private void animateAppearance(ViewHolder itemHolder, Rect beforeBounds, int afterLeft,
1999            int afterTop) {
2000        View newItemView = itemHolder.itemView;
2001        if (beforeBounds != null &&
2002                (beforeBounds.left != afterLeft || beforeBounds.top != afterTop)) {
2003            // slide items in if before/after locations differ
2004            itemHolder.setIsRecyclable(false);
2005            if (DEBUG) {
2006                Log.d(TAG, "APPEARING: " + itemHolder + " with view " + newItemView);
2007            }
2008            if (mItemAnimator.animateMove(itemHolder,
2009                    beforeBounds.left, beforeBounds.top,
2010                    afterLeft, afterTop)) {
2011                postAnimationRunner();
2012            }
2013        } else {
2014            if (DEBUG) {
2015                Log.d(TAG, "ADDED: " + itemHolder + " with view " + newItemView);
2016            }
2017            itemHolder.setIsRecyclable(false);
2018            if (mItemAnimator.animateAdd(itemHolder)) {
2019                postAnimationRunner();
2020            }
2021        }
2022    }
2023
2024    private void animateDisappearance(ItemHolderInfo disappearingItem) {
2025        View disappearingItemView = disappearingItem.holder.itemView;
2026        addAnimatingView(disappearingItemView);
2027        int oldLeft = disappearingItem.left;
2028        int oldTop = disappearingItem.top;
2029        int newLeft = disappearingItemView.getLeft();
2030        int newTop = disappearingItemView.getTop();
2031        if (oldLeft != newLeft || oldTop != newTop) {
2032            disappearingItem.holder.setIsRecyclable(false);
2033            disappearingItemView.layout(newLeft, newTop,
2034                    newLeft + disappearingItemView.getWidth(),
2035                    newTop + disappearingItemView.getHeight());
2036            if (DEBUG) {
2037                Log.d(TAG, "DISAPPEARING: " + disappearingItem.holder +
2038                        " with view " + disappearingItemView);
2039            }
2040            if (mItemAnimator.animateMove(disappearingItem.holder, oldLeft, oldTop,
2041                    newLeft, newTop)) {
2042                postAnimationRunner();
2043            }
2044        } else {
2045            if (DEBUG) {
2046                Log.d(TAG, "REMOVED: " + disappearingItem.holder +
2047                        " with view " + disappearingItemView);
2048            }
2049            disappearingItem.holder.setIsRecyclable(false);
2050            if (mItemAnimator.animateRemove(disappearingItem.holder)) {
2051                postAnimationRunner();
2052            }
2053        }
2054    }
2055
2056    private void animateChange(ViewHolder oldHolder, ViewHolder newHolder) {
2057        oldHolder.setIsRecyclable(false);
2058        removeDetachedView(oldHolder.itemView, false);
2059        addAnimatingView(oldHolder.itemView);
2060        oldHolder.mShadowedHolder = newHolder;
2061        mRecycler.unscrapView(oldHolder);
2062        if (DEBUG) {
2063            Log.d(TAG, "CHANGED: " + oldHolder + " with view " + oldHolder.itemView);
2064        }
2065        final int fromLeft = oldHolder.itemView.getLeft();
2066        final int fromTop = oldHolder.itemView.getTop();
2067        final int toLeft, toTop;
2068        if (newHolder == null || newHolder.shouldIgnore()) {
2069            toLeft = fromLeft;
2070            toTop = fromTop;
2071        } else {
2072            toLeft = newHolder.itemView.getLeft();
2073            toTop = newHolder.itemView.getTop();
2074            newHolder.setIsRecyclable(false);
2075            newHolder.mShadowingHolder = oldHolder;
2076        }
2077        if(mItemAnimator.animateChange(oldHolder, newHolder,
2078                fromLeft, fromTop, toLeft, toTop)) {
2079            postAnimationRunner();
2080        }
2081    }
2082
2083    @Override
2084    protected void onLayout(boolean changed, int l, int t, int r, int b) {
2085        eatRequestLayout();
2086        dispatchLayout();
2087        resumeRequestLayout(false);
2088        mFirstLayoutComplete = true;
2089    }
2090
2091    @Override
2092    public void requestLayout() {
2093        if (!mEatRequestLayout) {
2094            super.requestLayout();
2095        } else {
2096            mLayoutRequestEaten = true;
2097        }
2098    }
2099
2100    void markItemDecorInsetsDirty() {
2101        final int childCount = mChildHelper.getUnfilteredChildCount();
2102        for (int i = 0; i < childCount; i++) {
2103            final View child = mChildHelper.getUnfilteredChildAt(i);
2104            ((LayoutParams) child.getLayoutParams()).mInsetsDirty = true;
2105        }
2106        mRecycler.markItemDecorInsetsDirty();
2107    }
2108
2109    @Override
2110    public void draw(Canvas c) {
2111        super.draw(c);
2112
2113        final int count = mItemDecorations.size();
2114        for (int i = 0; i < count; i++) {
2115            mItemDecorations.get(i).onDrawOver(c, this, mState);
2116        }
2117        // TODO If padding is not 0 and chilChildrenToPadding is false, to draw glows properly, we
2118        // need find children closest to edges. Not sure if it is worth the effort.
2119        boolean needsInvalidate = false;
2120        if (mLeftGlow != null && !mLeftGlow.isFinished()) {
2121            final int restore = c.save();
2122            final int padding = mClipToPadding ? getPaddingBottom() : 0;
2123            c.rotate(270);
2124            c.translate(-getHeight() + padding, 0);
2125            needsInvalidate = mLeftGlow != null && mLeftGlow.draw(c);
2126            c.restoreToCount(restore);
2127        }
2128        if (mTopGlow != null && !mTopGlow.isFinished()) {
2129            final int restore = c.save();
2130            if (mClipToPadding) {
2131                c.translate(getPaddingLeft(), getPaddingTop());
2132            }
2133            needsInvalidate |= mTopGlow != null && mTopGlow.draw(c);
2134            c.restoreToCount(restore);
2135        }
2136        if (mRightGlow != null && !mRightGlow.isFinished()) {
2137            final int restore = c.save();
2138            final int width = getWidth();
2139            final int padding = mClipToPadding ? getPaddingTop() : 0;
2140            c.rotate(90);
2141            c.translate(-padding, -width);
2142            needsInvalidate |= mRightGlow != null && mRightGlow.draw(c);
2143            c.restoreToCount(restore);
2144        }
2145        if (mBottomGlow != null && !mBottomGlow.isFinished()) {
2146            final int restore = c.save();
2147            c.rotate(180);
2148            if (mClipToPadding) {
2149                c.translate(-getWidth() + getPaddingRight(), -getHeight() + getPaddingBottom());
2150            } else {
2151                c.translate(-getWidth(), -getHeight());
2152            }
2153            needsInvalidate |= mBottomGlow != null && mBottomGlow.draw(c);
2154            c.restoreToCount(restore);
2155        }
2156
2157        // If some views are animating, ItemDecorators are likely to move/change with them.
2158        // Invalidate RecyclerView to re-draw decorators. This is still efficient because children's
2159        // display lists are not invalidated.
2160        if (!needsInvalidate && mItemAnimator != null && mItemDecorations.size() > 0 &&
2161                mItemAnimator.isRunning()) {
2162            needsInvalidate = true;
2163        }
2164
2165        if (needsInvalidate) {
2166            ViewCompat.postInvalidateOnAnimation(this);
2167        }
2168    }
2169
2170    @Override
2171    public void onDraw(Canvas c) {
2172        super.onDraw(c);
2173
2174        final int count = mItemDecorations.size();
2175        for (int i = 0; i < count; i++) {
2176            mItemDecorations.get(i).onDraw(c, this, mState);
2177        }
2178    }
2179
2180    @Override
2181    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2182        return p instanceof LayoutParams && mLayout.checkLayoutParams((LayoutParams) p);
2183    }
2184
2185    @Override
2186    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
2187        if (mLayout == null) {
2188            throw new IllegalStateException("RecyclerView has no LayoutManager");
2189        }
2190        return mLayout.generateDefaultLayoutParams();
2191    }
2192
2193    @Override
2194    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2195        if (mLayout == null) {
2196            throw new IllegalStateException("RecyclerView has no LayoutManager");
2197        }
2198        return mLayout.generateLayoutParams(getContext(), attrs);
2199    }
2200
2201    @Override
2202    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2203        if (mLayout == null) {
2204            throw new IllegalStateException("RecyclerView has no LayoutManager");
2205        }
2206        return mLayout.generateLayoutParams(p);
2207    }
2208
2209    void saveOldPositions() {
2210        final int childCount = mChildHelper.getUnfilteredChildCount();
2211        for (int i = 0; i < childCount; i++) {
2212            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
2213            if (DEBUG && holder.mPosition == -1 && !holder.isRemoved()) {
2214                throw new IllegalStateException("view holder cannot have position -1 unless it"
2215                        + " is removed");
2216            }
2217            if (!holder.shouldIgnore()) {
2218                holder.saveOldPosition();
2219            }
2220        }
2221    }
2222
2223    void clearOldPositions() {
2224        final int childCount = mChildHelper.getUnfilteredChildCount();
2225        for (int i = 0; i < childCount; i++) {
2226            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
2227            if (!holder.shouldIgnore()) {
2228                holder.clearOldPosition();
2229            }
2230        }
2231        mRecycler.clearOldPositions();
2232    }
2233
2234    void offsetPositionRecordsForMove(int from, int to) {
2235        final int childCount = mChildHelper.getUnfilteredChildCount();
2236        final int start, end, inBetweenOffset;
2237        if (from < to) {
2238            start = from;
2239            end = to;
2240            inBetweenOffset = -1;
2241        } else {
2242            start = to;
2243            end = from;
2244            inBetweenOffset = 1;
2245        }
2246
2247        for (int i = 0; i < childCount; i++) {
2248            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
2249            if (holder == null || holder.mPosition < start || holder.mPosition > end) {
2250                continue;
2251            }
2252            if (DEBUG) {
2253                Log.d(TAG, "offsetPositionRecordsForMove attached child " + i + " holder " +
2254                        holder);
2255            }
2256            if (holder.mPosition == from) {
2257                holder.offsetPosition(to - from, false);
2258            } else {
2259                holder.offsetPosition(inBetweenOffset, false);
2260            }
2261
2262            mState.mStructureChanged = true;
2263        }
2264        mRecycler.offsetPositionRecordsForMove(from, to);
2265        requestLayout();
2266    }
2267
2268    void offsetPositionRecordsForInsert(int positionStart, int itemCount) {
2269        final int childCount = mChildHelper.getUnfilteredChildCount();
2270        for (int i = 0; i < childCount; i++) {
2271            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
2272            if (holder != null && !holder.shouldIgnore() && holder.mPosition >= positionStart) {
2273                if (DEBUG) {
2274                    Log.d(TAG, "offsetPositionRecordsForInsert attached child " + i + " holder " +
2275                            holder + " now at position " + (holder.mPosition + itemCount));
2276                }
2277                holder.offsetPosition(itemCount, false);
2278                mState.mStructureChanged = true;
2279            }
2280        }
2281        mRecycler.offsetPositionRecordsForInsert(positionStart, itemCount);
2282        requestLayout();
2283    }
2284
2285    void offsetPositionRecordsForRemove(int positionStart, int itemCount,
2286            boolean applyToPreLayout) {
2287        final int positionEnd = positionStart + itemCount;
2288        final int childCount = mChildHelper.getUnfilteredChildCount();
2289        for (int i = 0; i < childCount; i++) {
2290            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
2291            if (holder != null && !holder.shouldIgnore()) {
2292                if (holder.mPosition >= positionEnd) {
2293                    if (DEBUG) {
2294                        Log.d(TAG, "offsetPositionRecordsForRemove attached child " + i +
2295                                " holder " + holder + " now at position " +
2296                                (holder.mPosition - itemCount));
2297                    }
2298                    holder.offsetPosition(-itemCount, applyToPreLayout);
2299                    mState.mStructureChanged = true;
2300                } else if (holder.mPosition >= positionStart) {
2301                    if (DEBUG) {
2302                        Log.d(TAG, "offsetPositionRecordsForRemove attached child " + i +
2303                                " holder " + holder + " now REMOVED");
2304                    }
2305                    holder.addFlags(ViewHolder.FLAG_REMOVED);
2306                    mState.mStructureChanged = true;
2307                    holder.offsetPosition(-itemCount, applyToPreLayout);
2308                }
2309            }
2310        }
2311        mRecycler.offsetPositionRecordsForRemove(positionStart, itemCount, applyToPreLayout);
2312        requestLayout();
2313    }
2314
2315    /**
2316     * Rebind existing views for the given range, or create as needed.
2317     *
2318     * @param positionStart Adapter position to start at
2319     * @param itemCount Number of views that must explicitly be rebound
2320     */
2321    void viewRangeUpdate(int positionStart, int itemCount) {
2322        final int childCount = mChildHelper.getUnfilteredChildCount();
2323        final int positionEnd = positionStart + itemCount;
2324
2325        for (int i = 0; i < childCount; i++) {
2326            final View child = mChildHelper.getUnfilteredChildAt(i);
2327            final ViewHolder holder = getChildViewHolderInt(child);
2328            if (holder == null || holder.shouldIgnore()) {
2329                continue;
2330            }
2331            if (holder.mPosition >= positionStart && holder.mPosition < positionEnd) {
2332                // We re-bind these view holders after pre-processing is complete so that
2333                // ViewHolders have their final positions assigned.
2334                holder.addFlags(ViewHolder.FLAG_UPDATE);
2335                if (supportsChangeAnimations()) {
2336                    holder.addFlags(ViewHolder.FLAG_CHANGED);
2337                }
2338                // lp cannot be null since we get ViewHolder from it.
2339                ((LayoutParams) child.getLayoutParams()).mInsetsDirty = true;
2340            }
2341        }
2342        mRecycler.viewRangeUpdate(positionStart, itemCount);
2343    }
2344
2345    void rebindUpdatedViewHolders() {
2346        final int childCount = mChildHelper.getChildCount();
2347        for (int i = 0; i < childCount; i++) {
2348            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i));
2349            // validate type is correct
2350            if (holder == null || holder.shouldIgnore()) {
2351                continue;
2352            }
2353            if (holder.isRemoved() || holder.isInvalid()) {
2354                requestLayout();
2355            } else if (holder.needsUpdate()) {
2356                final int type = mAdapter.getItemViewType(holder.mPosition);
2357                if (holder.getItemViewType() == type) {
2358                    // Binding an attached view will request a layout if needed.
2359                    if (!holder.isChanged() || !supportsChangeAnimations()) {
2360                        mAdapter.bindViewHolder(holder, holder.mPosition);
2361                    } else {
2362                        // Don't rebind changed holders if change animations are enabled.
2363                        // We want the old contents for the animation and will get a new
2364                        // holder for the new contents.
2365                        requestLayout();
2366                    }
2367                } else {
2368                    // binding to a new view will need re-layout anyways. We can as well trigger
2369                    // it here so that it happens during layout
2370                    holder.addFlags(ViewHolder.FLAG_INVALID);
2371                    requestLayout();
2372                }
2373            }
2374        }
2375    }
2376
2377    /**
2378     * Mark all known views as invalid. Used in response to a, "the whole world might have changed"
2379     * data change event.
2380     */
2381    void markKnownViewsInvalid() {
2382        final int childCount = mChildHelper.getUnfilteredChildCount();
2383        for (int i = 0; i < childCount; i++) {
2384            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
2385            if (holder != null && !holder.shouldIgnore()) {
2386                holder.addFlags(ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID);
2387            }
2388        }
2389        markItemDecorInsetsDirty();
2390        mRecycler.markKnownViewsInvalid();
2391    }
2392
2393    /**
2394     * Invalidates all ItemDecorations. If RecyclerView has item decorations, calling this method
2395     * will trigger a {@link #requestLayout()} call.
2396     */
2397    public void invalidateItemDecorations() {
2398        if (mItemDecorations.size() == 0) {
2399            return;
2400        }
2401        if (mLayout != null) {
2402            mLayout.assertNotInLayoutOrScroll("Cannot invalidate item decorations during a scroll"
2403                    + " or layout");
2404        }
2405        markItemDecorInsetsDirty();
2406        requestLayout();
2407    }
2408
2409    /**
2410     * Retrieve the {@link ViewHolder} for the given child view.
2411     *
2412     * @param child Child of this RecyclerView to query for its ViewHolder
2413     * @return The child view's ViewHolder
2414     */
2415    public ViewHolder getChildViewHolder(View child) {
2416        final ViewParent parent = child.getParent();
2417        if (parent != null && parent != this) {
2418            throw new IllegalArgumentException("View " + child + " is not a direct child of " +
2419                    this);
2420        }
2421        return getChildViewHolderInt(child);
2422    }
2423
2424    static ViewHolder getChildViewHolderInt(View child) {
2425        if (child == null) {
2426            return null;
2427        }
2428        return ((LayoutParams) child.getLayoutParams()).mViewHolder;
2429    }
2430
2431    /**
2432     * Return the adapter position that the given child view corresponds to.
2433     *
2434     * @param child Child View to query
2435     * @return Adapter position corresponding to the given view or {@link #NO_POSITION}
2436     */
2437    public int getChildPosition(View child) {
2438        final ViewHolder holder = getChildViewHolderInt(child);
2439        return holder != null ? holder.getPosition() : NO_POSITION;
2440    }
2441
2442    /**
2443     * Return the stable item id that the given child view corresponds to.
2444     *
2445     * @param child Child View to query
2446     * @return Item id corresponding to the given view or {@link #NO_ID}
2447     */
2448    public long getChildItemId(View child) {
2449        if (mAdapter == null || !mAdapter.hasStableIds()) {
2450            return NO_ID;
2451        }
2452        final ViewHolder holder = getChildViewHolderInt(child);
2453        return holder != null ? holder.getItemId() : NO_ID;
2454    }
2455
2456    /**
2457     * Return the ViewHolder for the item in the given position of the data set.
2458     *
2459     * @param position The position of the item in the data set of the adapter
2460     * @return The ViewHolder at <code>position</code>
2461     */
2462    public ViewHolder findViewHolderForPosition(int position) {
2463        return findViewHolderForPosition(position, false);
2464    }
2465
2466    ViewHolder findViewHolderForPosition(int position, boolean checkNewPosition) {
2467        final int childCount = mChildHelper.getUnfilteredChildCount();
2468        for (int i = 0; i < childCount; i++) {
2469            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
2470            if (holder != null && !holder.isRemoved()) {
2471                if (checkNewPosition) {
2472                    if (holder.mPosition == position) {
2473                        return holder;
2474                    }
2475                } else if (holder.getPosition() == position) {
2476                    return holder;
2477                }
2478            }
2479        }
2480        // This method should not query cached views. It creates a problem during adapter updates
2481        // when we are dealing with already laid out views. Also, for the public method, it is more
2482        // reasonable to return null if position is not laid out.
2483        return null;
2484    }
2485
2486    /**
2487     * Return the ViewHolder for the item with the given id. The RecyclerView must
2488     * use an Adapter with {@link Adapter#setHasStableIds(boolean) stableIds} to
2489     * return a non-null value.
2490     *
2491     * @param id The id for the requested item
2492     * @return The ViewHolder with the given <code>id</code>, of null if there
2493     * is no such item.
2494     */
2495    public ViewHolder findViewHolderForItemId(long id) {
2496        final int childCount = mChildHelper.getUnfilteredChildCount();
2497        for (int i = 0; i < childCount; i++) {
2498            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
2499            if (holder != null && holder.getItemId() == id) {
2500                return holder;
2501            }
2502        }
2503        // this method should not query cached views. They are not children so they
2504        // should not be returned in this public method
2505        return null;
2506    }
2507
2508    /**
2509     * Find the topmost view under the given point.
2510     *
2511     * @param x Horizontal position in pixels to search
2512     * @param y Vertical position in pixels to search
2513     * @return The child view under (x, y) or null if no matching child is found
2514     */
2515    public View findChildViewUnder(float x, float y) {
2516        final int count = mChildHelper.getChildCount();
2517        for (int i = count - 1; i >= 0; i--) {
2518            final View child = mChildHelper.getChildAt(i);
2519            final float translationX = ViewCompat.getTranslationX(child);
2520            final float translationY = ViewCompat.getTranslationY(child);
2521            if (x >= child.getLeft() + translationX &&
2522                    x <= child.getRight() + translationX &&
2523                    y >= child.getTop() + translationY &&
2524                    y <= child.getBottom() + translationY) {
2525                return child;
2526            }
2527        }
2528        return null;
2529    }
2530
2531    /**
2532     * Offset the bounds of all child views by <code>dy</code> pixels.
2533     * Useful for implementing simple scrolling in {@link LayoutManager LayoutManagers}.
2534     *
2535     * @param dy Vertical pixel offset to apply to the bounds of all child views
2536     */
2537    public void offsetChildrenVertical(int dy) {
2538        final int childCount = mChildHelper.getChildCount();
2539        for (int i = 0; i < childCount; i++) {
2540            mChildHelper.getChildAt(i).offsetTopAndBottom(dy);
2541        }
2542    }
2543
2544    /**
2545     * Called when an item view is attached to this RecyclerView.
2546     *
2547     * <p>Subclasses of RecyclerView may want to perform extra bookkeeping or modifications
2548     * of child views as they become attached. This will be called before a
2549     * {@link LayoutManager} measures or lays out the view and is a good time to perform these
2550     * changes.</p>
2551     *
2552     * @param child Child view that is now attached to this RecyclerView and its associated window
2553     */
2554    public void onChildAttachedToWindow(View child) {
2555    }
2556
2557    /**
2558     * Called when an item view is detached from this RecyclerView.
2559     *
2560     * <p>Subclasses of RecyclerView may want to perform extra bookkeeping or modifications
2561     * of child views as they become detached. This will be called as a
2562     * {@link LayoutManager} fully detaches the child view from the parent and its window.</p>
2563     *
2564     * @param child Child view that is now detached from this RecyclerView and its associated window
2565     */
2566    public void onChildDetachedFromWindow(View child) {
2567    }
2568
2569    /**
2570     * Offset the bounds of all child views by <code>dx</code> pixels.
2571     * Useful for implementing simple scrolling in {@link LayoutManager LayoutManagers}.
2572     *
2573     * @param dx Horizontal pixel offset to apply to the bounds of all child views
2574     */
2575    public void offsetChildrenHorizontal(int dx) {
2576        final int childCount = mChildHelper.getChildCount();
2577        for (int i = 0; i < childCount; i++) {
2578            mChildHelper.getChildAt(i).offsetLeftAndRight(dx);
2579        }
2580    }
2581
2582    Rect getItemDecorInsetsForChild(View child) {
2583        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
2584        if (!lp.mInsetsDirty) {
2585            return lp.mDecorInsets;
2586        }
2587
2588        final Rect insets = lp.mDecorInsets;
2589        insets.set(0, 0, 0, 0);
2590        final int decorCount = mItemDecorations.size();
2591        for (int i = 0; i < decorCount; i++) {
2592            mTempRect.set(0, 0, 0, 0);
2593            mItemDecorations.get(i).getItemOffsets(mTempRect, child, this, mState);
2594            insets.left += mTempRect.left;
2595            insets.top += mTempRect.top;
2596            insets.right += mTempRect.right;
2597            insets.bottom += mTempRect.bottom;
2598        }
2599        lp.mInsetsDirty = false;
2600        return insets;
2601    }
2602
2603    private class ViewFlinger implements Runnable {
2604        private int mLastFlingX;
2605        private int mLastFlingY;
2606        private ScrollerCompat mScroller;
2607        private Interpolator mInterpolator = sQuinticInterpolator;
2608
2609
2610        // When set to true, postOnAnimation callbacks are delayed until the run method completes
2611        private boolean mEatRunOnAnimationRequest = false;
2612
2613        // Tracks if postAnimationCallback should be re-attached when it is done
2614        private boolean mReSchedulePostAnimationCallback = false;
2615
2616        public ViewFlinger() {
2617            mScroller = ScrollerCompat.create(getContext(), sQuinticInterpolator);
2618        }
2619
2620        @Override
2621        public void run() {
2622            disableRunOnAnimationRequests();
2623            consumePendingUpdateOperations();
2624            // keep a local reference so that if it is changed during onAnimation method, it won't
2625            // cause unexpected behaviors
2626            final ScrollerCompat scroller = mScroller;
2627            final SmoothScroller smoothScroller = mLayout.mSmoothScroller;
2628            if (scroller.computeScrollOffset()) {
2629                final int x = scroller.getCurrX();
2630                final int y = scroller.getCurrY();
2631                final int dx = x - mLastFlingX;
2632                final int dy = y - mLastFlingY;
2633                int hresult = 0;
2634                int vresult = 0;
2635                mLastFlingX = x;
2636                mLastFlingY = y;
2637                int overscrollX = 0, overscrollY = 0;
2638                if (mAdapter != null) {
2639                    eatRequestLayout();
2640                    mRunningLayoutOrScroll = true;
2641                    if (dx != 0) {
2642                        hresult = mLayout.scrollHorizontallyBy(dx, mRecycler, mState);
2643                        overscrollX = dx - hresult;
2644                    }
2645                    if (dy != 0) {
2646                        vresult = mLayout.scrollVerticallyBy(dy, mRecycler, mState);
2647                        overscrollY = dy - vresult;
2648                    }
2649                    if (supportsChangeAnimations()) {
2650                        // Fix up shadow views used by changing animations
2651                        int count = mChildHelper.getChildCount();
2652                        for (int i = 0; i < count; i++) {
2653                            View view = mChildHelper.getChildAt(i);
2654                            ViewHolder holder = getChildViewHolder(view);
2655                            if (holder != null && holder.mShadowingHolder != null) {
2656                                View shadowingView = holder.mShadowingHolder != null ?
2657                                        holder.mShadowingHolder.itemView : null;
2658                                if (shadowingView != null) {
2659                                    int left = view.getLeft();
2660                                    int top = view.getTop();
2661                                    if (left != shadowingView.getLeft() ||
2662                                            top != shadowingView.getTop()) {
2663                                        shadowingView.layout(left, top,
2664                                                left + shadowingView.getWidth(),
2665                                                top + shadowingView.getHeight());
2666                                    }
2667                                }
2668                            }
2669                        }
2670                    }
2671
2672                    if (smoothScroller != null && !smoothScroller.isPendingInitialRun() &&
2673                            smoothScroller.isRunning()) {
2674                        final int adapterSize = mState.getItemCount();
2675                        if (adapterSize == 0) {
2676                            smoothScroller.stop();
2677                        } else if (smoothScroller.getTargetPosition() >= adapterSize) {
2678                            smoothScroller.setTargetPosition(adapterSize - 1);
2679                            smoothScroller.onAnimation(dx - overscrollX, dy - overscrollY);
2680                        } else {
2681                            smoothScroller.onAnimation(dx - overscrollX, dy - overscrollY);
2682                        }
2683                    }
2684                    mRunningLayoutOrScroll = false;
2685                    resumeRequestLayout(false);
2686                }
2687                final boolean fullyConsumedScroll = dx == hresult && dy == vresult;
2688                if (!mItemDecorations.isEmpty()) {
2689                    invalidate();
2690                }
2691                if (ViewCompat.getOverScrollMode(RecyclerView.this) !=
2692                        ViewCompat.OVER_SCROLL_NEVER) {
2693                    considerReleasingGlowsOnScroll(dx, dy);
2694                }
2695                if (overscrollX != 0 || overscrollY != 0) {
2696                    final int vel = (int) scroller.getCurrVelocity();
2697
2698                    int velX = 0;
2699                    if (overscrollX != x) {
2700                        velX = overscrollX < 0 ? -vel : overscrollX > 0 ? vel : 0;
2701                    }
2702
2703                    int velY = 0;
2704                    if (overscrollY != y) {
2705                        velY = overscrollY < 0 ? -vel : overscrollY > 0 ? vel : 0;
2706                    }
2707
2708                    if (ViewCompat.getOverScrollMode(RecyclerView.this) !=
2709                            ViewCompat.OVER_SCROLL_NEVER) {
2710                        absorbGlows(velX, velY);
2711                    }
2712                    if ((velX != 0 || overscrollX == x || scroller.getFinalX() == 0) &&
2713                            (velY != 0 || overscrollY == y || scroller.getFinalY() == 0)) {
2714                        scroller.abortAnimation();
2715                    }
2716                }
2717                if (mScrollListener != null && (hresult != 0 || vresult != 0)) {
2718                    mScrollListener.onScrolled(RecyclerView.this, hresult, vresult);
2719                }
2720
2721                if (!awakenScrollBars()) {
2722                    invalidate();
2723                }
2724
2725                if (scroller.isFinished() || !fullyConsumedScroll) {
2726                    setScrollState(SCROLL_STATE_IDLE); // setting state to idle will stop this.
2727                } else {
2728                    postOnAnimation();
2729                }
2730            }
2731            // call this after the onAnimation is complete not to have inconsistent callbacks etc.
2732            if (smoothScroller != null && smoothScroller.isPendingInitialRun()) {
2733                smoothScroller.onAnimation(0, 0);
2734            }
2735            enableRunOnAnimationRequests();
2736        }
2737
2738        private void disableRunOnAnimationRequests() {
2739            mReSchedulePostAnimationCallback = false;
2740            mEatRunOnAnimationRequest = true;
2741        }
2742
2743        private void enableRunOnAnimationRequests() {
2744            mEatRunOnAnimationRequest = false;
2745            if (mReSchedulePostAnimationCallback) {
2746                postOnAnimation();
2747            }
2748        }
2749
2750        void postOnAnimation() {
2751            if (mEatRunOnAnimationRequest) {
2752                mReSchedulePostAnimationCallback = true;
2753            } else {
2754                ViewCompat.postOnAnimation(RecyclerView.this, this);
2755            }
2756        }
2757
2758        public void fling(int velocityX, int velocityY) {
2759            setScrollState(SCROLL_STATE_SETTLING);
2760            mLastFlingX = mLastFlingY = 0;
2761            mScroller.fling(0, 0, velocityX, velocityY,
2762                    Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
2763            postOnAnimation();
2764        }
2765
2766        public void smoothScrollBy(int dx, int dy) {
2767            smoothScrollBy(dx, dy, 0, 0);
2768        }
2769
2770        public void smoothScrollBy(int dx, int dy, int vx, int vy) {
2771            smoothScrollBy(dx, dy, computeScrollDuration(dx, dy, vx, vy));
2772        }
2773
2774        private float distanceInfluenceForSnapDuration(float f) {
2775            f -= 0.5f; // center the values about 0.
2776            f *= 0.3f * Math.PI / 2.0f;
2777            return (float) Math.sin(f);
2778        }
2779
2780        private int computeScrollDuration(int dx, int dy, int vx, int vy) {
2781            final int absDx = Math.abs(dx);
2782            final int absDy = Math.abs(dy);
2783            final boolean horizontal = absDx > absDy;
2784            final int velocity = (int) Math.sqrt(vx * vx + vy * vy);
2785            final int delta = (int) Math.sqrt(dx * dx + dy * dy);
2786            final int containerSize = horizontal ? getWidth() : getHeight();
2787            final int halfContainerSize = containerSize / 2;
2788            final float distanceRatio = Math.min(1.f, 1.f * delta / containerSize);
2789            final float distance = halfContainerSize + halfContainerSize *
2790                    distanceInfluenceForSnapDuration(distanceRatio);
2791
2792            final int duration;
2793            if (velocity > 0) {
2794                duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
2795            } else {
2796                float absDelta = (float) (horizontal ? absDx : absDy);
2797                duration = (int) (((absDelta / containerSize) + 1) * 300);
2798            }
2799            return Math.min(duration, MAX_SCROLL_DURATION);
2800        }
2801
2802        public void smoothScrollBy(int dx, int dy, int duration) {
2803            smoothScrollBy(dx, dy, duration, sQuinticInterpolator);
2804        }
2805
2806        public void smoothScrollBy(int dx, int dy, int duration, Interpolator interpolator) {
2807            if (mInterpolator != interpolator) {
2808                mInterpolator = interpolator;
2809                mScroller = ScrollerCompat.create(getContext(), interpolator);
2810            }
2811            setScrollState(SCROLL_STATE_SETTLING);
2812            mLastFlingX = mLastFlingY = 0;
2813            mScroller.startScroll(0, 0, dx, dy, duration);
2814            postOnAnimation();
2815        }
2816
2817        public void stop() {
2818            removeCallbacks(this);
2819            mScroller.abortAnimation();
2820        }
2821
2822    }
2823
2824    private class RecyclerViewDataObserver extends AdapterDataObserver {
2825        @Override
2826        public void onChanged() {
2827            assertNotInLayoutOrScroll(null);
2828            if (mAdapter.hasStableIds()) {
2829                // TODO Determine what actually changed.
2830                // This is more important to implement now since this callback will disable all
2831                // animations because we cannot rely on positions.
2832                mState.mStructureChanged = true;
2833                mDataSetHasChangedAfterLayout = true;
2834            } else {
2835                mState.mStructureChanged = true;
2836                mDataSetHasChangedAfterLayout = true;
2837            }
2838            if (!mAdapterHelper.hasPendingUpdates()) {
2839                requestLayout();
2840            }
2841        }
2842
2843        @Override
2844        public void onItemRangeChanged(int positionStart, int itemCount) {
2845            assertNotInLayoutOrScroll(null);
2846            if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount)) {
2847                triggerUpdateProcessor();
2848            }
2849        }
2850
2851        @Override
2852        public void onItemRangeInserted(int positionStart, int itemCount) {
2853            assertNotInLayoutOrScroll(null);
2854            if (mAdapterHelper.onItemRangeInserted(positionStart, itemCount)) {
2855                triggerUpdateProcessor();
2856            }
2857        }
2858
2859        @Override
2860        public void onItemRangeRemoved(int positionStart, int itemCount) {
2861            assertNotInLayoutOrScroll(null);
2862            if (mAdapterHelper.onItemRangeRemoved(positionStart, itemCount)) {
2863                triggerUpdateProcessor();
2864            }
2865        }
2866
2867        @Override
2868        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
2869            assertNotInLayoutOrScroll(null);
2870            if (mAdapterHelper.onItemRangeMoved(fromPosition, toPosition, itemCount)) {
2871                triggerUpdateProcessor();
2872            }
2873        }
2874
2875        void triggerUpdateProcessor() {
2876            if (mPostUpdatesOnAnimation && mHasFixedSize && mIsAttached) {
2877                ViewCompat.postOnAnimation(RecyclerView.this, mUpdateChildViewsRunnable);
2878            } else {
2879                mAdapterUpdateDuringMeasure = true;
2880                requestLayout();
2881            }
2882        }
2883    }
2884
2885    /**
2886     * RecycledViewPool lets you share Views between multiple RecyclerViews.
2887     * <p>
2888     * If you want to recycle views across RecyclerViews, create an instance of RecycledViewPool
2889     * and use {@link RecyclerView#setRecycledViewPool(RecycledViewPool)}.
2890     * <p>
2891     * RecyclerView automatically creates a pool for itself if you don't provide one.
2892     *
2893     */
2894    public static class RecycledViewPool {
2895        private SparseArray<ArrayList<ViewHolder>> mScrap =
2896                new SparseArray<ArrayList<ViewHolder>>();
2897        private SparseIntArray mMaxScrap = new SparseIntArray();
2898        private int mAttachCount = 0;
2899
2900        private static final int DEFAULT_MAX_SCRAP = 5;
2901
2902        public void clear() {
2903            mScrap.clear();
2904        }
2905
2906        public void setMaxRecycledViews(int viewType, int max) {
2907            mMaxScrap.put(viewType, max);
2908            final ArrayList<ViewHolder> scrapHeap = mScrap.get(viewType);
2909            if (scrapHeap != null) {
2910                while (scrapHeap.size() > max) {
2911                    scrapHeap.remove(scrapHeap.size() - 1);
2912                }
2913            }
2914        }
2915
2916        public ViewHolder getRecycledView(int viewType) {
2917            final ArrayList<ViewHolder> scrapHeap = mScrap.get(viewType);
2918            if (scrapHeap != null && !scrapHeap.isEmpty()) {
2919                final int index = scrapHeap.size() - 1;
2920                final ViewHolder scrap = scrapHeap.get(index);
2921                scrapHeap.remove(index);
2922                return scrap;
2923            }
2924            return null;
2925        }
2926
2927        int size() {
2928            int count = 0;
2929            for (int i = 0; i < mScrap.size(); i ++) {
2930                ArrayList<ViewHolder> viewHolders = mScrap.valueAt(i);
2931                if (viewHolders != null) {
2932                    count += viewHolders.size();
2933                }
2934            }
2935            return count;
2936        }
2937
2938        public void putRecycledView(ViewHolder scrap) {
2939            final int viewType = scrap.getItemViewType();
2940            final ArrayList scrapHeap = getScrapHeapForType(viewType);
2941            if (mMaxScrap.get(viewType) <= scrapHeap.size()) {
2942                return;
2943            }
2944            scrap.resetInternal();
2945            scrapHeap.add(scrap);
2946        }
2947
2948        void attach(Adapter adapter) {
2949            mAttachCount++;
2950        }
2951
2952        void detach() {
2953            mAttachCount--;
2954        }
2955
2956
2957        /**
2958         * Detaches the old adapter and attaches the new one.
2959         * <p>
2960         * RecycledViewPool will clear its cache if it has only one adapter attached and the new
2961         * adapter uses a different ViewHolder than the oldAdapter.
2962         *
2963         * @param oldAdapter The previous adapter instance. Will be detached.
2964         * @param newAdapter The new adapter instance. Will be attached.
2965         * @param compatibleWithPrevious True if both oldAdapter and newAdapter are using the same
2966         *                               ViewHolder and view types.
2967         */
2968        void onAdapterChanged(Adapter oldAdapter, Adapter newAdapter,
2969                boolean compatibleWithPrevious) {
2970            if (oldAdapter != null) {
2971                detach();
2972            }
2973            if (!compatibleWithPrevious && mAttachCount == 0) {
2974                clear();
2975            }
2976            if (newAdapter != null) {
2977                attach(newAdapter);
2978            }
2979        }
2980
2981        private ArrayList<ViewHolder> getScrapHeapForType(int viewType) {
2982            ArrayList<ViewHolder> scrap = mScrap.get(viewType);
2983            if (scrap == null) {
2984                scrap = new ArrayList<ViewHolder>();
2985                mScrap.put(viewType, scrap);
2986                if (mMaxScrap.indexOfKey(viewType) < 0) {
2987                    mMaxScrap.put(viewType, DEFAULT_MAX_SCRAP);
2988                }
2989            }
2990            return scrap;
2991        }
2992    }
2993
2994    /**
2995     * A Recycler is responsible for managing scrapped or detached item views for reuse.
2996     *
2997     * <p>A "scrapped" view is a view that is still attached to its parent RecyclerView but
2998     * that has been marked for removal or reuse.</p>
2999     *
3000     * <p>Typical use of a Recycler by a {@link LayoutManager} will be to obtain views for
3001     * an adapter's data set representing the data at a given position or item ID.
3002     * If the view to be reused is considered "dirty" the adapter will be asked to rebind it.
3003     * If not, the view can be quickly reused by the LayoutManager with no further work.
3004     * Clean views that have not {@link android.view.View#isLayoutRequested() requested layout}
3005     * may be repositioned by a LayoutManager without remeasurement.</p>
3006     */
3007    public final class Recycler {
3008        private final ArrayList<ViewHolder> mAttachedScrap = new ArrayList<ViewHolder>();
3009        private ArrayList<ViewHolder> mChangedScrap = null;
3010
3011        final ArrayList<ViewHolder> mCachedViews = new ArrayList<ViewHolder>();
3012
3013        private final List<ViewHolder>
3014                mUnmodifiableAttachedScrap = Collections.unmodifiableList(mAttachedScrap);
3015
3016        private int mViewCacheMax = DEFAULT_CACHE_SIZE;
3017
3018        private RecycledViewPool mRecyclerPool;
3019
3020        private ViewCacheExtension mViewCacheExtension;
3021
3022        private static final int DEFAULT_CACHE_SIZE = 2;
3023
3024        /**
3025         * Clear scrap views out of this recycler. Detached views contained within a
3026         * recycled view pool will remain.
3027         */
3028        public void clear() {
3029            mAttachedScrap.clear();
3030            recycleAndClearCachedViews();
3031        }
3032
3033        /**
3034         * Set the maximum number of detached, valid views we should retain for later use.
3035         *
3036         * @param viewCount Number of views to keep before sending views to the shared pool
3037         */
3038        public void setViewCacheSize(int viewCount) {
3039            mViewCacheMax = viewCount;
3040            // first, try the views that can be recycled
3041            for (int i = mCachedViews.size() - 1; i >= 0 && mCachedViews.size() > viewCount; i--) {
3042                tryToRecycleCachedViewAt(i);
3043            }
3044            // if we could not recycle enough of them, remove some.
3045            while (mCachedViews.size() > viewCount) {
3046                mCachedViews.remove(mCachedViews.size() - 1);
3047            }
3048        }
3049
3050        /**
3051         * Returns an unmodifiable list of ViewHolders that are currently in the scrap list.
3052         *
3053         * @return List of ViewHolders in the scrap list.
3054         */
3055        public List<ViewHolder> getScrapList() {
3056            return mUnmodifiableAttachedScrap;
3057        }
3058
3059        /**
3060         * Helper method for getViewForPosition.
3061         * <p>
3062         * Checks whether a given view holder can be used for the provided position.
3063         *
3064         * @param holder ViewHolder
3065         * @return true if ViewHolder matches the provided position, false otherwise
3066         */
3067        boolean validateViewHolderForOffsetPosition(ViewHolder holder) {
3068            // if it is a removed holder, nothing to verify since we cannot ask adapter anymore
3069            // if it is not removed, verify the type and id.
3070            if (holder.isRemoved()) {
3071                return true;
3072            }
3073            if (holder.mPosition < 0 || holder.mPosition >= mAdapter.getItemCount()) {
3074                throw new IndexOutOfBoundsException("Inconsistency detected. Invalid view holder "
3075                        + "adapter position" + holder);
3076            }
3077            if (!mState.isPreLayout()) {
3078                // don't check type if it is pre-layout.
3079                final int type = mAdapter.getItemViewType(holder.mPosition);
3080                if (type != holder.getItemViewType()) {
3081                    return false;
3082                }
3083            }
3084            if (mAdapter.hasStableIds()) {
3085                return holder.getItemId() == mAdapter.getItemId(holder.mPosition);
3086            }
3087            return true;
3088        }
3089
3090        /**
3091         * Binds the given View to the position. The View can be a View previously retrieved via
3092         * {@link #getViewForPosition(int)} or created by
3093         * {@link Adapter#onCreateViewHolder(ViewGroup, int)}.
3094         * <p>
3095         * Generally, a LayoutManager should acquire its views via {@link #getViewForPosition(int)}
3096         * and let the RecyclerView handle caching. This is a helper method for LayoutManager who
3097         * wants to handle its own recycling logic.
3098         * <p>
3099         * Note that, {@link #getViewForPosition(int)} already binds the View to the position so
3100         * you don't need to call this method unless you want to bind this View to another position.
3101         *
3102         * @param view The view to update.
3103         * @param position The position of the item to bind to this View.
3104         */
3105        public void bindViewToPosition(View view, int position) {
3106            ViewHolder holder = getChildViewHolderInt(view);
3107            if (holder == null) {
3108                throw new IllegalArgumentException("The view does not have a ViewHolder. You cannot"
3109                        + " pass arbitrary views to this method, they should be created by the "
3110                        + "Adapter");
3111            }
3112            final int offsetPosition = mAdapterHelper.findPositionOffset(position);
3113            if (offsetPosition < 0 || offsetPosition >= mAdapter.getItemCount()) {
3114                throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item "
3115                        + "position " + position + "(offset:" + offsetPosition + ")."
3116                        + "state:" + mState.getItemCount());
3117            }
3118            mAdapter.bindViewHolder(holder, offsetPosition);
3119            if (mState.isPreLayout()) {
3120                holder.mPreLayoutPosition = position;
3121            }
3122
3123            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
3124            if (lp == null) {
3125                lp = generateDefaultLayoutParams();
3126                holder.itemView.setLayoutParams(lp);
3127            } else if (!checkLayoutParams(lp)) {
3128                lp = generateLayoutParams(lp);
3129                holder.itemView.setLayoutParams(lp);
3130            }
3131            ((LayoutParams) lp).mInsetsDirty = true;
3132            ((LayoutParams) lp).mViewHolder = holder;
3133        }
3134
3135        /**
3136         * Obtain a view initialized for the given position.
3137         *
3138         * This method should be used by {@link LayoutManager} implementations to obtain
3139         * views to represent data from an {@link Adapter}.
3140         * <p>
3141         * The Recycler may reuse a scrap or detached view from a shared pool if one is
3142         * available for the correct view type. If the adapter has not indicated that the
3143         * data at the given position has changed, the Recycler will attempt to hand back
3144         * a scrap view that was previously initialized for that data without rebinding.
3145         *
3146         * @param position Position to obtain a view for
3147         * @return A view representing the data at <code>position</code> from <code>adapter</code>
3148         */
3149        public View getViewForPosition(int position) {
3150            return getViewForPosition(position, false);
3151        }
3152
3153        View getViewForPosition(int position, boolean dryRun) {
3154            if (position < 0 || position >= mState.getItemCount()) {
3155                throw new IndexOutOfBoundsException("Invalid item position " + position
3156                        + "(" + position + "). Item count:" + mState.getItemCount());
3157            }
3158            ViewHolder holder = null;
3159            // 0) If there is a changed scrap, try to find from there
3160            if (mState.isPreLayout()) {
3161                holder = getChangedScrapViewForPosition(position);
3162            }
3163            // 1) Find from scrap by position
3164            if (holder == null) {
3165                holder = getScrapViewForPosition(position, INVALID_TYPE, dryRun);
3166                if (holder != null) {
3167                    if (!validateViewHolderForOffsetPosition(holder)) {
3168                        // recycle this scrap
3169                        if (!dryRun) {
3170                            // we would like to recycle this but need to make sure it is not used by
3171                            // animation logic etc.
3172                            holder.addFlags(ViewHolder.FLAG_INVALID);
3173                            if (holder.isScrap()) {
3174                                removeDetachedView(holder.itemView, false);
3175                                holder.unScrap();
3176                            } else if (holder.wasReturnedFromScrap()) {
3177                                holder.clearReturnedFromScrapFlag();
3178                            }
3179                            recycleViewHolderInternal(holder);
3180                        }
3181                        holder = null;
3182                    }
3183                }
3184            }
3185            if (holder == null) {
3186                final int offsetPosition = mAdapterHelper.findPositionOffset(position);
3187                if (offsetPosition < 0 || offsetPosition >= mAdapter.getItemCount()) {
3188                    throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item "
3189                            + "position " + position + "(offset:" + offsetPosition + ")."
3190                            + "state:" + mState.getItemCount());
3191                }
3192
3193                final int type = mAdapter.getItemViewType(offsetPosition);
3194                // 2) Find from scrap via stable ids, if exists
3195                if (mAdapter.hasStableIds()) {
3196                    holder = getScrapViewForId(mAdapter.getItemId(offsetPosition), type, dryRun);
3197                    if (holder != null) {
3198                        // update position
3199                        holder.mPosition = offsetPosition;
3200                    }
3201                }
3202                if (holder == null && mViewCacheExtension != null) {
3203                    // We are NOT sending the offsetPosition because LayoutManager does not
3204                    // know it.
3205                    final View view = mViewCacheExtension
3206                            .getViewForPositionAndType(this, position, type);
3207                    if (view != null) {
3208                        holder = getChildViewHolder(view);
3209                        if (holder == null) {
3210                            throw new IllegalArgumentException("getViewForPositionAndType returned"
3211                                    + " a view which does not have a ViewHolder");
3212                        } else if (holder.shouldIgnore()) {
3213                            throw new IllegalArgumentException("getViewForPositionAndType returned"
3214                                    + " a view that is ignored. You must call stopIgnoring before"
3215                                    + " returning this view.");
3216                        }
3217                    }
3218                }
3219                if (holder == null) { // fallback to recycler
3220                    // try recycler.
3221                    // Head to the shared pool.
3222                    if (DEBUG) {
3223                        Log.d(TAG, "getViewForPosition(" + position + ") fetching from shared "
3224                                + "pool");
3225                    }
3226                    holder = getRecycledViewPool()
3227                            .getRecycledView(mAdapter.getItemViewType(offsetPosition));
3228                    if (holder != null) {
3229                        holder.resetInternal();
3230                        if (FORCE_INVALIDATE_DISPLAY_LIST) {
3231                            invalidateDisplayListInt(holder);
3232                        }
3233                    }
3234                }
3235                if (holder == null) {
3236                    holder = mAdapter.createViewHolder(RecyclerView.this,
3237                            mAdapter.getItemViewType(offsetPosition));
3238                    if (DEBUG) {
3239                        Log.d(TAG, "getViewForPosition created new ViewHolder");
3240                    }
3241                }
3242            }
3243
3244            if (mState.isPreLayout() && holder.isBound()) {
3245                // do not update unless we absolutely have to.
3246                holder.mPreLayoutPosition = position;
3247            } else if (!holder.isBound() || holder.needsUpdate() || holder.isInvalid()) {
3248                if (DEBUG && holder.isRemoved()) {
3249                    throw new IllegalStateException("Removed holder should be bound and it should"
3250                            + " come here only in pre-layout. Holder: " + holder);
3251                }
3252                final int offsetPosition = mAdapterHelper.findPositionOffset(position);
3253                mAdapter.bindViewHolder(holder, offsetPosition);
3254                if (mState.isPreLayout()) {
3255                    holder.mPreLayoutPosition = position;
3256                }
3257            }
3258
3259            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
3260            if (lp == null) {
3261                lp = generateDefaultLayoutParams();
3262                holder.itemView.setLayoutParams(lp);
3263            } else if (!checkLayoutParams(lp)) {
3264                lp = generateLayoutParams(lp);
3265                holder.itemView.setLayoutParams(lp);
3266            }
3267            ((LayoutParams) lp).mViewHolder = holder;
3268
3269            return holder.itemView;
3270        }
3271
3272        private void invalidateDisplayListInt(ViewHolder holder) {
3273            if (holder.itemView instanceof ViewGroup) {
3274                invalidateDisplayListInt((ViewGroup) holder.itemView, false);
3275            }
3276        }
3277
3278        private void invalidateDisplayListInt(ViewGroup viewGroup, boolean invalidateThis) {
3279            for (int i = viewGroup.getChildCount() - 1; i >= 0; i--) {
3280                final View view = viewGroup.getChildAt(i);
3281                if (view instanceof ViewGroup) {
3282                    invalidateDisplayListInt((ViewGroup) view, true);
3283                }
3284            }
3285            if (!invalidateThis) {
3286                return;
3287            }
3288            // we need to force it to become invisible
3289            if (viewGroup.getVisibility() == View.INVISIBLE) {
3290                viewGroup.setVisibility(View.VISIBLE);
3291                viewGroup.setVisibility(View.INVISIBLE);
3292            } else {
3293                final int visibility = viewGroup.getVisibility();
3294                viewGroup.setVisibility(View.INVISIBLE);
3295                viewGroup.setVisibility(visibility);
3296            }
3297        }
3298
3299        /**
3300         * Recycle a detached view. The specified view will be added to a pool of views
3301         * for later rebinding and reuse.
3302         *
3303         * <p>A view must be fully detached before it may be recycled. If the View is scrapped,
3304         * it will be removed from scrap list.</p>
3305         *
3306         * @param view Removed view for recycling
3307         * @see LayoutManager#removeAndRecycleView(View, Recycler)
3308         */
3309        public void recycleView(View view) {
3310            // This public recycle method tries to make view recycle-able since layout manager
3311            // intended to recycle this view (e.g. even if it is in scrap or change cache)
3312            ViewHolder holder = getChildViewHolderInt(view);
3313            if (holder.isScrap()) {
3314                holder.unScrap();
3315            } else if (holder.wasReturnedFromScrap()){
3316                holder.clearReturnedFromScrapFlag();
3317            }
3318            recycleViewHolderInternal(holder);
3319        }
3320
3321        /**
3322         * Internally, use this method instead of {@link #recycleView(android.view.View)} to
3323         * catch potential bugs.
3324         * @param view
3325         */
3326        void recycleViewInternal(View view) {
3327            recycleViewHolderInternal(getChildViewHolderInt(view));
3328        }
3329
3330        void recycleAndClearCachedViews() {
3331            final int count = mCachedViews.size();
3332            for (int i = count - 1; i >= 0; i--) {
3333                tryToRecycleCachedViewAt(i);
3334            }
3335            mCachedViews.clear();
3336        }
3337
3338        /**
3339         * Tries to recyle a cached view and removes the view from the list if and only if it
3340         * is recycled.
3341         *
3342         * @param cachedViewIndex The index of the view in cached views list
3343         * @return True if item is recycled
3344         */
3345        boolean tryToRecycleCachedViewAt(int cachedViewIndex) {
3346            if (DEBUG) {
3347                Log.d(TAG, "Recycling cached view at index " + cachedViewIndex);
3348            }
3349            ViewHolder viewHolder = mCachedViews.get(cachedViewIndex);
3350            if (DEBUG) {
3351                Log.d(TAG, "CachedViewHolder to be recycled(if recycleable): " + viewHolder);
3352            }
3353            if (viewHolder.isRecyclable()) {
3354                getRecycledViewPool().putRecycledView(viewHolder);
3355                dispatchViewRecycled(viewHolder);
3356                mCachedViews.remove(cachedViewIndex);
3357                return true;
3358            }
3359            return false;
3360        }
3361
3362        /**
3363         * internal implementation checks if view is scrapped or attached and throws an exception
3364         * if so.
3365         * Public version un-scraps before calling recycle.
3366         */
3367        void recycleViewHolderInternal(ViewHolder holder) {
3368            if (holder.isScrap() || holder.itemView.getParent() != null) {
3369                throw new IllegalArgumentException(
3370                        "Scrapped or attached views may not be recycled. isScrap:"
3371                                + holder.isScrap() + " isAttached:"
3372                                + (holder.itemView.getParent() != null));
3373            }
3374
3375            if (holder.shouldIgnore()) {
3376                throw new IllegalArgumentException("Trying to recycle an ignored view holder. You"
3377                        + " should first call stopIgnoringView(view) before calling recycle.");
3378            }
3379            if (holder.isRecyclable()) {
3380                boolean cached = false;
3381                if (!holder.isInvalid() && (mState.mInPreLayout || !holder.isRemoved()) &&
3382                        !holder.isChanged()) {
3383                    // Retire oldest cached views first
3384                    if (mCachedViews.size() == mViewCacheMax && !mCachedViews.isEmpty()) {
3385                        for (int i = 0; i < mCachedViews.size(); i++) {
3386                            if (tryToRecycleCachedViewAt(i)) {
3387                                break;
3388                            }
3389                        }
3390                    }
3391                    if (mCachedViews.size() < mViewCacheMax) {
3392                        mCachedViews.add(holder);
3393                        cached = true;
3394                    }
3395                }
3396                if (!cached) {
3397                    getRecycledViewPool().putRecycledView(holder);
3398                    dispatchViewRecycled(holder);
3399                }
3400            } else if (DEBUG) {
3401                Log.d(TAG, "trying to recycle a non-recycleable holder. Hopefully, it will "
3402                        + "re-visit here. We are stil removing it from animation lists");
3403            }
3404            // even if the holder is not removed, we still call this method so that it is removed
3405            // from view holder lists.
3406            mState.onViewRecycled(holder);
3407        }
3408
3409        /**
3410         * Used as a fast path for unscrapping and recycling a view during a bulk operation.
3411         * The caller must call {@link #clearScrap()} when it's done to update the recycler's
3412         * internal bookkeeping.
3413         */
3414        void quickRecycleScrapView(View view) {
3415            final ViewHolder holder = getChildViewHolderInt(view);
3416            holder.mScrapContainer = null;
3417            holder.clearReturnedFromScrapFlag();
3418            recycleViewHolderInternal(holder);
3419        }
3420
3421        /**
3422         * Mark an attached view as scrap.
3423         *
3424         * <p>"Scrap" views are still attached to their parent RecyclerView but are eligible
3425         * for rebinding and reuse. Requests for a view for a given position may return a
3426         * reused or rebound scrap view instance.</p>
3427         *
3428         * @param view View to scrap
3429         */
3430        void scrapView(View view) {
3431            final ViewHolder holder = getChildViewHolderInt(view);
3432            holder.setScrapContainer(this);
3433            if (!holder.isChanged() || !supportsChangeAnimations()) {
3434                if (holder.isInvalid() && !holder.isRemoved() && !mAdapter.hasStableIds()) {
3435                    throw new IllegalArgumentException("Called scrap view with an invalid view."
3436                            + " Invalid views cannot be reused from scrap, they should rebound from"
3437                            + " recycler pool.");
3438                }
3439                mAttachedScrap.add(holder);
3440            } else {
3441                if (mChangedScrap == null) {
3442                    mChangedScrap = new ArrayList<ViewHolder>();
3443                }
3444                mChangedScrap.add(holder);
3445            }
3446        }
3447
3448        /**
3449         * Remove a previously scrapped view from the pool of eligible scrap.
3450         *
3451         * <p>This view will no longer be eligible for reuse until re-scrapped or
3452         * until it is explicitly removed and recycled.</p>
3453         */
3454        void unscrapView(ViewHolder holder) {
3455            if (!holder.isChanged() || !supportsChangeAnimations() || mChangedScrap == null) {
3456                mAttachedScrap.remove(holder);
3457            } else {
3458                mChangedScrap.remove(holder);
3459            }
3460            holder.mScrapContainer = null;
3461            holder.clearReturnedFromScrapFlag();
3462        }
3463
3464        int getScrapCount() {
3465            return mAttachedScrap.size();
3466        }
3467
3468        View getScrapViewAt(int index) {
3469            return mAttachedScrap.get(index).itemView;
3470        }
3471
3472        void clearScrap() {
3473            mAttachedScrap.clear();
3474        }
3475
3476        ViewHolder getChangedScrapViewForPosition(int position) {
3477            // If pre-layout, check the changed scrap for an exact match.
3478            final int changedScrapSize;
3479            if (mChangedScrap == null || (changedScrapSize = mChangedScrap.size()) == 0) {
3480                return null;
3481            }
3482            // find by position
3483            for (int i = 0; i < changedScrapSize; i++) {
3484                final ViewHolder holder = mChangedScrap.get(i);
3485                if (!holder.wasReturnedFromScrap() && holder.getPosition() == position) {
3486                    holder.addFlags(ViewHolder.FLAG_RETURNED_FROM_SCRAP);
3487                    return holder;
3488                }
3489            }
3490            // find by id
3491            if (mAdapter.hasStableIds()) {
3492                final int offsetPosition = mAdapterHelper.findPositionOffset(position);
3493                if (offsetPosition > 0 && offsetPosition < mAdapter.getItemCount()) {
3494                    final long id = mAdapter.getItemId(offsetPosition);
3495                    for (int i = 0; i < changedScrapSize; i++) {
3496                        final ViewHolder holder = mChangedScrap.get(i);
3497                        if (!holder.wasReturnedFromScrap() && holder.getItemId() == id) {
3498                            holder.addFlags(ViewHolder.FLAG_RETURNED_FROM_SCRAP);
3499                            return holder;
3500                        }
3501                    }
3502                }
3503            }
3504            return null;
3505        }
3506
3507        /**
3508         * Returns a scrap view for the position. If type is not INVALID_TYPE, it also checks if
3509         * ViewHolder's type matches the provided type.
3510         *
3511         * @param position Item position
3512         * @param type View type
3513         * @param dryRun  Does a dry run, finds the ViewHolder but does not remove
3514         * @return a ViewHolder that can be re-used for this position.
3515         */
3516        ViewHolder getScrapViewForPosition(int position, int type, boolean dryRun) {
3517            final int scrapCount = mAttachedScrap.size();
3518
3519            // Try first for an exact, non-invalid match from scrap.
3520            for (int i = 0; i < scrapCount; i++) {
3521                final ViewHolder holder = mAttachedScrap.get(i);
3522                if (!holder.wasReturnedFromScrap() && holder.getPosition() == position
3523                        && !holder.isInvalid() && (mState.mInPreLayout || !holder.isRemoved())) {
3524                    if (type != INVALID_TYPE && holder.getItemViewType() != type) {
3525                        Log.e(TAG, "Scrap view for position " + position + " isn't dirty but has" +
3526                                " wrong view type! (found " + holder.getItemViewType() +
3527                                " but expected " + type + ")");
3528                        break;
3529                    }
3530                    holder.addFlags(ViewHolder.FLAG_RETURNED_FROM_SCRAP);
3531                    return holder;
3532                }
3533            }
3534
3535            if (!dryRun) {
3536                View view = mChildHelper.findHiddenNonRemovedView(position, type);
3537                if (view != null) {
3538                    // ending the animation should cause it to get recycled before we reuse it
3539                    mItemAnimator.endAnimation(getChildViewHolder(view));
3540                }
3541            }
3542
3543            // Search in our first-level recycled view cache.
3544            final int cacheSize = mCachedViews.size();
3545            for (int i = 0; i < cacheSize; i++) {
3546                final ViewHolder holder = mCachedViews.get(i);
3547                // invalid view holders may be in cache if adapter has stable ids as they can be
3548                // retrieved via getScrapViewForId
3549                if (!holder.isInvalid() && holder.getPosition() == position) {
3550                    if (!dryRun) {
3551                        mCachedViews.remove(i);
3552                    }
3553                    if (DEBUG) {
3554                        Log.d(TAG, "getScrapViewForPosition(" + position + ", " + type +
3555                                ") found match in cache: " + holder);
3556                    }
3557                    return holder;
3558                }
3559            }
3560            return null;
3561        }
3562
3563        ViewHolder getScrapViewForId(long id, int type, boolean dryRun) {
3564            // Look in our attached views first
3565            final int count = mAttachedScrap.size();
3566            for (int i = count - 1; i >= 0; i--) {
3567                final ViewHolder holder = mAttachedScrap.get(i);
3568                if (holder.getItemId() == id && !holder.wasReturnedFromScrap()) {
3569                    if (type == holder.getItemViewType()) {
3570                        holder.addFlags(ViewHolder.FLAG_RETURNED_FROM_SCRAP);
3571                        if (holder.isRemoved()) {
3572                            // this might be valid in two cases:
3573                            // > item is removed but we are in pre-layout pass
3574                            // >> do nothing. return as is. make sure we don't rebind
3575                            // > item is removed then added to another position and we are in
3576                            // post layout.
3577                            // >> remove removed and invalid flags, add update flag to rebind
3578                            // because item was invisible to us and we don't know what happened in
3579                            // between.
3580                            if (!mState.isPreLayout()) {
3581                                holder.setFlags(ViewHolder.FLAG_UPDATE, ViewHolder.FLAG_UPDATE |
3582                                        ViewHolder.FLAG_INVALID | ViewHolder.FLAG_REMOVED);
3583                            }
3584                        }
3585                        return holder;
3586                    } else if (!dryRun) {
3587                        // Recycle this scrap. Type mismatch.
3588                        mAttachedScrap.remove(i);
3589                        removeDetachedView(holder.itemView, false);
3590                        quickRecycleScrapView(holder.itemView);
3591                    }
3592                }
3593            }
3594
3595            // Search the first-level cache
3596            final int cacheSize = mCachedViews.size();
3597            for (int i = cacheSize - 1; i >= 0; i--) {
3598                final ViewHolder holder = mCachedViews.get(i);
3599                if (holder.getItemId() == id) {
3600                    if (type == holder.getItemViewType()) {
3601                        if (!dryRun) {
3602                            mCachedViews.remove(i);
3603                        }
3604                        return holder;
3605                    } else if (!dryRun) {
3606                        tryToRecycleCachedViewAt(i);
3607                    }
3608                }
3609            }
3610            return null;
3611        }
3612
3613        void dispatchViewRecycled(ViewHolder holder) {
3614            if (mRecyclerListener != null) {
3615                mRecyclerListener.onViewRecycled(holder);
3616            }
3617            if (mAdapter != null) {
3618                mAdapter.onViewRecycled(holder);
3619            }
3620            if (mState != null) {
3621                mState.onViewRecycled(holder);
3622            }
3623            if (DEBUG) Log.d(TAG, "dispatchViewRecycled: " + holder);
3624        }
3625
3626        void onAdapterChanged(Adapter oldAdapter, Adapter newAdapter,
3627                boolean compatibleWithPrevious) {
3628            clear();
3629            getRecycledViewPool().onAdapterChanged(oldAdapter, newAdapter, compatibleWithPrevious);
3630        }
3631
3632        void offsetPositionRecordsForMove(int from, int to) {
3633            final int start, end, inBetweenOffset;
3634            if (from < to) {
3635                start = from;
3636                end = to;
3637                inBetweenOffset = -1;
3638            } else {
3639                start = to;
3640                end = from;
3641                inBetweenOffset = 1;
3642            }
3643            final int cachedCount = mCachedViews.size();
3644            for (int i = 0; i < cachedCount; i++) {
3645                final ViewHolder holder = mCachedViews.get(i);
3646                if (holder == null || holder.mPosition < start || holder.mPosition > end) {
3647                    continue;
3648                }
3649                if (holder.mPosition == from) {
3650                    holder.offsetPosition(to - from, false);
3651                } else {
3652                    holder.offsetPosition(inBetweenOffset, false);
3653                }
3654                if (DEBUG) {
3655                    Log.d(TAG, "offsetPositionRecordsForMove cached child " + i + " holder " +
3656                            holder);
3657                }
3658            }
3659        }
3660
3661        void offsetPositionRecordsForInsert(int insertedAt, int count) {
3662            final int cachedCount = mCachedViews.size();
3663            for (int i = 0; i < cachedCount; i++) {
3664                final ViewHolder holder = mCachedViews.get(i);
3665                if (holder != null && holder.getPosition() >= insertedAt) {
3666                    if (DEBUG) {
3667                        Log.d(TAG, "offsetPositionRecordsForInsert cached " + i + " holder " +
3668                                holder + " now at position " + (holder.mPosition + count));
3669                    }
3670                    holder.offsetPosition(count, true);
3671                }
3672            }
3673        }
3674
3675        /**
3676         * @param removedFrom Remove start index
3677         * @param count Remove count
3678         * @param applyToPreLayout If true, changes will affect ViewHolder's pre-layout position, if
3679         *                         false, they'll be applied before the second layout pass
3680         */
3681        void offsetPositionRecordsForRemove(int removedFrom, int count, boolean applyToPreLayout) {
3682            final int removedEnd = removedFrom + count;
3683            final int cachedCount = mCachedViews.size();
3684            for (int i = cachedCount - 1; i >= 0; i--) {
3685                final ViewHolder holder = mCachedViews.get(i);
3686                if (holder != null) {
3687                    if (holder.getPosition() >= removedEnd) {
3688                        if (DEBUG) {
3689                            Log.d(TAG, "offsetPositionRecordsForRemove cached " + i +
3690                                    " holder " + holder + " now at position " +
3691                                    (holder.mPosition - count));
3692                        }
3693                        holder.offsetPosition(-count, applyToPreLayout);
3694                    } else if (holder.getPosition() >= removedFrom) {
3695                        // Item for this view was removed. Dump it from the cache.
3696                        if (!tryToRecycleCachedViewAt(i)) {
3697                            // if we cannot recycle it, at least invalidate so that we won't return
3698                            // it by position.
3699                            holder.addFlags(ViewHolder.FLAG_INVALID);
3700                            if (DEBUG) {
3701                                Log.d(TAG, "offsetPositionRecordsForRemove cached " + i +
3702                                        " holder " + holder + " now flagged as invalid because it "
3703                                        + "could not be recycled");
3704                            }
3705                        } else if (DEBUG) {
3706                            Log.d(TAG, "offsetPositionRecordsForRemove cached " + i +
3707                                    " holder " + holder + " now placed in pool");
3708                        }
3709                    }
3710                }
3711            }
3712        }
3713
3714        void setViewCacheExtension(ViewCacheExtension extension) {
3715            mViewCacheExtension = extension;
3716        }
3717
3718        void setRecycledViewPool(RecycledViewPool pool) {
3719            if (mRecyclerPool != null) {
3720                mRecyclerPool.detach();
3721            }
3722            mRecyclerPool = pool;
3723            if (pool != null) {
3724                mRecyclerPool.attach(getAdapter());
3725            }
3726        }
3727
3728        RecycledViewPool getRecycledViewPool() {
3729            if (mRecyclerPool == null) {
3730                mRecyclerPool = new RecycledViewPool();
3731            }
3732            return mRecyclerPool;
3733        }
3734
3735        void viewRangeUpdate(int positionStart, int itemCount) {
3736            final int positionEnd = positionStart + itemCount;
3737            final int cachedCount = mCachedViews.size();
3738            for (int i = 0; i < cachedCount; i++) {
3739                final ViewHolder holder = mCachedViews.get(i);
3740                if (holder == null) {
3741                    continue;
3742                }
3743
3744                final int pos = holder.getPosition();
3745                if (pos >= positionStart && pos < positionEnd) {
3746                    holder.addFlags(ViewHolder.FLAG_UPDATE);
3747                    if (supportsChangeAnimations()) {
3748                        holder.addFlags(ViewHolder.FLAG_CHANGED);
3749                    }
3750                }
3751            }
3752        }
3753
3754        void markKnownViewsInvalid() {
3755            if (mAdapter != null && mAdapter.hasStableIds()) {
3756                final int cachedCount = mCachedViews.size();
3757                for (int i = 0; i < cachedCount; i++) {
3758                    final ViewHolder holder = mCachedViews.get(i);
3759                    if (holder != null) {
3760                        holder.addFlags(ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID);
3761                    }
3762                }
3763            } else {
3764                // we cannot re-use cached views in this case. Recycle the ones we can and flag
3765                // the remaining as invalid so that they can be recycled later on (when their
3766                // animations end.)
3767                for (int i = mCachedViews.size() - 1; i >= 0; i--) {
3768                    if (!tryToRecycleCachedViewAt(i)) {
3769                        final ViewHolder holder = mCachedViews.get(i);
3770                        holder.addFlags(ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID);
3771                    }
3772                }
3773            }
3774
3775        }
3776
3777        void clearOldPositions() {
3778            final int cachedCount = mCachedViews.size();
3779            for (int i = 0; i < cachedCount; i++) {
3780                final ViewHolder holder = mCachedViews.get(i);
3781                holder.clearOldPosition();
3782            }
3783        }
3784
3785        void markItemDecorInsetsDirty() {
3786            final int cachedCount = mCachedViews.size();
3787            for (int i = 0; i < cachedCount; i++) {
3788                final ViewHolder holder = mCachedViews.get(i);
3789                LayoutParams layoutParams = (LayoutParams) holder.itemView.getLayoutParams();
3790                if (layoutParams != null) {
3791                    layoutParams.mInsetsDirty = true;
3792                }
3793            }
3794        }
3795    }
3796
3797    /**
3798     * ViewCacheExtension is a helper class to provide an additional layer of view caching that can
3799     * ben controlled by the developer.
3800     * <p>
3801     * When {@link Recycler#getViewForPosition(int)} is called, Recycler checks attached scrap and
3802     * first level cache to find a matching View. If it cannot find a suitable View, Recycler will
3803     * call the {@link #getViewForPositionAndType(Recycler, int, int)} before checking
3804     * {@link RecycledViewPool}.
3805     * <p>
3806     * Note that, Recycler never sends Views to this method to be cached. It is developers
3807     * responsibility to decide whether they want to keep their Views in this custom cache or let
3808     * the default recycling policy handle it.
3809     */
3810    public abstract static class ViewCacheExtension {
3811
3812        /**
3813         * Returns a View that can be binded to the given Adapter position.
3814         * <p>
3815         * This method should <b>not</b> create a new View. Instead, it is expected to return
3816         * an already created View that can be re-used for the given type and position.
3817         * If the View is marked as ignored, it should first call
3818         * {@link LayoutManager#stopIgnoringView(View)} before returning the View.
3819         * <p>
3820         * RecyclerView will re-bind the returned View to the position if necessary.
3821         *
3822         * @param recycler The Recycler that can be used to bind the View
3823         * @param position The adapter position
3824         * @param type     The type of the View, defined by adapter
3825         * @return A View that is bound to the given position or NULL if there is no View to re-use
3826         * @see LayoutManager#ignoreView(View)
3827         */
3828        abstract public View getViewForPositionAndType(Recycler recycler, int position, int type);
3829    }
3830
3831    /**
3832     * Base class for an Adapter
3833     *
3834     * <p>Adapters provide a binding from an app-specific data set to views that are displayed
3835     * within a {@link RecyclerView}.</p>
3836     */
3837    public static abstract class Adapter<VH extends ViewHolder> {
3838        private final AdapterDataObservable mObservable = new AdapterDataObservable();
3839        private boolean mHasStableIds = false;
3840
3841        public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
3842        public abstract void onBindViewHolder(VH holder, int position);
3843
3844        public final VH createViewHolder(ViewGroup parent, int viewType) {
3845            final VH holder = onCreateViewHolder(parent, viewType);
3846            holder.mItemViewType = viewType;
3847            return holder;
3848        }
3849
3850        public final void bindViewHolder(VH holder, int position) {
3851            holder.mPosition = position;
3852            if (hasStableIds()) {
3853                holder.mItemId = getItemId(position);
3854            }
3855            onBindViewHolder(holder, position);
3856            holder.setFlags(ViewHolder.FLAG_BOUND,
3857                    ViewHolder.FLAG_BOUND | ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID);
3858        }
3859
3860        /**
3861         * Return the view type of the item at <code>position</code> for the purposes
3862         * of view recycling.
3863         *
3864         * <p>The default implementation of this method returns 0, making the assumption of
3865         * a single view type for the adapter. Unlike ListView adapters, types need not
3866         * be contiguous. Consider using id resources to uniquely identify item view types.
3867         *
3868         * @param position position to query
3869         * @return integer value identifying the type of the view needed to represent the item at
3870         *                 <code>position</code>. Type codes need not be contiguous.
3871         */
3872        public int getItemViewType(int position) {
3873            return 0;
3874        }
3875
3876        public void setHasStableIds(boolean hasStableIds) {
3877            if (hasObservers()) {
3878                throw new IllegalStateException("Cannot change whether this adapter has " +
3879                        "stable IDs while the adapter has registered observers.");
3880            }
3881            mHasStableIds = hasStableIds;
3882        }
3883
3884        /**
3885         * Return the stable ID for the item at <code>position</code>. If {@link #hasStableIds()}
3886         * would return false this method should return {@link #NO_ID}. The default implementation
3887         * of this method returns {@link #NO_ID}.
3888         *
3889         * @param position Adapter position to query
3890         * @return the stable ID of the item at position
3891         */
3892        public long getItemId(int position) {
3893            return NO_ID;
3894        }
3895
3896        public abstract int getItemCount();
3897
3898        /**
3899         * Returns true if this adapter publishes a unique <code>long</code> value that can
3900         * act as a key for the item at a given position in the data set. If that item is relocated
3901         * in the data set, the ID returned for that item should be the same.
3902         *
3903         * @return true if this adapter's items have stable IDs
3904         */
3905        public final boolean hasStableIds() {
3906            return mHasStableIds;
3907        }
3908
3909        /**
3910         * Called when a view created by this adapter has been recycled.
3911         *
3912         * <p>A view is recycled when a {@link LayoutManager} decides that it no longer
3913         * needs to be attached to its parent {@link RecyclerView}. This can be because it has
3914         * fallen out of visibility or a set of cached views represented by views still
3915         * attached to the parent RecyclerView. If an item view has large or expensive data
3916         * bound to it such as large bitmaps, this may be a good place to release those
3917         * resources.</p>
3918         *
3919         * @param holder The ViewHolder for the view being recycled
3920         */
3921        public void onViewRecycled(VH holder) {
3922        }
3923
3924        /**
3925         * Called when a view created by this adapter has been attached to a window.
3926         *
3927         * <p>This can be used as a reasonable signal that the view is about to be seen
3928         * by the user. If the adapter previously freed any resources in
3929         * {@link #onViewDetachedFromWindow(RecyclerView.ViewHolder) onViewDetachedFromWindow}
3930         * those resources should be restored here.</p>
3931         *
3932         * @param holder Holder of the view being attached
3933         */
3934        public void onViewAttachedToWindow(VH holder) {
3935        }
3936
3937        /**
3938         * Called when a view created by this adapter has been detached from its window.
3939         *
3940         * <p>Becoming detached from the window is not necessarily a permanent condition;
3941         * the consumer of an Adapter's views may choose to cache views offscreen while they
3942         * are not visible, attaching an detaching them as appropriate.</p>
3943         *
3944         * @param holder Holder of the view being detached
3945         */
3946        public void onViewDetachedFromWindow(VH holder) {
3947        }
3948
3949        /**
3950         * Returns true if one or more observers are attached to this adapter.
3951         * @return true if this adapter has observers
3952         */
3953        public final boolean hasObservers() {
3954            return mObservable.hasObservers();
3955        }
3956
3957        /**
3958         * Register a new observer to listen for data changes.
3959         *
3960         * <p>The adapter may publish a variety of events describing specific changes.
3961         * Not all adapters may support all change types and some may fall back to a generic
3962         * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver#onChanged()
3963         * "something changed"} event if more specific data is not available.</p>
3964         *
3965         * <p>Components registering observers with an adapter are responsible for
3966         * {@link #unregisterAdapterDataObserver(android.support.v7.widget.RecyclerView.AdapterDataObserver)
3967         * unregistering} those observers when finished.</p>
3968         *
3969         * @param observer Observer to register
3970         *
3971         * @see #unregisterAdapterDataObserver(android.support.v7.widget.RecyclerView.AdapterDataObserver)
3972         */
3973        public void registerAdapterDataObserver(AdapterDataObserver observer) {
3974            mObservable.registerObserver(observer);
3975        }
3976
3977        /**
3978         * Unregister an observer currently listening for data changes.
3979         *
3980         * <p>The unregistered observer will no longer receive events about changes
3981         * to the adapter.</p>
3982         *
3983         * @param observer Observer to unregister
3984         *
3985         * @see #registerAdapterDataObserver(android.support.v7.widget.RecyclerView.AdapterDataObserver)
3986         */
3987        public void unregisterAdapterDataObserver(AdapterDataObserver observer) {
3988            mObservable.unregisterObserver(observer);
3989        }
3990
3991        /**
3992         * Notify any registered observers that the data set has changed.
3993         *
3994         * <p>There are two different classes of data change events, item changes and structural
3995         * changes. Item changes are when a single item has its data updated but no positional
3996         * changes have occurred. Structural changes are when items are inserted, removed or moved
3997         * within the data set.</p>
3998         *
3999         * <p>This event does not specify what about the data set has changed, forcing
4000         * any observers to assume that all existing items and structure may no longer be valid.
4001         * LayoutManagers will be forced to fully rebind and relayout all visible views.</p>
4002         *
4003         * <p><code>RecyclerView</code> will attempt to synthesize visible structural change events
4004         * for adapters that report that they have {@link #hasStableIds() stable IDs} when
4005         * this method is used. This can help for the purposes of animation and visual
4006         * object persistence but individual item views will still need to be rebound
4007         * and relaid out.</p>
4008         *
4009         * <p>If you are writing an adapter it will always be more efficient to use the more
4010         * specific change events if you can. Rely on <code>notifyDataSetChanged()</code>
4011         * as a last resort.</p>
4012         *
4013         * @see #notifyItemChanged(int)
4014         * @see #notifyItemInserted(int)
4015         * @see #notifyItemRemoved(int)
4016         * @see #notifyItemRangeChanged(int, int)
4017         * @see #notifyItemRangeInserted(int, int)
4018         * @see #notifyItemRangeRemoved(int, int)
4019         */
4020        public final void notifyDataSetChanged() {
4021            mObservable.notifyChanged();
4022        }
4023
4024        /**
4025         * Notify any registered observers that the item at <code>position</code> has changed.
4026         *
4027         * <p>This is an item change event, not a structural change event. It indicates that any
4028         * reflection of the data at <code>position</code> is out of date and should be updated.
4029         * The item at <code>position</code> retains the same identity.</p>
4030         *
4031         * @param position Position of the item that has changed
4032         *
4033         * @see #notifyItemRangeChanged(int, int)
4034         */
4035        public final void notifyItemChanged(int position) {
4036            mObservable.notifyItemRangeChanged(position, 1);
4037        }
4038
4039        /**
4040         * Notify any registered observers that the <code>itemCount</code> items starting at
4041         * position <code>positionStart</code> have changed.
4042         *
4043         * <p>This is an item change event, not a structural change event. It indicates that
4044         * any reflection of the data in the given position range is out of date and should
4045         * be updated. The items in the given range retain the same identity.</p>
4046         *
4047         * @param positionStart Position of the first item that has changed
4048         * @param itemCount Number of items that have changed
4049         *
4050         * @see #notifyItemChanged(int)
4051         */
4052        public final void notifyItemRangeChanged(int positionStart, int itemCount) {
4053            mObservable.notifyItemRangeChanged(positionStart, itemCount);
4054        }
4055
4056        /**
4057         * Notify any registered observers that the item reflected at <code>position</code>
4058         * has been newly inserted. The item previously at <code>position</code> is now at
4059         * position <code>position + 1</code>.
4060         *
4061         * <p>This is a structural change event. Representations of other existing items in the
4062         * data set are still considered up to date and will not be rebound, though their
4063         * positions may be altered.</p>
4064         *
4065         * @param position Position of the newly inserted item in the data set
4066         *
4067         * @see #notifyItemRangeInserted(int, int)
4068         */
4069        public final void notifyItemInserted(int position) {
4070            mObservable.notifyItemRangeInserted(position, 1);
4071        }
4072
4073        /**
4074         * Notify any registered observers that the item reflected at <code>fromPosition</code>
4075         * has been moved to <code>toPosition</code>.
4076         *
4077         * <p>This is a structural change event. Representations of other existing items in the
4078         * data set are still considered up to date and will not be rebound, though their
4079         * positions may be altered.</p>
4080         *
4081         * @param fromPosition Previous position of the item.
4082         * @param toPosition New position of the item.
4083         */
4084        public final void notifyItemMoved(int fromPosition, int toPosition) {
4085            mObservable.notifyItemMoved(fromPosition, toPosition);
4086        }
4087
4088        /**
4089         * Notify any registered observers that the currently reflected <code>itemCount</code>
4090         * items starting at <code>positionStart</code> have been newly inserted. The items
4091         * previously located at <code>positionStart</code> and beyond can now be found starting
4092         * at position <code>positionStart + itemCount</code>.
4093         *
4094         * <p>This is a structural change event. Representations of other existing items in the
4095         * data set are still considered up to date and will not be rebound, though their positions
4096         * may be altered.</p>
4097         *
4098         * @param positionStart Position of the first item that was inserted
4099         * @param itemCount Number of items inserted
4100         *
4101         * @see #notifyItemInserted(int)
4102         */
4103        public final void notifyItemRangeInserted(int positionStart, int itemCount) {
4104            mObservable.notifyItemRangeInserted(positionStart, itemCount);
4105        }
4106
4107        /**
4108         * Notify any registered observers that the item previously located at <code>position</code>
4109         * has been removed from the data set. The items previously located at and after
4110         * <code>position</code> may now be found at <code>oldPosition - 1</code>.
4111         *
4112         * <p>This is a structural change event. Representations of other existing items in the
4113         * data set are still considered up to date and will not be rebound, though their positions
4114         * may be altered.</p>
4115         *
4116         * @param position Position of the item that has now been removed
4117         *
4118         * @see #notifyItemRangeRemoved(int, int)
4119         */
4120        public final void notifyItemRemoved(int position) {
4121            mObservable.notifyItemRangeRemoved(position, 1);
4122        }
4123
4124        /**
4125         * Notify any registered observers that the <code>itemCount</code> items previously
4126         * located at <code>positionStart</code> have been removed from the data set. The items
4127         * previously located at and after <code>positionStart + itemCount</code> may now be found
4128         * at <code>oldPosition - itemCount</code>.
4129         *
4130         * <p>This is a structural change event. Representations of other existing items in the data
4131         * set are still considered up to date and will not be rebound, though their positions
4132         * may be altered.</p>
4133         *
4134         * @param positionStart Previous position of the first item that was removed
4135         * @param itemCount Number of items removed from the data set
4136         */
4137        public final void notifyItemRangeRemoved(int positionStart, int itemCount) {
4138            mObservable.notifyItemRangeRemoved(positionStart, itemCount);
4139        }
4140    }
4141
4142    /**
4143     * A <code>LayoutManager</code> is responsible for measuring and positioning item views
4144     * within a <code>RecyclerView</code> as well as determining the policy for when to recycle
4145     * item views that are no longer visible to the user. By changing the <code>LayoutManager</code>
4146     * a <code>RecyclerView</code> can be used to implement a standard vertically scrolling list,
4147     * a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock
4148     * layout managers are provided for general use.
4149     */
4150    public static abstract class LayoutManager {
4151        ChildHelper mChildHelper;
4152        RecyclerView mRecyclerView;
4153
4154        @Nullable
4155        SmoothScroller mSmoothScroller;
4156
4157        private boolean mRequestedSimpleAnimations = false;
4158
4159        void setRecyclerView(RecyclerView recyclerView) {
4160            if (recyclerView == null) {
4161                mRecyclerView = null;
4162                mChildHelper = null;
4163            } else {
4164                mRecyclerView = recyclerView;
4165                mChildHelper = recyclerView.mChildHelper;
4166            }
4167
4168        }
4169
4170        /**
4171         * Calls {@code RecyclerView#requestLayout} on the underlying RecyclerView
4172         */
4173        public void requestLayout() {
4174            if(mRecyclerView != null) {
4175                mRecyclerView.requestLayout();
4176            }
4177        }
4178
4179        /**
4180         * Checks if RecyclerView is in the middle of a layout or scroll and throws an
4181         * {@link IllegalStateException} if it <b>is not</b>.
4182         *
4183         * @param message The message for the exception. Can be null.
4184         * @see #assertNotInLayoutOrScroll(String)
4185         */
4186        public void assertInLayoutOrScroll(String message) {
4187            if (mRecyclerView != null) {
4188                mRecyclerView.assertInLayoutOrScroll(message);
4189            }
4190        }
4191
4192        /**
4193         * Checks if RecyclerView is in the middle of a layout or scroll and throws an
4194         * {@link IllegalStateException} if it <b>is</b>.
4195         *
4196         * @param message The message for the exception. Can be null.
4197         * @see #assertInLayoutOrScroll(String)
4198         */
4199        public void assertNotInLayoutOrScroll(String message) {
4200            if (mRecyclerView != null) {
4201                mRecyclerView.assertNotInLayoutOrScroll(message);
4202            }
4203        }
4204
4205        /**
4206         * Returns whether this LayoutManager supports automatic item animations.
4207         * A LayoutManager wishing to support item animations should obey certain
4208         * rules as outlined in {@link #onLayoutChildren(Recycler, State)}.
4209         * The default return value is <code>false</code>, so subclasses of LayoutManager
4210         * will not get predictive item animations by default.
4211         *
4212         * <p>Whether item animations are enabled in a RecyclerView is determined both
4213         * by the return value from this method and the
4214         * {@link RecyclerView#setItemAnimator(ItemAnimator) ItemAnimator} set on the
4215         * RecyclerView itself. If the RecyclerView has a non-null ItemAnimator but this
4216         * method returns false, then simple item animations will be enabled, in which
4217         * views that are moving onto or off of the screen are simply faded in/out. If
4218         * the RecyclerView has a non-null ItemAnimator and this method returns true,
4219         * then there will be two calls to {@link #onLayoutChildren(Recycler, State)} to
4220         * setup up the information needed to more intelligently predict where appearing
4221         * and disappearing views should be animated from/to.</p>
4222         *
4223         * @return true if predictive item animations should be enabled, false otherwise
4224         */
4225        public boolean supportsPredictiveItemAnimations() {
4226            return false;
4227        }
4228
4229        /**
4230         * Called when this LayoutManager is both attached to a RecyclerView and that RecyclerView
4231         * is attached to a window.
4232         *
4233         * <p>Subclass implementations should always call through to the superclass implementation.
4234         * </p>
4235         *
4236         * @param view The RecyclerView this LayoutManager is bound to
4237         */
4238        public void onAttachedToWindow(RecyclerView view) {
4239        }
4240
4241        /**
4242         * @deprecated
4243         * override {@link #onDetachedFromWindow(RecyclerView, Recycler)}
4244         */
4245        @Deprecated
4246        public void onDetachedFromWindow(RecyclerView view) {
4247
4248        }
4249
4250        /**
4251         * Called when this LayoutManager is detached from its parent RecyclerView or when
4252         * its parent RecyclerView is detached from its window.
4253         *
4254         * <p>Subclass implementations should always call through to the superclass implementation.
4255         * </p>
4256         *
4257         * @param view The RecyclerView this LayoutManager is bound to
4258         * @param recycler The recycler to use if you prefer to recycle your children instead of
4259         *                 keeping them around.
4260         */
4261        public void onDetachedFromWindow(RecyclerView view, Recycler recycler) {
4262            onDetachedFromWindow(view);
4263        }
4264
4265        /**
4266         * Check if the RecyclerView is configured to clip child views to its padding.
4267         *
4268         * @return true if this RecyclerView clips children to its padding, false otherwise
4269         */
4270        public boolean getClipToPadding() {
4271            return mRecyclerView != null && mRecyclerView.mClipToPadding;
4272        }
4273
4274        /**
4275         * Lay out all relevant child views from the given adapter.
4276         *
4277         * The LayoutManager is in charge of the behavior of item animations. By default,
4278         * RecyclerView has a non-null {@link #getItemAnimator() ItemAnimator}, and simple
4279         * item animations are enabled. This means that add/remove operations on the
4280         * adapter will result in animations to add new or appearing items, removed or
4281         * disappearing items, and moved items. If a LayoutManager returns false from
4282         * {@link #supportsPredictiveItemAnimations()}, which is the default, and runs a
4283         * normal layout operation during {@link #onLayoutChildren(Recycler, State)}, the
4284         * RecyclerView will have enough information to run those animations in a simple
4285         * way. For example, the default ItemAnimator, {@link DefaultItemAnimator}, will
4286         * simple fade views in and out, whether they are actuall added/removed or whether
4287         * they are moved on or off the screen due to other add/remove operations.
4288         *
4289         * <p>A LayoutManager wanting a better item animation experience, where items can be
4290         * animated onto and off of the screen according to where the items exist when they
4291         * are not on screen, then the LayoutManager should return true from
4292         * {@link #supportsPredictiveItemAnimations()} and add additional logic to
4293         * {@link #onLayoutChildren(Recycler, State)}. Supporting predictive animations
4294         * means that {@link #onLayoutChildren(Recycler, State)} will be called twice;
4295         * once as a "pre" layout step to determine where items would have been prior to
4296         * a real layout, and again to do the "real" layout. In the pre-layout phase,
4297         * items will remember their pre-layout positions to allow them to be laid out
4298         * appropriately. Also, {@link LayoutParams#isItemRemoved() removed} items will
4299         * be returned from the scrap to help determine correct placement of other items.
4300         * These removed items should not be added to the child list, but should be used
4301         * to help calculate correct positioning of other views, including views that
4302         * were not previously onscreen (referred to as APPEARING views), but whose
4303         * pre-layout offscreen position can be determined given the extra
4304         * information about the pre-layout removed views.</p>
4305         *
4306         * <p>The second layout pass is the real layout in which only non-removed views
4307         * will be used. The only additional requirement during this pass is, if
4308         * {@link #supportsPredictiveItemAnimations()} returns true, to note which
4309         * views exist in the child list prior to layout and which are not there after
4310         * layout (referred to as DISAPPEARING views), and to position/layout those views
4311         * appropriately, without regard to the actual bounds of the RecyclerView. This allows
4312         * the animation system to know the location to which to animate these disappearing
4313         * views.</p>
4314         *
4315         * <p>The default LayoutManager implementations for RecyclerView handle all of these
4316         * requirements for animations already. Clients of RecyclerView can either use one
4317         * of these layout managers directly or look at their implementations of
4318         * onLayoutChildren() to see how they account for the APPEARING and
4319         * DISAPPEARING views.</p>
4320         *
4321         * @param recycler         Recycler to use for fetching potentially cached views for a
4322         *                         position
4323         * @param state            Transient state of RecyclerView
4324         */
4325        public void onLayoutChildren(Recycler recycler, State state) {
4326            Log.e(TAG, "You must override onLayoutChildren(Recycler recycler, State state) ");
4327        }
4328
4329        /**
4330         * Create a default <code>LayoutParams</code> object for a child of the RecyclerView.
4331         *
4332         * <p>LayoutManagers will often want to use a custom <code>LayoutParams</code> type
4333         * to store extra information specific to the layout. Client code should subclass
4334         * {@link RecyclerView.LayoutParams} for this purpose.</p>
4335         *
4336         * <p><em>Important:</em> if you use your own custom <code>LayoutParams</code> type
4337         * you must also override
4338         * {@link #checkLayoutParams(LayoutParams)},
4339         * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and
4340         * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.</p>
4341         *
4342         * @return A new LayoutParams for a child view
4343         */
4344        public abstract LayoutParams generateDefaultLayoutParams();
4345
4346        /**
4347         * Determines the validity of the supplied LayoutParams object.
4348         *
4349         * <p>This should check to make sure that the object is of the correct type
4350         * and all values are within acceptable ranges. The default implementation
4351         * returns <code>true</code> for non-null params.</p>
4352         *
4353         * @param lp LayoutParams object to check
4354         * @return true if this LayoutParams object is valid, false otherwise
4355         */
4356        public boolean checkLayoutParams(LayoutParams lp) {
4357            return lp != null;
4358        }
4359
4360        /**
4361         * Create a LayoutParams object suitable for this LayoutManager, copying relevant
4362         * values from the supplied LayoutParams object if possible.
4363         *
4364         * <p><em>Important:</em> if you use your own custom <code>LayoutParams</code> type
4365         * you must also override
4366         * {@link #checkLayoutParams(LayoutParams)},
4367         * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and
4368         * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.</p>
4369         *
4370         * @param lp Source LayoutParams object to copy values from
4371         * @return a new LayoutParams object
4372         */
4373        public LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
4374            if (lp instanceof LayoutParams) {
4375                return new LayoutParams((LayoutParams) lp);
4376            } else if (lp instanceof MarginLayoutParams) {
4377                return new LayoutParams((MarginLayoutParams) lp);
4378            } else {
4379                return new LayoutParams(lp);
4380            }
4381        }
4382
4383        /**
4384         * Create a LayoutParams object suitable for this LayoutManager from
4385         * an inflated layout resource.
4386         *
4387         * <p><em>Important:</em> if you use your own custom <code>LayoutParams</code> type
4388         * you must also override
4389         * {@link #checkLayoutParams(LayoutParams)},
4390         * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and
4391         * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.</p>
4392         *
4393         * @param c Context for obtaining styled attributes
4394         * @param attrs AttributeSet describing the supplied arguments
4395         * @return a new LayoutParams object
4396         */
4397        public LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
4398            return new LayoutParams(c, attrs);
4399        }
4400
4401        /**
4402         * Scroll horizontally by dx pixels in screen coordinates and return the distance traveled.
4403         * The default implementation does nothing and returns 0.
4404         *
4405         * @param dx            distance to scroll by in pixels. X increases as scroll position
4406         *                      approaches the right.
4407         * @param recycler      Recycler to use for fetching potentially cached views for a
4408         *                      position
4409         * @param state         Transient state of RecyclerView
4410         * @return The actual distance scrolled. The return value will be negative if dx was
4411         * negative and scrolling proceeeded in that direction.
4412         * <code>Math.abs(result)</code> may be less than dx if a boundary was reached.
4413         */
4414        public int scrollHorizontallyBy(int dx, Recycler recycler, State state) {
4415            return 0;
4416        }
4417
4418        /**
4419         * Scroll vertically by dy pixels in screen coordinates and return the distance traveled.
4420         * The default implementation does nothing and returns 0.
4421         *
4422         * @param dy            distance to scroll in pixels. Y increases as scroll position
4423         *                      approaches the bottom.
4424         * @param recycler      Recycler to use for fetching potentially cached views for a
4425         *                      position
4426         * @param state         Transient state of RecyclerView
4427         * @return The actual distance scrolled. The return value will be negative if dy was
4428         * negative and scrolling proceeeded in that direction.
4429         * <code>Math.abs(result)</code> may be less than dy if a boundary was reached.
4430         */
4431        public int scrollVerticallyBy(int dy, Recycler recycler, State state) {
4432            return 0;
4433        }
4434
4435        /**
4436         * Query if horizontal scrolling is currently supported. The default implementation
4437         * returns false.
4438         *
4439         * @return True if this LayoutManager can scroll the current contents horizontally
4440         */
4441        public boolean canScrollHorizontally() {
4442            return false;
4443        }
4444
4445        /**
4446         * Query if vertical scrolling is currently supported. The default implementation
4447         * returns false.
4448         *
4449         * @return True if this LayoutManager can scroll the current contents vertically
4450         */
4451        public boolean canScrollVertically() {
4452            return false;
4453        }
4454
4455        /**
4456         * Scroll to the specified adapter position.
4457         *
4458         * Actual position of the item on the screen depends on the LayoutManager implementation.
4459         * @param position Scroll to this adapter position.
4460         */
4461        public void scrollToPosition(int position) {
4462            if (DEBUG) {
4463                Log.e(TAG, "You MUST implement scrollToPosition. It will soon become abstract");
4464            }
4465        }
4466
4467        /**
4468         * <p>Smooth scroll to the specified adapter position.</p>
4469         * <p>To support smooth scrolling, override this method, create your {@link SmoothScroller}
4470         * instance and call {@link #startSmoothScroll(SmoothScroller)}.
4471         * </p>
4472         * @param recyclerView The RecyclerView to which this layout manager is attached
4473         * @param state    Current State of RecyclerView
4474         * @param position Scroll to this adapter position.
4475         */
4476        public void smoothScrollToPosition(RecyclerView recyclerView, State state,
4477                int position) {
4478            Log.e(TAG, "You must override smoothScrollToPosition to support smooth scrolling");
4479        }
4480
4481        /**
4482         * <p>Starts a smooth scroll using the provided SmoothScroller.</p>
4483         * <p>Calling this method will cancel any previous smooth scroll request.</p>
4484         * @param smoothScroller Unstance which defines how smooth scroll should be animated
4485         */
4486        public void startSmoothScroll(SmoothScroller smoothScroller) {
4487            if (mSmoothScroller != null && smoothScroller != mSmoothScroller
4488                    && mSmoothScroller.isRunning()) {
4489                mSmoothScroller.stop();
4490            }
4491            mSmoothScroller = smoothScroller;
4492            mSmoothScroller.start(mRecyclerView, this);
4493        }
4494
4495        /**
4496         * @return true if RecycylerView is currently in the state of smooth scrolling.
4497         */
4498        public boolean isSmoothScrolling() {
4499            return mSmoothScroller != null && mSmoothScroller.isRunning();
4500        }
4501
4502
4503        /**
4504         * Returns the resolved layout direction for this RecyclerView.
4505         *
4506         * @return {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL} if the layout
4507         * direction is RTL or returns
4508         * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} if the layout direction
4509         * is not RTL.
4510         */
4511        public int getLayoutDirection() {
4512            return ViewCompat.getLayoutDirection(mRecyclerView);
4513        }
4514
4515        /**
4516         * Ends all animations on the view created by the {@link ItemAnimator}.
4517         *
4518         * @param view The View for which the animations should be ended.
4519         * @see RecyclerView.ItemAnimator#endAnimations()
4520         */
4521        public void endAnimation(View view) {
4522            if (mRecyclerView.mItemAnimator != null) {
4523                mRecyclerView.mItemAnimator.endAnimation(getChildViewHolderInt(view));
4524            }
4525        }
4526
4527        /**
4528         * To be called only during {@link #onLayoutChildren(Recycler, State)} to add a view
4529         * to the layout that is known to be going away, either because it has been
4530         * {@link Adapter#notifyItemRemoved(int) removed} or because it is actually not in the
4531         * visible portion of the container but is being laid out in order to inform RecyclerView
4532         * in how to animate the item out of view.
4533         * <p>
4534         * Views added via this method are going to be invisible to LayoutManager after the
4535         * dispatchLayout pass is complete. They cannot be retrieved via {@link #getChildAt(int)}
4536         * or won't be included in {@link #getChildCount()} method.
4537         *
4538         * @param child View to add and then remove with animation.
4539         */
4540        public void addDisappearingView(View child) {
4541            addDisappearingView(child, -1);
4542        }
4543
4544        /**
4545         * To be called only during {@link #onLayoutChildren(Recycler, State)} to add a view
4546         * to the layout that is known to be going away, either because it has been
4547         * {@link Adapter#notifyItemRemoved(int) removed} or because it is actually not in the
4548         * visible portion of the container but is being laid out in order to inform RecyclerView
4549         * in how to animate the item out of view.
4550         * <p>
4551         * Views added via this method are going to be invisible to LayoutManager after the
4552         * dispatchLayout pass is complete. They cannot be retrieved via {@link #getChildAt(int)}
4553         * or won't be included in {@link #getChildCount()} method.
4554         *
4555         * @param child View to add and then remove with animation.
4556         * @param index Index of the view.
4557         */
4558        public void addDisappearingView(View child, int index) {
4559            addViewInt(child, index, true);
4560        }
4561
4562        /**
4563         * Add a view to the currently attached RecyclerView if needed. LayoutManagers should
4564         * use this method to add views obtained from a {@link Recycler} using
4565         * {@link Recycler#getViewForPosition(int)}.
4566         *
4567         * @param child View to add
4568         */
4569        public void addView(View child) {
4570            addView(child, -1);
4571        }
4572
4573        /**
4574         * Add a view to the currently attached RecyclerView if needed. LayoutManagers should
4575         * use this method to add views obtained from a {@link Recycler} using
4576         * {@link Recycler#getViewForPosition(int)}.
4577         *
4578         * @param child View to add
4579         * @param index Index to add child at
4580         */
4581        public void addView(View child, int index) {
4582            addViewInt(child, index, false);
4583        }
4584
4585        private void addViewInt(View child, int index, boolean disappearing) {
4586            final ViewHolder holder = getChildViewHolderInt(child);
4587            if (disappearing || holder.isRemoved()) {
4588                // these views will be hidden at the end of the layout pass.
4589                mRecyclerView.mDisappearingViewsInLayoutPass.add(child);
4590            } else {
4591                // This may look like unnecessary but may happen if layout manager supports
4592                // predictive layouts and adapter removed then re-added the same item.
4593                // In this case, added version will be visible in the post layout (because add is
4594                // deferred) but RV will still bind it to the same View.
4595                // So if a View re-appears in post layout pass, remove it from disappearing list.
4596                mRecyclerView.mDisappearingViewsInLayoutPass.remove(child);
4597            }
4598
4599            if (holder.wasReturnedFromScrap() || holder.isScrap()) {
4600                if (holder.isScrap()) {
4601                    holder.unScrap();
4602                } else {
4603                    holder.clearReturnedFromScrapFlag();
4604                }
4605                mChildHelper.attachViewToParent(child, index, child.getLayoutParams(), false);
4606                if (DISPATCH_TEMP_DETACH) {
4607                    ViewCompat.dispatchFinishTemporaryDetach(child);
4608                }
4609            } else if (child.getParent() == mRecyclerView) { // it was not a scrap but a valid child
4610                // ensure in correct position
4611                int currentIndex = mChildHelper.indexOfChild(child);
4612                if (index == -1) {
4613                    index = mChildHelper.getChildCount();
4614                }
4615                if (currentIndex == -1) {
4616                    throw new IllegalStateException("Added View has RecyclerView as parent but"
4617                            + " view is not a real child. Unfiltered index:"
4618                            + mRecyclerView.indexOfChild(child));
4619                }
4620                if (currentIndex != index) {
4621                    mRecyclerView.mLayout.moveView(currentIndex, index);
4622                }
4623            } else {
4624                mChildHelper.addView(child, index, false);
4625                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
4626                lp.mInsetsDirty = true;
4627                final Adapter adapter = mRecyclerView.getAdapter();
4628                if (adapter != null) {
4629                    adapter.onViewAttachedToWindow(getChildViewHolderInt(child));
4630                }
4631                mRecyclerView.onChildAttachedToWindow(child);
4632                if (mSmoothScroller != null && mSmoothScroller.isRunning()) {
4633                    mSmoothScroller.onChildAttachedToWindow(child);
4634                }
4635            }
4636        }
4637
4638        /**
4639         * Remove a view from the currently attached RecyclerView if needed. LayoutManagers should
4640         * use this method to completely remove a child view that is no longer needed.
4641         * LayoutManagers should strongly consider recycling removed views using
4642         * {@link Recycler#recycleView(android.view.View)}.
4643         *
4644         * @param child View to remove
4645         */
4646        public void removeView(View child) {
4647            final Adapter adapter = mRecyclerView.getAdapter();
4648            if (adapter != null) {
4649                adapter.onViewDetachedFromWindow(getChildViewHolderInt(child));
4650            }
4651            mRecyclerView.onChildDetachedFromWindow(child);
4652            mChildHelper.removeView(child);
4653        }
4654
4655        /**
4656         * Remove a view from the currently attached RecyclerView if needed. LayoutManagers should
4657         * use this method to completely remove a child view that is no longer needed.
4658         * LayoutManagers should strongly consider recycling removed views using
4659         * {@link Recycler#recycleView(android.view.View)}.
4660         *
4661         * @param index Index of the child view to remove
4662         */
4663        public void removeViewAt(int index) {
4664            final View child = getChildAt(index);
4665            if (child != null) {
4666                final Adapter adapter = mRecyclerView.getAdapter();
4667                if (adapter != null) {
4668                    adapter.onViewDetachedFromWindow(getChildViewHolderInt(child));
4669                }
4670                mRecyclerView.onChildDetachedFromWindow(child);
4671                mChildHelper.removeViewAt(index);
4672            }
4673        }
4674
4675        /**
4676         * Remove all views from the currently attached RecyclerView. This will not recycle
4677         * any of the affected views; the LayoutManager is responsible for doing so if desired.
4678         */
4679        public void removeAllViews() {
4680            final Adapter adapter = mRecyclerView.getAdapter();
4681            // Only remove non-animating views
4682            final int childCount = getChildCount();
4683
4684            for (int i = 0; i < childCount; i++) {
4685                final View child = getChildAt(i);
4686                if (adapter != null) {
4687                    adapter.onViewDetachedFromWindow(getChildViewHolderInt(child));
4688                }
4689                mRecyclerView.onChildDetachedFromWindow(child);
4690            }
4691
4692            for (int i = childCount - 1; i >= 0; i--) {
4693                mChildHelper.removeViewAt(i);
4694            }
4695        }
4696
4697        /**
4698         * Returns the adapter position of the item represented by the given View.
4699         *
4700         * @param view The view to query
4701         * @return The adapter position of the item which is rendered by this View.
4702         */
4703        public int getPosition(View view) {
4704            return ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewPosition();
4705        }
4706
4707        /**
4708         * Returns the View type defined by the adapter.
4709         *
4710         * @param view The view to query
4711         * @return The type of the view assigned by the adapter.
4712         */
4713        public int getItemViewType(View view) {
4714            return getChildViewHolderInt(view).getItemViewType();
4715        }
4716
4717        /**
4718         * <p>Finds the view which represents the given adapter position.</p>
4719         * <p>This method traverses each child since it has no information about child order.
4720         * Override this method to improve performance if your LayoutManager keeps data about
4721         * child views.</p>
4722         *
4723         * @param position Position of the item in adapter
4724         * @return The child view that represents the given position or null if the position is not
4725         * visible
4726         */
4727        public View findViewByPosition(int position) {
4728            final int childCount = getChildCount();
4729            for (int i = 0; i < childCount; i++) {
4730                View child = getChildAt(i);
4731                ViewHolder vh = getChildViewHolderInt(child);
4732                if (vh == null) {
4733                    continue;
4734                }
4735                if (vh.getPosition() == position &&
4736                        (mRecyclerView.mState.isPreLayout() || !vh.isRemoved())) {
4737                    return child;
4738                }
4739            }
4740            return null;
4741        }
4742
4743        /**
4744         * Temporarily detach a child view.
4745         *
4746         * <p>LayoutManagers may want to perform a lightweight detach operation to rearrange
4747         * views currently attached to the RecyclerView. Generally LayoutManager implementations
4748         * will want to use {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)}
4749         * so that the detached view may be rebound and reused.</p>
4750         *
4751         * <p>If a LayoutManager uses this method to detach a view, it <em>must</em>
4752         * {@link #attachView(android.view.View, int, RecyclerView.LayoutParams) reattach}
4753         * or {@link #removeDetachedView(android.view.View) fully remove} the detached view
4754         * before the LayoutManager entry point method called by RecyclerView returns.</p>
4755         *
4756         * @param child Child to detach
4757         */
4758        public void detachView(View child) {
4759            if (DISPATCH_TEMP_DETACH) {
4760                ViewCompat.dispatchStartTemporaryDetach(child);
4761            }
4762            mRecyclerView.detachViewFromParent(child);
4763        }
4764
4765        /**
4766         * Temporarily detach a child view.
4767         *
4768         * <p>LayoutManagers may want to perform a lightweight detach operation to rearrange
4769         * views currently attached to the RecyclerView. Generally LayoutManager implementations
4770         * will want to use {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)}
4771         * so that the detached view may be rebound and reused.</p>
4772         *
4773         * <p>If a LayoutManager uses this method to detach a view, it <em>must</em>
4774         * {@link #attachView(android.view.View, int, RecyclerView.LayoutParams) reattach}
4775         * or {@link #removeDetachedView(android.view.View) fully remove} the detached view
4776         * before the LayoutManager entry point method called by RecyclerView returns.</p>
4777         *
4778         * @param index Index of the child to detach
4779         */
4780        public void detachViewAt(int index) {
4781            if (DISPATCH_TEMP_DETACH) {
4782                ViewCompat.dispatchStartTemporaryDetach(getChildAt(index));
4783            }
4784            mChildHelper.detachViewFromParent(index);
4785        }
4786
4787        /**
4788         * Reattach a previously {@link #detachView(android.view.View) detached} view.
4789         * This method should not be used to reattach views that were previously
4790         * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)}  scrapped}.
4791         *
4792         * @param child Child to reattach
4793         * @param index Intended child index for child
4794         * @param lp LayoutParams for child
4795         */
4796        public void attachView(View child, int index, LayoutParams lp) {
4797            ViewHolder vh = getChildViewHolderInt(child);
4798            if (vh.isRemoved()) {
4799                mRecyclerView.mDisappearingViewsInLayoutPass.add(child);
4800            } else {
4801                mRecyclerView.mDisappearingViewsInLayoutPass.remove(child);
4802            }
4803            mChildHelper.attachViewToParent(child, index, lp, vh.isRemoved());
4804            if (DISPATCH_TEMP_DETACH)  {
4805                ViewCompat.dispatchFinishTemporaryDetach(child);
4806            }
4807        }
4808
4809        /**
4810         * Reattach a previously {@link #detachView(android.view.View) detached} view.
4811         * This method should not be used to reattach views that were previously
4812         * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)}  scrapped}.
4813         *
4814         * @param child Child to reattach
4815         * @param index Intended child index for child
4816         */
4817        public void attachView(View child, int index) {
4818            attachView(child, index, (LayoutParams) child.getLayoutParams());
4819        }
4820
4821        /**
4822         * Reattach a previously {@link #detachView(android.view.View) detached} view.
4823         * This method should not be used to reattach views that were previously
4824         * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)}  scrapped}.
4825         *
4826         * @param child Child to reattach
4827         */
4828        public void attachView(View child) {
4829            attachView(child, -1);
4830        }
4831
4832        /**
4833         * Finish removing a view that was previously temporarily
4834         * {@link #detachView(android.view.View) detached}.
4835         *
4836         * @param child Detached child to remove
4837         */
4838        public void removeDetachedView(View child) {
4839            mRecyclerView.removeDetachedView(child, false);
4840        }
4841
4842        /**
4843         * Moves a View from one position to another.
4844         *
4845         * @param fromIndex The View's initial index
4846         * @param toIndex The View's target index
4847         */
4848        public void moveView(int fromIndex, int toIndex) {
4849            View view = getChildAt(fromIndex);
4850            if (view == null) {
4851                throw new IllegalArgumentException("Cannot move a child from non-existing index:"
4852                        + fromIndex);
4853            }
4854            detachViewAt(fromIndex);
4855            attachView(view, toIndex);
4856        }
4857
4858        /**
4859         * Detach a child view and add it to a {@link Recycler Recycler's} scrap heap.
4860         *
4861         * <p>Scrapping a view allows it to be rebound and reused to show updated or
4862         * different data.</p>
4863         *
4864         * @param child Child to detach and scrap
4865         * @param recycler Recycler to deposit the new scrap view into
4866         */
4867        public void detachAndScrapView(View child, Recycler recycler) {
4868            int index = mChildHelper.indexOfChild(child);
4869            scrapOrRecycleView(recycler, index, child);
4870        }
4871
4872        /**
4873         * Detach a child view and add it to a {@link Recycler Recycler's} scrap heap.
4874         *
4875         * <p>Scrapping a view allows it to be rebound and reused to show updated or
4876         * different data.</p>
4877         *
4878         * @param index Index of child to detach and scrap
4879         * @param recycler Recycler to deposit the new scrap view into
4880         */
4881        public void detachAndScrapViewAt(int index, Recycler recycler) {
4882            final View child = getChildAt(index);
4883            scrapOrRecycleView(recycler, index, child);
4884        }
4885
4886        /**
4887         * Remove a child view and recycle it using the given Recycler.
4888         *
4889         * @param child Child to remove and recycle
4890         * @param recycler Recycler to use to recycle child
4891         */
4892        public void removeAndRecycleView(View child, Recycler recycler) {
4893            removeView(child);
4894            recycler.recycleView(child);
4895        }
4896
4897        /**
4898         * Remove a child view and recycle it using the given Recycler.
4899         *
4900         * @param index Index of child to remove and recycle
4901         * @param recycler Recycler to use to recycle child
4902         */
4903        public void removeAndRecycleViewAt(int index, Recycler recycler) {
4904            final View view = getChildAt(index);
4905            removeViewAt(index);
4906            recycler.recycleView(view);
4907        }
4908
4909        /**
4910         * Return the current number of child views attached to the parent RecyclerView.
4911         * This does not include child views that were temporarily detached and/or scrapped.
4912         *
4913         * @return Number of attached children
4914         */
4915        public int getChildCount() {
4916            return mChildHelper != null ? mChildHelper.getChildCount() : 0;
4917        }
4918
4919        /**
4920         * Return the child view at the given index
4921         * @param index Index of child to return
4922         * @return Child view at index
4923         */
4924        public View getChildAt(int index) {
4925            return mChildHelper != null ? mChildHelper.getChildAt(index) : null;
4926        }
4927
4928        /**
4929         * Return the width of the parent RecyclerView
4930         *
4931         * @return Width in pixels
4932         */
4933        public int getWidth() {
4934            return mRecyclerView != null ? mRecyclerView.getWidth() : 0;
4935        }
4936
4937        /**
4938         * Return the height of the parent RecyclerView
4939         *
4940         * @return Height in pixels
4941         */
4942        public int getHeight() {
4943            return mRecyclerView != null ? mRecyclerView.getHeight() : 0;
4944        }
4945
4946        /**
4947         * Return the left padding of the parent RecyclerView
4948         *
4949         * @return Padding in pixels
4950         */
4951        public int getPaddingLeft() {
4952            return mRecyclerView != null ? mRecyclerView.getPaddingLeft() : 0;
4953        }
4954
4955        /**
4956         * Return the top padding of the parent RecyclerView
4957         *
4958         * @return Padding in pixels
4959         */
4960        public int getPaddingTop() {
4961            return mRecyclerView != null ? mRecyclerView.getPaddingTop() : 0;
4962        }
4963
4964        /**
4965         * Return the right padding of the parent RecyclerView
4966         *
4967         * @return Padding in pixels
4968         */
4969        public int getPaddingRight() {
4970            return mRecyclerView != null ? mRecyclerView.getPaddingRight() : 0;
4971        }
4972
4973        /**
4974         * Return the bottom padding of the parent RecyclerView
4975         *
4976         * @return Padding in pixels
4977         */
4978        public int getPaddingBottom() {
4979            return mRecyclerView != null ? mRecyclerView.getPaddingBottom() : 0;
4980        }
4981
4982        /**
4983         * Return the start padding of the parent RecyclerView
4984         *
4985         * @return Padding in pixels
4986         */
4987        public int getPaddingStart() {
4988            return mRecyclerView != null ? ViewCompat.getPaddingStart(mRecyclerView) : 0;
4989        }
4990
4991        /**
4992         * Return the end padding of the parent RecyclerView
4993         *
4994         * @return Padding in pixels
4995         */
4996        public int getPaddingEnd() {
4997            return mRecyclerView != null ? ViewCompat.getPaddingEnd(mRecyclerView) : 0;
4998        }
4999
5000        /**
5001         * Returns true if the RecyclerView this LayoutManager is bound to has focus.
5002         *
5003         * @return True if the RecyclerView has focus, false otherwise.
5004         * @see View#isFocused()
5005         */
5006        public boolean isFocused() {
5007            return mRecyclerView != null && mRecyclerView.isFocused();
5008        }
5009
5010        /**
5011         * Returns true if the RecyclerView this LayoutManager is bound to has or contains focus.
5012         *
5013         * @return true if the RecyclerView has or contains focus
5014         * @see View#hasFocus()
5015         */
5016        public boolean hasFocus() {
5017            return mRecyclerView != null && mRecyclerView.hasFocus();
5018        }
5019
5020        /**
5021         * Returns the item View which has or contains focus.
5022         *
5023         * @return A direct child of RecyclerView which has focus or contains the focused child.
5024         */
5025        public View findItemWhichHasFocus() {
5026            if (mRecyclerView == null) {
5027                return null;
5028            }
5029            View focused = mRecyclerView.findFocus();
5030            if (focused != null && focused.getParent() == mRecyclerView) {
5031                return focused;
5032            }
5033            return null;
5034        }
5035
5036        /**
5037         * Returns the number of items in the adapter bound to the parent RecyclerView.
5038         * <p>
5039         * Note that this number is not necessarily equal to {@link State#getItemCount()}. In
5040         * methods where State is available, you should use {@link State#getItemCount()} instead.
5041         * For more details, check the documentation for {@link State#getItemCount()}.
5042         *
5043         * @return The number of items in the bound adapter
5044         * @see State#getItemCount()
5045         */
5046        public int getItemCount() {
5047            final Adapter a = mRecyclerView != null ? mRecyclerView.getAdapter() : null;
5048            return a != null ? a.getItemCount() : 0;
5049        }
5050
5051        /**
5052         * Offset all child views attached to the parent RecyclerView by dx pixels along
5053         * the horizontal axis.
5054         *
5055         * @param dx Pixels to offset by
5056         */
5057        public void offsetChildrenHorizontal(int dx) {
5058            if (mRecyclerView != null) {
5059                mRecyclerView.offsetChildrenHorizontal(dx);
5060            }
5061        }
5062
5063        /**
5064         * Offset all child views attached to the parent RecyclerView by dy pixels along
5065         * the vertical axis.
5066         *
5067         * @param dy Pixels to offset by
5068         */
5069        public void offsetChildrenVertical(int dy) {
5070            if (mRecyclerView != null) {
5071                mRecyclerView.offsetChildrenVertical(dy);
5072            }
5073        }
5074
5075        /**
5076         * Flags a view so that it will not be scrapped or recycled.
5077         * <p>
5078         * Scope of ignoring a child is strictly restricted to scrapping and recyling. Methods like
5079         * {@link #removeAndRecycleAllViews(Recycler)} will ignore the child whereas
5080         * {@link #removeAllViews()} or {@link #offsetChildrenHorizontal(int)} will not ignore the
5081         * child.
5082         * <p>
5083         * Before this child can be recycled again, you have to call
5084         * {@link #stopIgnoringView(View)}.
5085         *
5086         * @param view View to ignore.
5087         * @see #stopIgnoringView(View)
5088         */
5089        public void ignoreView(View view) {
5090            if (view.getParent() != mRecyclerView || mRecyclerView.indexOfChild(view) == -1) {
5091                // checking this because calling this method on a recycled or detached view may
5092                // cause loss of state.
5093                throw new IllegalArgumentException("View should be fully attached to be ignored");
5094            }
5095            final ViewHolder vh = getChildViewHolderInt(view);
5096            vh.addFlags(ViewHolder.FLAG_IGNORE);
5097            mRecyclerView.mState.onViewIgnored(vh);
5098        }
5099
5100        /**
5101         * View can be scrapped and recycled again.
5102         * <p>
5103         * Note that calling this method removes all information in the view holder.
5104         *
5105         * @param view View to ignore.
5106         */
5107        public void stopIgnoringView(View view) {
5108            final ViewHolder vh = getChildViewHolderInt(view);
5109            vh.stopIgnoring();
5110            vh.resetInternal();
5111            vh.addFlags(ViewHolder.FLAG_INVALID);
5112        }
5113
5114        /**
5115         * Temporarily detach and scrap all currently attached child views. Views will be scrapped
5116         * into the given Recycler. The Recycler may prefer to reuse scrap views before
5117         * other views that were previously recycled.
5118         *
5119         * @param recycler Recycler to scrap views into
5120         */
5121        public void detachAndScrapAttachedViews(Recycler recycler) {
5122            final int childCount = getChildCount();
5123            for (int i = childCount - 1; i >= 0; i--) {
5124                final View v = getChildAt(i);
5125                scrapOrRecycleView(recycler, i, v);
5126            }
5127        }
5128
5129        private void scrapOrRecycleView(Recycler recycler, int index, View view) {
5130            final ViewHolder viewHolder = getChildViewHolderInt(view);
5131            if (viewHolder.shouldIgnore()) {
5132                if (DEBUG) {
5133                    Log.d(TAG, "ignoring view " + viewHolder);
5134                }
5135                return;
5136            }
5137            if (viewHolder.isInvalid() && !viewHolder.isRemoved() && !viewHolder.isChanged() &&
5138                    !mRecyclerView.mAdapter.hasStableIds()) {
5139                removeViewAt(index);
5140                recycler.recycleViewHolderInternal(viewHolder);
5141            } else {
5142                detachViewAt(index);
5143                recycler.scrapView(view);
5144            }
5145        }
5146
5147        /**
5148         * Recycles the scrapped views.
5149         * <p>
5150         * When a view is detached and removed, it does not trigger a ViewGroup invalidate. This is
5151         * the expected behavior if scrapped views are used for animations. Otherwise, we need to
5152         * call remove and invalidate RecyclerView to ensure UI update.
5153         *
5154         * @param recycler Recycler
5155         * @param remove   Whether scrapped views should be removed from ViewGroup or not. This
5156         *                 method will invalidate RecyclerView if it removes any scrapped child.
5157         */
5158        void removeAndRecycleScrapInt(Recycler recycler, boolean remove) {
5159            final int scrapCount = recycler.getScrapCount();
5160            for (int i = 0; i < scrapCount; i++) {
5161                final View scrap = recycler.getScrapViewAt(i);
5162                if (getChildViewHolderInt(scrap).shouldIgnore()) {
5163                    continue;
5164                }
5165                if (remove) {
5166                    mRecyclerView.removeDetachedView(scrap, false);
5167                }
5168                recycler.quickRecycleScrapView(scrap);
5169            }
5170            recycler.clearScrap();
5171            if (remove && scrapCount > 0) {
5172                mRecyclerView.invalidate();
5173            }
5174        }
5175
5176
5177        /**
5178         * Measure a child view using standard measurement policy, taking the padding
5179         * of the parent RecyclerView and any added item decorations into account.
5180         *
5181         * <p>If the RecyclerView can be scrolled in either dimension the caller may
5182         * pass 0 as the widthUsed or heightUsed parameters as they will be irrelevant.</p>
5183         *
5184         * @param child Child view to measure
5185         * @param widthUsed Width in pixels currently consumed by other views, if relevant
5186         * @param heightUsed Height in pixels currently consumed by other views, if relevant
5187         */
5188        public void measureChild(View child, int widthUsed, int heightUsed) {
5189            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
5190
5191            final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child);
5192            widthUsed += insets.left + insets.right;
5193            heightUsed += insets.top + insets.bottom;
5194
5195            final int widthSpec = getChildMeasureSpec(getWidth(),
5196                    getPaddingLeft() + getPaddingRight() + widthUsed, lp.width,
5197                    canScrollHorizontally());
5198            final int heightSpec = getChildMeasureSpec(getHeight(),
5199                    getPaddingTop() + getPaddingBottom() + heightUsed, lp.height,
5200                    canScrollVertically());
5201            child.measure(widthSpec, heightSpec);
5202        }
5203
5204        /**
5205         * Measure a child view using standard measurement policy, taking the padding
5206         * of the parent RecyclerView, any added item decorations and the child margins
5207         * into account.
5208         *
5209         * <p>If the RecyclerView can be scrolled in either dimension the caller may
5210         * pass 0 as the widthUsed or heightUsed parameters as they will be irrelevant.</p>
5211         *
5212         * @param child Child view to measure
5213         * @param widthUsed Width in pixels currently consumed by other views, if relevant
5214         * @param heightUsed Height in pixels currently consumed by other views, if relevant
5215         */
5216        public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
5217            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
5218
5219            final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child);
5220            widthUsed += insets.left + insets.right;
5221            heightUsed += insets.top + insets.bottom;
5222
5223            final int widthSpec = getChildMeasureSpec(getWidth(),
5224                    getPaddingLeft() + getPaddingRight() +
5225                            lp.leftMargin + lp.rightMargin + widthUsed, lp.width,
5226                    canScrollHorizontally());
5227            final int heightSpec = getChildMeasureSpec(getHeight(),
5228                    getPaddingTop() + getPaddingBottom() +
5229                            lp.topMargin + lp.bottomMargin + heightUsed, lp.height,
5230                    canScrollVertically());
5231            child.measure(widthSpec, heightSpec);
5232        }
5233
5234        /**
5235         * Calculate a MeasureSpec value for measuring a child view in one dimension.
5236         *
5237         * @param parentSize Size of the parent view where the child will be placed
5238         * @param padding Total space currently consumed by other elements of parent
5239         * @param childDimension Desired size of the child view, or MATCH_PARENT/WRAP_CONTENT.
5240         *                       Generally obtained from the child view's LayoutParams
5241         * @param canScroll true if the parent RecyclerView can scroll in this dimension
5242         *
5243         * @return a MeasureSpec value for the child view
5244         */
5245        public static int getChildMeasureSpec(int parentSize, int padding, int childDimension,
5246                boolean canScroll) {
5247            int size = Math.max(0, parentSize - padding);
5248            int resultSize = 0;
5249            int resultMode = 0;
5250
5251            if (canScroll) {
5252                if (childDimension >= 0) {
5253                    resultSize = childDimension;
5254                    resultMode = MeasureSpec.EXACTLY;
5255                } else {
5256                    // MATCH_PARENT can't be applied since we can scroll in this dimension, wrap
5257                    // instead using UNSPECIFIED.
5258                    resultSize = 0;
5259                    resultMode = MeasureSpec.UNSPECIFIED;
5260                }
5261            } else {
5262                if (childDimension >= 0) {
5263                    resultSize = childDimension;
5264                    resultMode = MeasureSpec.EXACTLY;
5265                } else if (childDimension == LayoutParams.FILL_PARENT) {
5266                    resultSize = size;
5267                    resultMode = MeasureSpec.EXACTLY;
5268                } else if (childDimension == LayoutParams.WRAP_CONTENT) {
5269                    resultSize = size;
5270                    resultMode = MeasureSpec.AT_MOST;
5271                }
5272            }
5273            return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
5274        }
5275
5276        /**
5277         * Returns the measured width of the given child, plus the additional size of
5278         * any insets applied by {@link ItemDecoration ItemDecorations}.
5279         *
5280         * @param child Child view to query
5281         * @return child's measured width plus <code>ItemDecoration</code> insets
5282         *
5283         * @see View#getMeasuredWidth()
5284         */
5285        public int getDecoratedMeasuredWidth(View child) {
5286            final Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets;
5287            return child.getMeasuredWidth() + insets.left + insets.right;
5288        }
5289
5290        /**
5291         * Returns the measured height of the given child, plus the additional size of
5292         * any insets applied by {@link ItemDecoration ItemDecorations}.
5293         *
5294         * @param child Child view to query
5295         * @return child's measured height plus <code>ItemDecoration</code> insets
5296         *
5297         * @see View#getMeasuredHeight()
5298         */
5299        public int getDecoratedMeasuredHeight(View child) {
5300            final Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets;
5301            return child.getMeasuredHeight() + insets.top + insets.bottom;
5302        }
5303
5304        /**
5305         * Lay out the given child view within the RecyclerView using coordinates that
5306         * include any current {@link ItemDecoration ItemDecorations}.
5307         *
5308         * <p>LayoutManagers should prefer working in sizes and coordinates that include
5309         * item decoration insets whenever possible. This allows the LayoutManager to effectively
5310         * ignore decoration insets within measurement and layout code. See the following
5311         * methods:</p>
5312         * <ul>
5313         *     <li>{@link #measureChild(View, int, int)}</li>
5314         *     <li>{@link #measureChildWithMargins(View, int, int)}</li>
5315         *     <li>{@link #getDecoratedLeft(View)}</li>
5316         *     <li>{@link #getDecoratedTop(View)}</li>
5317         *     <li>{@link #getDecoratedRight(View)}</li>
5318         *     <li>{@link #getDecoratedBottom(View)}</li>
5319         *     <li>{@link #getDecoratedMeasuredWidth(View)}</li>
5320         *     <li>{@link #getDecoratedMeasuredHeight(View)}</li>
5321         * </ul>
5322         *
5323         * @param child Child to lay out
5324         * @param left Left edge, with item decoration insets included
5325         * @param top Top edge, with item decoration insets included
5326         * @param right Right edge, with item decoration insets included
5327         * @param bottom Bottom edge, with item decoration insets included
5328         *
5329         * @see View#layout(int, int, int, int)
5330         */
5331        public void layoutDecorated(View child, int left, int top, int right, int bottom) {
5332            final Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets;
5333            child.layout(left + insets.left, top + insets.top, right - insets.right,
5334                    bottom - insets.bottom);
5335        }
5336
5337        /**
5338         * Returns the left edge of the given child view within its parent, offset by any applied
5339         * {@link ItemDecoration ItemDecorations}.
5340         *
5341         * @param child Child to query
5342         * @return Child left edge with offsets applied
5343         * @see #getLeftDecorationWidth(View)
5344         */
5345        public int getDecoratedLeft(View child) {
5346            return child.getLeft() - getLeftDecorationWidth(child);
5347        }
5348
5349        /**
5350         * Returns the top edge of the given child view within its parent, offset by any applied
5351         * {@link ItemDecoration ItemDecorations}.
5352         *
5353         * @param child Child to query
5354         * @return Child top edge with offsets applied
5355         * @see #getTopDecorationHeight(View)
5356         */
5357        public int getDecoratedTop(View child) {
5358            return child.getTop() - getTopDecorationHeight(child);
5359        }
5360
5361        /**
5362         * Returns the right edge of the given child view within its parent, offset by any applied
5363         * {@link ItemDecoration ItemDecorations}.
5364         *
5365         * @param child Child to query
5366         * @return Child right edge with offsets applied
5367         * @see #getRightDecorationWidth(View)
5368         */
5369        public int getDecoratedRight(View child) {
5370            return child.getRight() + getRightDecorationWidth(child);
5371        }
5372
5373        /**
5374         * Returns the bottom edge of the given child view within its parent, offset by any applied
5375         * {@link ItemDecoration ItemDecorations}.
5376         *
5377         * @param child Child to query
5378         * @return Child bottom edge with offsets applied
5379         * @see #getBottomDecorationHeight(View)
5380         */
5381        public int getDecoratedBottom(View child) {
5382            return child.getBottom() + getBottomDecorationHeight(child);
5383        }
5384
5385        /**
5386         * Calculates the item decor insets applied to the given child and updates the provided
5387         * Rect instance with the inset values.
5388         * <ul>
5389         *     <li>The Rect's left is set to the total width of left decorations.</li>
5390         *     <li>The Rect's top is set to the total height of top decorations.</li>
5391         *     <li>The Rect's right is set to the total width of right decorations.</li>
5392         *     <li>The Rect's bottom is set to total height of bottom decorations.</li>
5393         * </ul>
5394         * <p>
5395         * Note that item decorations are automatically calculated when one of the LayoutManager's
5396         * measure child methods is called. If you need to measure the child with custom specs via
5397         * {@link View#measure(int, int)}, you can use this method to get decorations.
5398         *
5399         * @param child The child view whose decorations should be calculated
5400         * @param outRect The Rect to hold result values
5401         */
5402        public void calculateItemDecorationsForChild(View child, Rect outRect) {
5403            if (mRecyclerView == null) {
5404                outRect.set(0, 0, 0, 0);
5405                return;
5406            }
5407            Rect insets = mRecyclerView.getItemDecorInsetsForChild(child);
5408            outRect.set(insets);
5409        }
5410
5411        /**
5412         * Returns the total height of item decorations applied to child's top.
5413         * <p>
5414         * Note that this value is not updated until the View is measured or
5415         * {@link #calculateItemDecorationsForChild(View, Rect)} is called.
5416         *
5417         * @param child Child to query
5418         * @return The total height of item decorations applied to the child's top.
5419         * @see #getDecoratedTop(View)
5420         * @see #calculateItemDecorationsForChild(View, Rect)
5421         */
5422        public int getTopDecorationHeight(View child) {
5423            return ((LayoutParams) child.getLayoutParams()).mDecorInsets.top;
5424        }
5425
5426        /**
5427         * Returns the total height of item decorations applied to child's bottom.
5428         * <p>
5429         * Note that this value is not updated until the View is measured or
5430         * {@link #calculateItemDecorationsForChild(View, Rect)} is called.
5431         *
5432         * @param child Child to query
5433         * @return The total height of item decorations applied to the child's bottom.
5434         * @see #getDecoratedBottom(View)
5435         * @see #calculateItemDecorationsForChild(View, Rect)
5436         */
5437        public int getBottomDecorationHeight(View child) {
5438            return ((LayoutParams) child.getLayoutParams()).mDecorInsets.bottom;
5439        }
5440
5441        /**
5442         * Returns the total width of item decorations applied to child's left.
5443         * <p>
5444         * Note that this value is not updated until the View is measured or
5445         * {@link #calculateItemDecorationsForChild(View, Rect)} is called.
5446         *
5447         * @param child Child to query
5448         * @return The total width of item decorations applied to the child's left.
5449         * @see #getDecoratedLeft(View)
5450         * @see #calculateItemDecorationsForChild(View, Rect)
5451         */
5452        public int getLeftDecorationWidth(View child) {
5453            return ((LayoutParams) child.getLayoutParams()).mDecorInsets.left;
5454        }
5455
5456        /**
5457         * Returns the total width of item decorations applied to child's right.
5458         * <p>
5459         * Note that this value is not updated until the View is measured or
5460         * {@link #calculateItemDecorationsForChild(View, Rect)} is called.
5461         *
5462         * @param child Child to query
5463         * @return The total width of item decorations applied to the child's right.
5464         * @see #getDecoratedRight(View)
5465         * @see #calculateItemDecorationsForChild(View, Rect)
5466         */
5467        public int getRightDecorationWidth(View child) {
5468            return ((LayoutParams) child.getLayoutParams()).mDecorInsets.right;
5469        }
5470
5471        /**
5472         * Called when searching for a focusable view in the given direction has failed
5473         * for the current content of the RecyclerView.
5474         *
5475         * <p>This is the LayoutManager's opportunity to populate views in the given direction
5476         * to fulfill the request if it can. The LayoutManager should attach and return
5477         * the view to be focused. The default implementation returns null.</p>
5478         *
5479         * @param focused   The currently focused view
5480         * @param direction One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
5481         *                  {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
5482         *                  {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
5483         *                  or 0 for not applicable
5484         * @param recycler  The recycler to use for obtaining views for currently offscreen items
5485         * @param state     Transient state of RecyclerView
5486         * @return The chosen view to be focused
5487         */
5488        public View onFocusSearchFailed(View focused, int direction, Recycler recycler,
5489                State state) {
5490            return null;
5491        }
5492
5493        /**
5494         * This method gives a LayoutManager an opportunity to intercept the initial focus search
5495         * before the default behavior of {@link FocusFinder} is used. If this method returns
5496         * null FocusFinder will attempt to find a focusable child view. If it fails
5497         * then {@link #onFocusSearchFailed(View, int, RecyclerView.Recycler, RecyclerView.State)}
5498         * will be called to give the LayoutManager an opportunity to add new views for items
5499         * that did not have attached views representing them. The LayoutManager should not add
5500         * or remove views from this method.
5501         *
5502         * @param focused The currently focused view
5503         * @param direction One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
5504         *                  {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
5505         *                  {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
5506         * @return A descendant view to focus or null to fall back to default behavior.
5507         *         The default implementation returns null.
5508         */
5509        public View onInterceptFocusSearch(View focused, int direction) {
5510            return null;
5511        }
5512
5513        /**
5514         * Called when a child of the RecyclerView wants a particular rectangle to be positioned
5515         * onto the screen. See {@link ViewParent#requestChildRectangleOnScreen(android.view.View,
5516         * android.graphics.Rect, boolean)} for more details.
5517         *
5518         * <p>The base implementation will attempt to perform a standard programmatic scroll
5519         * to bring the given rect into view, within the padded area of the RecyclerView.</p>
5520         *
5521         * @param child The direct child making the request.
5522         * @param rect  The rectangle in the child's coordinates the child
5523         *              wishes to be on the screen.
5524         * @param immediate True to forbid animated or delayed scrolling,
5525         *                  false otherwise
5526         * @return Whether the group scrolled to handle the operation
5527         */
5528        public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect,
5529                boolean immediate) {
5530            final int parentLeft = getPaddingLeft();
5531            final int parentTop = getPaddingTop();
5532            final int parentRight = getWidth() - getPaddingRight();
5533            final int parentBottom = getHeight() - getPaddingBottom();
5534            final int childLeft = child.getLeft() + rect.left;
5535            final int childTop = child.getTop() + rect.top;
5536            final int childRight = childLeft + rect.right;
5537            final int childBottom = childTop + rect.bottom;
5538
5539            final int offScreenLeft = Math.min(0, childLeft - parentLeft);
5540            final int offScreenTop = Math.min(0, childTop - parentTop);
5541            final int offScreenRight = Math.max(0, childRight - parentRight);
5542            final int offScreenBottom = Math.max(0, childBottom - parentBottom);
5543
5544            // Favor the "start" layout direction over the end when bringing one side or the other
5545            // of a large rect into view.
5546            final int dx;
5547            if (ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL) {
5548                dx = offScreenRight != 0 ? offScreenRight : offScreenLeft;
5549            } else {
5550                dx = offScreenLeft != 0 ? offScreenLeft : offScreenRight;
5551            }
5552
5553            // Favor bringing the top into view over the bottom
5554            final int dy = offScreenTop != 0 ? offScreenTop : offScreenBottom;
5555
5556            if (dx != 0 || dy != 0) {
5557                if (immediate) {
5558                    parent.scrollBy(dx, dy);
5559                } else {
5560                    parent.smoothScrollBy(dx, dy);
5561                }
5562                return true;
5563            }
5564            return false;
5565        }
5566
5567        /**
5568         * @deprecated Use {@link #onRequestChildFocus(RecyclerView, State, View, View)}
5569         */
5570        @Deprecated
5571        public boolean onRequestChildFocus(RecyclerView parent, View child, View focused) {
5572            return false;
5573        }
5574
5575        /**
5576         * Called when a descendant view of the RecyclerView requests focus.
5577         *
5578         * <p>A LayoutManager wishing to keep focused views aligned in a specific
5579         * portion of the view may implement that behavior in an override of this method.</p>
5580         *
5581         * <p>If the LayoutManager executes different behavior that should override the default
5582         * behavior of scrolling the focused child on screen instead of running alongside it,
5583         * this method should return true.</p>
5584         *
5585         * @param parent  The RecyclerView hosting this LayoutManager
5586         * @param state   Current state of RecyclerView
5587         * @param child   Direct child of the RecyclerView containing the newly focused view
5588         * @param focused The newly focused view. This may be the same view as child or it may be
5589         *                null
5590         * @return true if the default scroll behavior should be suppressed
5591         */
5592        public boolean onRequestChildFocus(RecyclerView parent, State state, View child,
5593                View focused) {
5594            return onRequestChildFocus(parent, child, focused);
5595        }
5596
5597        /**
5598         * Called if the RecyclerView this LayoutManager is bound to has a different adapter set.
5599         * The LayoutManager may use this opportunity to clear caches and configure state such
5600         * that it can relayout appropriately with the new data and potentially new view types.
5601         *
5602         * <p>The default implementation removes all currently attached views.</p>
5603         *
5604         * @param oldAdapter The previous adapter instance. Will be null if there was previously no
5605         *                   adapter.
5606         * @param newAdapter The new adapter instance. Might be null if
5607         *                   {@link #setAdapter(RecyclerView.Adapter)} is called with {@code null}.
5608         */
5609        public void onAdapterChanged(Adapter oldAdapter, Adapter newAdapter) {
5610        }
5611
5612        /**
5613         * Called to populate focusable views within the RecyclerView.
5614         *
5615         * <p>The LayoutManager implementation should return <code>true</code> if the default
5616         * behavior of {@link ViewGroup#addFocusables(java.util.ArrayList, int)} should be
5617         * suppressed.</p>
5618         *
5619         * <p>The default implementation returns <code>false</code> to trigger RecyclerView
5620         * to fall back to the default ViewGroup behavior.</p>
5621         *
5622         * @param recyclerView The RecyclerView hosting this LayoutManager
5623         * @param views List of output views. This method should add valid focusable views
5624         *              to this list.
5625         * @param direction One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
5626         *                  {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
5627         *                  {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
5628         * @param focusableMode The type of focusables to be added.
5629         *
5630         * @return true to suppress the default behavior, false to add default focusables after
5631         *         this method returns.
5632         *
5633         * @see #FOCUSABLES_ALL
5634         * @see #FOCUSABLES_TOUCH_MODE
5635         */
5636        public boolean onAddFocusables(RecyclerView recyclerView, ArrayList<View> views,
5637                int direction, int focusableMode) {
5638            return false;
5639        }
5640
5641        /**
5642         * Called when {@link Adapter#notifyDataSetChanged()} is triggered instead of giving
5643         * detailed information on what has actually changed.
5644         *
5645         * @param recyclerView
5646         */
5647        public void onItemsChanged(RecyclerView recyclerView) {
5648        }
5649
5650        /**
5651         * Called when items have been added to the adapter. The LayoutManager may choose to
5652         * requestLayout if the inserted items would require refreshing the currently visible set
5653         * of child views. (e.g. currently empty space would be filled by appended items, etc.)
5654         *
5655         * @param recyclerView
5656         * @param positionStart
5657         * @param itemCount
5658         */
5659        public void onItemsAdded(RecyclerView recyclerView, int positionStart, int itemCount) {
5660        }
5661
5662        /**
5663         * Called when items have been removed from the adapter.
5664         *
5665         * @param recyclerView
5666         * @param positionStart
5667         * @param itemCount
5668         */
5669        public void onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount) {
5670        }
5671
5672        /**
5673         * Called when items have been changed in the adapter.
5674         *
5675         * @param recyclerView
5676         * @param positionStart
5677         * @param itemCount
5678         */
5679        public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount) {
5680        }
5681
5682        /**
5683         * Called when an item is moved withing the adapter.
5684         * <p>
5685         * Note that, an item may also change position in response to another ADD/REMOVE/MOVE
5686         * operation. This callback is only called if and only if {@link Adapter#notifyItemMoved}
5687         * is called.
5688         *
5689         * @param recyclerView
5690         * @param from
5691         * @param to
5692         * @param itemCount
5693         */
5694        public void onItemsMoved(RecyclerView recyclerView, int from, int to, int itemCount) {
5695
5696        }
5697
5698
5699        /**
5700         * <p>Override this method if you want to support scroll bars.</p>
5701         *
5702         * <p>Read {@link RecyclerView#computeHorizontalScrollExtent()} for details.</p>
5703         *
5704         * <p>Default implementation returns 0.</p>
5705         *
5706         * @param state Current state of RecyclerView
5707         * @return The horizontal extent of the scrollbar's thumb
5708         * @see RecyclerView#computeHorizontalScrollExtent()
5709         */
5710        public int computeHorizontalScrollExtent(State state) {
5711            return 0;
5712        }
5713
5714        /**
5715         * <p>Override this method if you want to support scroll bars.</p>
5716         *
5717         * <p>Read {@link RecyclerView#computeHorizontalScrollOffset()} for details.</p>
5718         *
5719         * <p>Default implementation returns 0.</p>
5720         *
5721         * @param state Current State of RecyclerView where you can find total item count
5722         * @return The horizontal offset of the scrollbar's thumb
5723         * @see RecyclerView#computeHorizontalScrollOffset()
5724         */
5725        public int computeHorizontalScrollOffset(State state) {
5726            return 0;
5727        }
5728
5729        /**
5730         * <p>Override this method if you want to support scroll bars.</p>
5731         *
5732         * <p>Read {@link RecyclerView#computeHorizontalScrollRange()} for details.</p>
5733         *
5734         * <p>Default implementation returns 0.</p>
5735         *
5736         * @param state Current State of RecyclerView where you can find total item count
5737         * @return The total horizontal range represented by the vertical scrollbar
5738         * @see RecyclerView#computeHorizontalScrollRange()
5739         */
5740        public int computeHorizontalScrollRange(State state) {
5741            return 0;
5742        }
5743
5744        /**
5745         * <p>Override this method if you want to support scroll bars.</p>
5746         *
5747         * <p>Read {@link RecyclerView#computeVerticalScrollExtent()} for details.</p>
5748         *
5749         * <p>Default implementation returns 0.</p>
5750         *
5751         * @param state Current state of RecyclerView
5752         * @return The vertical extent of the scrollbar's thumb
5753         * @see RecyclerView#computeVerticalScrollExtent()
5754         */
5755        public int computeVerticalScrollExtent(State state) {
5756            return 0;
5757        }
5758
5759        /**
5760         * <p>Override this method if you want to support scroll bars.</p>
5761         *
5762         * <p>Read {@link RecyclerView#computeVerticalScrollOffset()} for details.</p>
5763         *
5764         * <p>Default implementation returns 0.</p>
5765         *
5766         * @param state Current State of RecyclerView where you can find total item count
5767         * @return The vertical offset of the scrollbar's thumb
5768         * @see RecyclerView#computeVerticalScrollOffset()
5769         */
5770        public int computeVerticalScrollOffset(State state) {
5771            return 0;
5772        }
5773
5774        /**
5775         * <p>Override this method if you want to support scroll bars.</p>
5776         *
5777         * <p>Read {@link RecyclerView#computeVerticalScrollRange()} for details.</p>
5778         *
5779         * <p>Default implementation returns 0.</p>
5780         *
5781         * @param state Current State of RecyclerView where you can find total item count
5782         * @return The total vertical range represented by the vertical scrollbar
5783         * @see RecyclerView#computeVerticalScrollRange()
5784         */
5785        public int computeVerticalScrollRange(State state) {
5786            return 0;
5787        }
5788
5789        /**
5790         * Measure the attached RecyclerView. Implementations must call
5791         * {@link #setMeasuredDimension(int, int)} before returning.
5792         *
5793         * <p>The default implementation will handle EXACTLY measurements and respect
5794         * the minimum width and height properties of the host RecyclerView if measured
5795         * as UNSPECIFIED. AT_MOST measurements will be treated as EXACTLY and the RecyclerView
5796         * will consume all available space.</p>
5797         *
5798         * @param recycler Recycler
5799         * @param state Transient state of RecyclerView
5800         * @param widthSpec Width {@link android.view.View.MeasureSpec}
5801         * @param heightSpec Height {@link android.view.View.MeasureSpec}
5802         */
5803        public void onMeasure(Recycler recycler, State state, int widthSpec, int heightSpec) {
5804            final int widthMode = MeasureSpec.getMode(widthSpec);
5805            final int heightMode = MeasureSpec.getMode(heightSpec);
5806            final int widthSize = MeasureSpec.getSize(widthSpec);
5807            final int heightSize = MeasureSpec.getSize(heightSpec);
5808
5809            int width = 0;
5810            int height = 0;
5811
5812            switch (widthMode) {
5813                case MeasureSpec.EXACTLY:
5814                case MeasureSpec.AT_MOST:
5815                    width = widthSize;
5816                    break;
5817                case MeasureSpec.UNSPECIFIED:
5818                default:
5819                    width = getMinimumWidth();
5820                    break;
5821            }
5822
5823            switch (heightMode) {
5824                case MeasureSpec.EXACTLY:
5825                case MeasureSpec.AT_MOST:
5826                    height = heightSize;
5827                    break;
5828                case MeasureSpec.UNSPECIFIED:
5829                default:
5830                    height = getMinimumHeight();
5831                    break;
5832            }
5833
5834            setMeasuredDimension(width, height);
5835        }
5836
5837        /**
5838         * {@link View#setMeasuredDimension(int, int) Set the measured dimensions} of the
5839         * host RecyclerView.
5840         *
5841         * @param widthSize Measured width
5842         * @param heightSize Measured height
5843         */
5844        public void setMeasuredDimension(int widthSize, int heightSize) {
5845            mRecyclerView.setMeasuredDimension(widthSize, heightSize);
5846        }
5847
5848        /**
5849         * @return The host RecyclerView's {@link View#getMinimumWidth()}
5850         */
5851        public int getMinimumWidth() {
5852            return ViewCompat.getMinimumWidth(mRecyclerView);
5853        }
5854
5855        /**
5856         * @return The host RecyclerView's {@link View#getMinimumHeight()}
5857         */
5858        public int getMinimumHeight() {
5859            return ViewCompat.getMinimumHeight(mRecyclerView);
5860        }
5861        /**
5862         * <p>Called when the LayoutManager should save its state. This is a good time to save your
5863         * scroll position, configuration and anything else that may be required to restore the same
5864         * layout state if the LayoutManager is recreated.</p>
5865         * <p>RecyclerView does NOT verify if the LayoutManager has changed between state save and
5866         * restore. This will let you share information between your LayoutManagers but it is also
5867         * your responsibility to make sure they use the same parcelable class.</p>
5868         *
5869         * @return Necessary information for LayoutManager to be able to restore its state
5870         */
5871        public Parcelable onSaveInstanceState() {
5872            return null;
5873        }
5874
5875
5876        public void onRestoreInstanceState(Parcelable state) {
5877
5878        }
5879
5880        void stopSmoothScroller() {
5881            if (mSmoothScroller != null) {
5882                mSmoothScroller.stop();
5883            }
5884        }
5885
5886        private void onSmoothScrollerStopped(SmoothScroller smoothScroller) {
5887            if (mSmoothScroller == smoothScroller) {
5888                mSmoothScroller = null;
5889            }
5890        }
5891
5892        /**
5893         * RecyclerView calls this method to notify LayoutManager that scroll state has changed.
5894         *
5895         * @param state The new scroll state for RecyclerView
5896         */
5897        public void onScrollStateChanged(int state) {
5898        }
5899
5900        /**
5901         * Removes all views and recycles them using the given recycler.
5902         * <p>
5903         * If you want to clean cached views as well, you should call {@link Recycler#clear()} too.
5904         * <p>
5905         * If a View is marked as "ignored", it is not removed nor recycled.
5906         *
5907         * @param recycler Recycler to use to recycle children
5908         * @see #removeAndRecycleView(View, Recycler)
5909         * @see #removeAndRecycleViewAt(int, Recycler)
5910         * @see #ignoreView(View)
5911         */
5912        public void removeAndRecycleAllViews(Recycler recycler) {
5913            for (int i = getChildCount() - 1; i >= 0; i--) {
5914                final View view = getChildAt(i);
5915                if (!getChildViewHolderInt(view).shouldIgnore()) {
5916                    removeAndRecycleViewAt(i, recycler);
5917                }
5918            }
5919        }
5920
5921        /**
5922         * A LayoutManager can call this method to force RecyclerView to run simple animations in
5923         * the next layout pass, even if there is not any trigger to do so. (e.g. adapter data
5924         * change).
5925         * <p>
5926         * Note that, calling this method will not guarantee that RecyclerView will run animations
5927         * at all. For example, if there is not any {@link ItemAnimator} set, RecyclerView will
5928         * not run any animations but will still clear this flag after the layout is complete.
5929         *
5930         */
5931        public void requestSimpleAnimationsInNextLayout() {
5932            mRequestedSimpleAnimations = true;
5933        }
5934    }
5935
5936    /**
5937     * An ItemDecoration allows the application to add a special drawing and layout offset
5938     * to specific item views from the adapter's data set. This can be useful for drawing dividers
5939     * between items, highlights, visual grouping boundaries and more.
5940     *
5941     * <p>All ItemDecorations are drawn in the order they were added, before the item
5942     * views (in {@link ItemDecoration#onDraw(Canvas, RecyclerView, RecyclerView.State) onDraw()}
5943     * and after the items (in {@link ItemDecoration#onDrawOver(Canvas, RecyclerView,
5944     * RecyclerView.State)}.</p>
5945     */
5946    public static abstract class ItemDecoration {
5947        /**
5948         * Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
5949         * Any content drawn by this method will be drawn before the item views are drawn,
5950         * and will thus appear underneath the views.
5951         *
5952         * @param c Canvas to draw into
5953         * @param parent RecyclerView this ItemDecoration is drawing into
5954         * @param state The current state of RecyclerView
5955         */
5956        public void onDraw(Canvas c, RecyclerView parent, State state) {
5957            onDraw(c, parent);
5958        }
5959
5960        /**
5961         * @deprecated
5962         * Override {@link #onDraw(Canvas, RecyclerView, RecyclerView.State)}
5963         */
5964        @Deprecated
5965        public void onDraw(Canvas c, RecyclerView parent) {
5966        }
5967
5968        /**
5969         * Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
5970         * Any content drawn by this method will be drawn after the item views are drawn
5971         * and will thus appear over the views.
5972         *
5973         * @param c Canvas to draw into
5974         * @param parent RecyclerView this ItemDecoration is drawing into
5975         * @param state The current state of RecyclerView.
5976         */
5977        public void onDrawOver(Canvas c, RecyclerView parent, State state) {
5978            onDrawOver(c, parent);
5979        }
5980
5981        /**
5982         * @deprecated
5983         * Override {@link #onDrawOver(Canvas, RecyclerView, RecyclerView.State)}
5984         */
5985        @Deprecated
5986        public void onDrawOver(Canvas c, RecyclerView parent) {
5987        }
5988
5989
5990        /**
5991         * @deprecated
5992         * Use {@link #getItemOffsets(Rect, View, RecyclerView, State)}
5993         */
5994        @Deprecated
5995        public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
5996            outRect.set(0, 0, 0, 0);
5997        }
5998
5999        /**
6000         * Retrieve any offsets for the given item. Each field of <code>outRect</code> specifies
6001         * the number of pixels that the item view should be inset by, similar to padding or margin.
6002         * The default implementation sets the bounds of outRect to 0 and returns.
6003         *
6004         * <p>If this ItemDecoration does not affect the positioning of item views it should set
6005         * all four fields of <code>outRect</code> (left, top, right, bottom) to zero
6006         * before returning.</p>
6007         *
6008         * @param outRect Rect to receive the output.
6009         * @param view    The child view to decorate
6010         * @param parent  RecyclerView this ItemDecoration is decorating
6011         * @param state   The current state of RecyclerView.
6012         */
6013        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
6014            getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewPosition(),
6015                    parent);
6016        }
6017    }
6018
6019    /**
6020     * An OnItemTouchListener allows the application to intercept touch events in progress at the
6021     * view hierarchy level of the RecyclerView before those touch events are considered for
6022     * RecyclerView's own scrolling behavior.
6023     *
6024     * <p>This can be useful for applications that wish to implement various forms of gestural
6025     * manipulation of item views within the RecyclerView. OnItemTouchListeners may intercept
6026     * a touch interaction already in progress even if the RecyclerView is already handling that
6027     * gesture stream itself for the purposes of scrolling.</p>
6028     */
6029    public interface OnItemTouchListener {
6030        /**
6031         * Silently observe and/or take over touch events sent to the RecyclerView
6032         * before they are handled by either the RecyclerView itself or its child views.
6033         *
6034         * <p>The onInterceptTouchEvent methods of each attached OnItemTouchListener will be run
6035         * in the order in which each listener was added, before any other touch processing
6036         * by the RecyclerView itself or child views occurs.</p>
6037         *
6038         * @param e MotionEvent describing the touch event. All coordinates are in
6039         *          the RecyclerView's coordinate system.
6040         * @return true if this OnItemTouchListener wishes to begin intercepting touch events, false
6041         *         to continue with the current behavior and continue observing future events in
6042         *         the gesture.
6043         */
6044        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e);
6045
6046        /**
6047         * Process a touch event as part of a gesture that was claimed by returning true from
6048         * a previous call to {@link #onInterceptTouchEvent}.
6049         *
6050         * @param e MotionEvent describing the touch event. All coordinates are in
6051         *          the RecyclerView's coordinate system.
6052         */
6053        public void onTouchEvent(RecyclerView rv, MotionEvent e);
6054    }
6055
6056    /**
6057     * An OnScrollListener can be set on a RecyclerView to receive messages
6058     * when a scrolling event has occurred on that RecyclerView.
6059     *
6060     * @see RecyclerView#setOnScrollListener(OnScrollListener)
6061     */
6062    abstract static public class OnScrollListener {
6063        /**
6064         * Callback method to be invoked when RecyclerView's scroll state changes.
6065         *
6066         * @param recyclerView The RecyclerView whose scroll state has changed.
6067         * @param newState     The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
6068         *                     {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
6069         */
6070        public void onScrollStateChanged(RecyclerView recyclerView, int newState){}
6071
6072        /**
6073         * Callback method to be invoked when the RecyclerView has been scrolled. This will be
6074         * called after the scroll has completed.
6075         *
6076         * @param recyclerView The RecyclerView which scrolled.
6077         * @param dx The amount of horizontal scroll.
6078         * @param dy The amount of vertical scroll.
6079         */
6080        public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
6081    }
6082
6083    /**
6084     * A RecyclerListener can be set on a RecyclerView to receive messages whenever
6085     * a view is recycled.
6086     *
6087     * @see RecyclerView#setRecyclerListener(RecyclerListener)
6088     */
6089    public interface RecyclerListener {
6090
6091        /**
6092         * This method is called whenever the view in the ViewHolder is recycled.
6093         *
6094         * @param holder The ViewHolder containing the view that was recycled
6095         */
6096        public void onViewRecycled(ViewHolder holder);
6097    }
6098
6099    /**
6100     * A ViewHolder describes an item view and metadata about its place within the RecyclerView.
6101     *
6102     * <p>{@link Adapter} implementations should subclass ViewHolder and add fields for caching
6103     * potentially expensive {@link View#findViewById(int)} results.</p>
6104     *
6105     * <p>While {@link LayoutParams} belong to the {@link LayoutManager},
6106     * {@link ViewHolder ViewHolders} belong to the adapter. Adapters should feel free to use
6107     * their own custom ViewHolder implementations to store data that makes binding view contents
6108     * easier. Implementations should assume that individual item views will hold strong references
6109     * to <code>ViewHolder</code> objects and that <code>RecyclerView</code> instances may hold
6110     * strong references to extra off-screen item views for caching purposes</p>
6111     */
6112    public static abstract class ViewHolder {
6113        public final View itemView;
6114        int mPosition = NO_POSITION;
6115        int mOldPosition = NO_POSITION;
6116        long mItemId = NO_ID;
6117        int mItemViewType = INVALID_TYPE;
6118        int mPreLayoutPosition = NO_POSITION;
6119
6120        // The item that this holder is shadowing during an item change event/animation
6121        ViewHolder mShadowedHolder = null;
6122        // The item that is shadowing this holder during an item change event/animation
6123        ViewHolder mShadowingHolder = null;
6124
6125        /**
6126         * This ViewHolder has been bound to a position; mPosition, mItemId and mItemViewType
6127         * are all valid.
6128         */
6129        static final int FLAG_BOUND = 1 << 0;
6130
6131        /**
6132         * The data this ViewHolder's view reflects is stale and needs to be rebound
6133         * by the adapter. mPosition and mItemId are consistent.
6134         */
6135        static final int FLAG_UPDATE = 1 << 1;
6136
6137        /**
6138         * This ViewHolder's data is invalid. The identity implied by mPosition and mItemId
6139         * are not to be trusted and may no longer match the item view type.
6140         * This ViewHolder must be fully rebound to different data.
6141         */
6142        static final int FLAG_INVALID = 1 << 2;
6143
6144        /**
6145         * This ViewHolder points at data that represents an item previously removed from the
6146         * data set. Its view may still be used for things like outgoing animations.
6147         */
6148        static final int FLAG_REMOVED = 1 << 3;
6149
6150        /**
6151         * This ViewHolder should not be recycled. This flag is set via setIsRecyclable()
6152         * and is intended to keep views around during animations.
6153         */
6154        static final int FLAG_NOT_RECYCLABLE = 1 << 4;
6155
6156        /**
6157         * This ViewHolder is returned from scrap which means we are expecting an addView call
6158         * for this itemView. When returned from scrap, ViewHolder stays in the scrap list until
6159         * the end of the layout pass and then recycled by RecyclerView if it is not added back to
6160         * the RecyclerView.
6161         */
6162        static final int FLAG_RETURNED_FROM_SCRAP = 1 << 5;
6163
6164        /**
6165         * This ViewHolder's contents have changed. This flag is used as an indication that
6166         * change animations may be used, if supported by the ItemAnimator.
6167         */
6168        static final int FLAG_CHANGED = 1 << 6;
6169
6170        /**
6171         * This ViewHolder is fully managed by the LayoutManager. We do not scrap, recycle or remove
6172         * it unless LayoutManager is replaced.
6173         * It is still fully visible to the LayoutManager.
6174         */
6175        static final int FLAG_IGNORE = 1 << 7;
6176
6177        private int mFlags;
6178
6179        private int mIsRecyclableCount = 0;
6180
6181        // If non-null, view is currently considered scrap and may be reused for other data by the
6182        // scrap container.
6183        private Recycler mScrapContainer = null;
6184
6185        public ViewHolder(View itemView) {
6186            if (itemView == null) {
6187                throw new IllegalArgumentException("itemView may not be null");
6188            }
6189            this.itemView = itemView;
6190        }
6191
6192        void offsetPosition(int offset, boolean applyToPreLayout) {
6193            if (mOldPosition == NO_POSITION) {
6194                mOldPosition = mPosition;
6195            }
6196            if (mPreLayoutPosition == NO_POSITION) {
6197                mPreLayoutPosition = mPosition;
6198            }
6199            if (applyToPreLayout) {
6200                mPreLayoutPosition += offset;
6201            }
6202            mPosition += offset;
6203            if (itemView.getLayoutParams() != null) {
6204                ((LayoutParams) itemView.getLayoutParams()).mInsetsDirty = true;
6205            }
6206        }
6207
6208        void clearOldPosition() {
6209            mOldPosition = NO_POSITION;
6210            mPreLayoutPosition = NO_POSITION;
6211        }
6212
6213        void saveOldPosition() {
6214            if (mOldPosition == NO_POSITION) {
6215                mOldPosition = mPosition;
6216            }
6217        }
6218
6219        boolean shouldIgnore() {
6220            return (mFlags & FLAG_IGNORE) != 0;
6221        }
6222
6223        public final int getPosition() {
6224            return mPreLayoutPosition == NO_POSITION ? mPosition : mPreLayoutPosition;
6225        }
6226
6227        /**
6228         * When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders
6229         * to perform animations.
6230         * <p>
6231         * If a ViewHolder was laid out in the previous onLayout call, old position will keep its
6232         * adapter index in the previous layout.
6233         *
6234         * @return The previous adapter index of the Item represented by this ViewHolder or
6235         * {@link #NO_POSITION} if old position does not exists or cleared (pre-layout is
6236         * complete).
6237         */
6238        public final int getOldPosition() {
6239            return mOldPosition;
6240        }
6241
6242        /**
6243         * Returns The itemId represented by this ViewHolder.
6244         *
6245         * @return The the item's id if adapter has stable ids, {@link RecyclerView#NO_ID}
6246         * otherwise
6247         */
6248        public final long getItemId() {
6249            return mItemId;
6250        }
6251
6252        /**
6253         * @return The view type of this ViewHolder.
6254         */
6255        public final int getItemViewType() {
6256            return mItemViewType;
6257        }
6258
6259        boolean isScrap() {
6260            return mScrapContainer != null;
6261        }
6262
6263        void unScrap() {
6264            mScrapContainer.unscrapView(this);
6265        }
6266
6267        boolean wasReturnedFromScrap() {
6268            return (mFlags & FLAG_RETURNED_FROM_SCRAP) != 0;
6269        }
6270
6271        void clearReturnedFromScrapFlag() {
6272            mFlags = mFlags & ~FLAG_RETURNED_FROM_SCRAP;
6273        }
6274
6275        void stopIgnoring() {
6276            mFlags = mFlags & ~FLAG_IGNORE;
6277        }
6278
6279        void setScrapContainer(Recycler recycler) {
6280            mScrapContainer = recycler;
6281        }
6282
6283        boolean isInvalid() {
6284            return (mFlags & FLAG_INVALID) != 0;
6285        }
6286
6287        boolean needsUpdate() {
6288            return (mFlags & FLAG_UPDATE) != 0;
6289        }
6290
6291        boolean isChanged() {
6292            return (mFlags & FLAG_CHANGED) != 0;
6293        }
6294
6295        boolean isBound() {
6296            return (mFlags & FLAG_BOUND) != 0;
6297        }
6298
6299        boolean isRemoved() {
6300            return (mFlags & FLAG_REMOVED) != 0;
6301        }
6302
6303        void setFlags(int flags, int mask) {
6304            mFlags = (mFlags & ~mask) | (flags & mask);
6305        }
6306
6307        void addFlags(int flags) {
6308            mFlags |= flags;
6309        }
6310
6311        void resetInternal() {
6312            mFlags = 0;
6313            mPosition = NO_POSITION;
6314            mOldPosition = NO_POSITION;
6315            mItemId = NO_ID;
6316            mPreLayoutPosition = NO_POSITION;
6317            mIsRecyclableCount = 0;
6318            mShadowedHolder = null;
6319            mShadowingHolder = null;
6320        }
6321
6322        @Override
6323        public String toString() {
6324            final StringBuilder sb = new StringBuilder("ViewHolder{" +
6325                    Integer.toHexString(hashCode()) + " position=" + mPosition + " id=" + mItemId +
6326                    ", oldPos=" + mOldPosition + ", pLpos:" + mPreLayoutPosition);
6327            if (isScrap()) sb.append(" scrap");
6328            if (isInvalid()) sb.append(" invalid");
6329            if (!isBound()) sb.append(" unbound");
6330            if (needsUpdate()) sb.append(" update");
6331            if (isRemoved()) sb.append(" removed");
6332            if (shouldIgnore()) sb.append(" ignored");
6333            if (isChanged()) sb.append(" changed");
6334            if (!isRecyclable()) sb.append(" not recyclable(" + mIsRecyclableCount + ")");
6335            if (itemView.getParent() == null) sb.append(" no parent");
6336            sb.append("}");
6337            return sb.toString();
6338        }
6339
6340        /**
6341         * Informs the recycler whether this item can be recycled. Views which are not
6342         * recyclable will not be reused for other items until setIsRecyclable() is
6343         * later set to true. Calls to setIsRecyclable() should always be paired (one
6344         * call to setIsRecyclabe(false) should always be matched with a later call to
6345         * setIsRecyclable(true)). Pairs of calls may be nested, as the state is internally
6346         * reference-counted.
6347         *
6348         * @param recyclable Whether this item is available to be recycled. Default value
6349         * is true.
6350         */
6351        public final void setIsRecyclable(boolean recyclable) {
6352            mIsRecyclableCount = recyclable ? mIsRecyclableCount - 1 : mIsRecyclableCount + 1;
6353            if (mIsRecyclableCount < 0) {
6354                mIsRecyclableCount = 0;
6355                Log.e(VIEW_LOG_TAG, "isRecyclable decremented below 0: " +
6356                        "unmatched pair of setIsRecyable() calls");
6357            } else if (!recyclable && mIsRecyclableCount == 1) {
6358                mFlags |= FLAG_NOT_RECYCLABLE;
6359            } else if (recyclable && mIsRecyclableCount == 0) {
6360                mFlags &= ~FLAG_NOT_RECYCLABLE;
6361            }
6362            if (DEBUG) {
6363                Log.d(TAG, "setIsRecyclable val:" + recyclable + ":" + this);
6364            }
6365        }
6366
6367        /**
6368         * @see {@link #setIsRecyclable(boolean)}
6369         *
6370         * @return true if this item is available to be recycled, false otherwise.
6371         */
6372        public final boolean isRecyclable() {
6373            return (mFlags & FLAG_NOT_RECYCLABLE) == 0 &&
6374                    !ViewCompat.hasTransientState(itemView);
6375        }
6376    }
6377
6378    /**
6379     * {@link android.view.ViewGroup.MarginLayoutParams LayoutParams} subclass for children of
6380     * {@link RecyclerView}. Custom {@link LayoutManager layout managers} are encouraged
6381     * to create their own subclass of this <code>LayoutParams</code> class
6382     * to store any additional required per-child view metadata about the layout.
6383     */
6384    public static class LayoutParams extends android.view.ViewGroup.MarginLayoutParams {
6385        ViewHolder mViewHolder;
6386        final Rect mDecorInsets = new Rect();
6387        boolean mInsetsDirty = true;
6388
6389        public LayoutParams(Context c, AttributeSet attrs) {
6390            super(c, attrs);
6391        }
6392
6393        public LayoutParams(int width, int height) {
6394            super(width, height);
6395        }
6396
6397        public LayoutParams(MarginLayoutParams source) {
6398            super(source);
6399        }
6400
6401        public LayoutParams(ViewGroup.LayoutParams source) {
6402            super(source);
6403        }
6404
6405        public LayoutParams(LayoutParams source) {
6406            super((ViewGroup.LayoutParams) source);
6407        }
6408
6409        /**
6410         * Returns true if the view this LayoutParams is attached to needs to have its content
6411         * updated from the corresponding adapter.
6412         *
6413         * @return true if the view should have its content updated
6414         */
6415        public boolean viewNeedsUpdate() {
6416            return mViewHolder.needsUpdate();
6417        }
6418
6419        /**
6420         * Returns true if the view this LayoutParams is attached to is now representing
6421         * potentially invalid data. A LayoutManager should scrap/recycle it.
6422         *
6423         * @return true if the view is invalid
6424         */
6425        public boolean isViewInvalid() {
6426            return mViewHolder.isInvalid();
6427        }
6428
6429        /**
6430         * Returns true if the adapter data item corresponding to the view this LayoutParams
6431         * is attached to has been removed from the data set. A LayoutManager may choose to
6432         * treat it differently in order to animate its outgoing or disappearing state.
6433         *
6434         * @return true if the item the view corresponds to was removed from the data set
6435         */
6436        public boolean isItemRemoved() {
6437            return mViewHolder.isRemoved();
6438        }
6439
6440        /**
6441         * Returns true if the adapter data item corresponding to the view this LayoutParams
6442         * is attached to has been changed in the data set. A LayoutManager may choose to
6443         * treat it differently in order to animate its changing state.
6444         *
6445         * @return true if the item the view corresponds to was changed in the data set
6446         */
6447        public boolean isItemChanged() {
6448            return mViewHolder.isChanged();
6449        }
6450
6451        /**
6452         * Returns the position that the view this LayoutParams is attached to corresponds to.
6453         *
6454         * @return the adapter position this view was bound from
6455         */
6456        public int getViewPosition() {
6457            return mViewHolder.getPosition();
6458        }
6459    }
6460
6461    /**
6462     * Observer base class for watching changes to an {@link Adapter}.
6463     * See {@link Adapter#registerAdapterDataObserver(AdapterDataObserver)}.
6464     */
6465    public static abstract class AdapterDataObserver {
6466        public void onChanged() {
6467            // Do nothing
6468        }
6469
6470        public void onItemRangeChanged(int positionStart, int itemCount) {
6471            // do nothing
6472        }
6473
6474        public void onItemRangeInserted(int positionStart, int itemCount) {
6475            // do nothing
6476        }
6477
6478        public void onItemRangeRemoved(int positionStart, int itemCount) {
6479            // do nothing
6480        }
6481
6482        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
6483            // do nothing
6484        }
6485    }
6486
6487    /**
6488     * <p>Base class for smooth scrolling. Handles basic tracking of the target view position and
6489     * provides methods to trigger a programmatic scroll.</p>
6490     *
6491     * @see LinearSmoothScroller
6492     */
6493    public static abstract class SmoothScroller {
6494
6495        private int mTargetPosition = RecyclerView.NO_POSITION;
6496
6497        private RecyclerView mRecyclerView;
6498
6499        private LayoutManager mLayoutManager;
6500
6501        private boolean mPendingInitialRun;
6502
6503        private boolean mRunning;
6504
6505        private View mTargetView;
6506
6507        private final Action mRecyclingAction;
6508
6509        public SmoothScroller() {
6510            mRecyclingAction = new Action(0, 0);
6511        }
6512
6513        /**
6514         * Starts a smooth scroll for the given target position.
6515         * <p>In each animation step, {@link RecyclerView} will check
6516         * for the target view and call either
6517         * {@link #onTargetFound(android.view.View, RecyclerView.State, SmoothScroller.Action)} or
6518         * {@link #onSeekTargetStep(int, int, RecyclerView.State, SmoothScroller.Action)} until
6519         * SmoothScroller is stopped.</p>
6520         *
6521         * <p>Note that if RecyclerView finds the target view, it will automatically stop the
6522         * SmoothScroller. This <b>does not</b> mean that scroll will stop, it only means it will
6523         * stop calling SmoothScroller in each animation step.</p>
6524         */
6525        void start(RecyclerView recyclerView, LayoutManager layoutManager) {
6526            mRecyclerView = recyclerView;
6527            mLayoutManager = layoutManager;
6528            if (mTargetPosition == RecyclerView.NO_POSITION) {
6529                throw new IllegalArgumentException("Invalid target position");
6530            }
6531            mRecyclerView.mState.mTargetPosition = mTargetPosition;
6532            mRunning = true;
6533            mPendingInitialRun = true;
6534            mTargetView = findViewByPosition(getTargetPosition());
6535            onStart();
6536            mRecyclerView.mViewFlinger.postOnAnimation();
6537        }
6538
6539        public void setTargetPosition(int targetPosition) {
6540            mTargetPosition = targetPosition;
6541        }
6542
6543        /**
6544         * @return The LayoutManager to which this SmoothScroller is attached
6545         */
6546        public LayoutManager getLayoutManager() {
6547            return mLayoutManager;
6548        }
6549
6550        /**
6551         * Stops running the SmoothScroller in each animation callback. Note that this does not
6552         * cancel any existing {@link Action} updated by
6553         * {@link #onTargetFound(android.view.View, RecyclerView.State, SmoothScroller.Action)} or
6554         * {@link #onSeekTargetStep(int, int, RecyclerView.State, SmoothScroller.Action)}.
6555         */
6556        final protected void stop() {
6557            if (!mRunning) {
6558                return;
6559            }
6560            onStop();
6561            mRecyclerView.mState.mTargetPosition = RecyclerView.NO_POSITION;
6562            mTargetView = null;
6563            mTargetPosition = RecyclerView.NO_POSITION;
6564            mPendingInitialRun = false;
6565            mRunning = false;
6566            // trigger a cleanup
6567            mLayoutManager.onSmoothScrollerStopped(this);
6568            // clear references to avoid any potential leak by a custom smooth scroller
6569            mLayoutManager = null;
6570            mRecyclerView = null;
6571        }
6572
6573        /**
6574         * Returns true if SmoothScroller has been started but has not received the first
6575         * animation
6576         * callback yet.
6577         *
6578         * @return True if this SmoothScroller is waiting to start
6579         */
6580        public boolean isPendingInitialRun() {
6581            return mPendingInitialRun;
6582        }
6583
6584
6585        /**
6586         * @return True if SmoothScroller is currently active
6587         */
6588        public boolean isRunning() {
6589            return mRunning;
6590        }
6591
6592        /**
6593         * Returns the adapter position of the target item
6594         *
6595         * @return Adapter position of the target item or
6596         * {@link RecyclerView#NO_POSITION} if no target view is set.
6597         */
6598        public int getTargetPosition() {
6599            return mTargetPosition;
6600        }
6601
6602        private void onAnimation(int dx, int dy) {
6603            if (!mRunning || mTargetPosition == RecyclerView.NO_POSITION) {
6604                stop();
6605            }
6606            mPendingInitialRun = false;
6607            if (mTargetView != null) {
6608                // verify target position
6609                if (getChildPosition(mTargetView) == mTargetPosition) {
6610                    onTargetFound(mTargetView, mRecyclerView.mState, mRecyclingAction);
6611                    mRecyclingAction.runIfNecessary(mRecyclerView);
6612                    stop();
6613                } else {
6614                    Log.e(TAG, "Passed over target position while smooth scrolling.");
6615                    mTargetView = null;
6616                }
6617            }
6618            if (mRunning) {
6619                onSeekTargetStep(dx, dy, mRecyclerView.mState, mRecyclingAction);
6620                mRecyclingAction.runIfNecessary(mRecyclerView);
6621            }
6622        }
6623
6624        /**
6625         * @see RecyclerView#getChildPosition(android.view.View)
6626         */
6627        public int getChildPosition(View view) {
6628            return mRecyclerView.getChildPosition(view);
6629        }
6630
6631        /**
6632         * @see RecyclerView.LayoutManager#getChildCount()
6633         */
6634        public int getChildCount() {
6635            return mRecyclerView.mLayout.getChildCount();
6636        }
6637
6638        /**
6639         * @see RecyclerView.LayoutManager#findViewByPosition(int)
6640         */
6641        public View findViewByPosition(int position) {
6642            return mRecyclerView.mLayout.findViewByPosition(position);
6643        }
6644
6645        /**
6646         * @see RecyclerView#scrollToPosition(int)
6647         */
6648        public void instantScrollToPosition(int position) {
6649            mRecyclerView.scrollToPosition(position);
6650        }
6651
6652        protected void onChildAttachedToWindow(View child) {
6653            if (getChildPosition(child) == getTargetPosition()) {
6654                mTargetView = child;
6655                if (DEBUG) {
6656                    Log.d(TAG, "smooth scroll target view has been attached");
6657                }
6658            }
6659        }
6660
6661        /**
6662         * Normalizes the vector.
6663         * @param scrollVector The vector that points to the target scroll position
6664         */
6665        protected void normalize(PointF scrollVector) {
6666            final double magnitute = Math.sqrt(scrollVector.x * scrollVector.x + scrollVector.y *
6667                    scrollVector.y);
6668            scrollVector.x /= magnitute;
6669            scrollVector.y /= magnitute;
6670        }
6671
6672        /**
6673         * Called when smooth scroll is started. This might be a good time to do setup.
6674         */
6675        abstract protected void onStart();
6676
6677        /**
6678         * Called when smooth scroller is stopped. This is a good place to cleanup your state etc.
6679         * @see #stop()
6680         */
6681        abstract protected void onStop();
6682
6683        /**
6684         * <p>RecyclerView will call this method each time it scrolls until it can find the target
6685         * position in the layout.</p>
6686         * <p>SmoothScroller should check dx, dy and if scroll should be changed, update the
6687         * provided {@link Action} to define the next scroll.</p>
6688         *
6689         * @param dx        Last scroll amount horizontally
6690         * @param dy        Last scroll amount verticaully
6691         * @param state     Transient state of RecyclerView
6692         * @param action    If you want to trigger a new smooth scroll and cancel the previous one,
6693         *                  update this object.
6694         */
6695        abstract protected void onSeekTargetStep(int dx, int dy, State state, Action action);
6696
6697        /**
6698         * Called when the target position is laid out. This is the last callback SmoothScroller
6699         * will receive and it should update the provided {@link Action} to define the scroll
6700         * details towards the target view.
6701         * @param targetView    The view element which render the target position.
6702         * @param state         Transient state of RecyclerView
6703         * @param action        Action instance that you should update to define final scroll action
6704         *                      towards the targetView
6705         * @return An {@link Action} to finalize the smooth scrolling
6706         */
6707        abstract protected void onTargetFound(View targetView, State state, Action action);
6708
6709        /**
6710         * Holds information about a smooth scroll request by a {@link SmoothScroller}.
6711         */
6712        public static class Action {
6713
6714            public static final int UNDEFINED_DURATION = Integer.MIN_VALUE;
6715
6716            private int mDx;
6717
6718            private int mDy;
6719
6720            private int mDuration;
6721
6722            private Interpolator mInterpolator;
6723
6724            private boolean changed = false;
6725
6726            // we track this variable to inform custom implementer if they are updating the action
6727            // in every animation callback
6728            private int consecutiveUpdates = 0;
6729
6730            /**
6731             * @param dx Pixels to scroll horizontally
6732             * @param dy Pixels to scroll vertically
6733             */
6734            public Action(int dx, int dy) {
6735                this(dx, dy, UNDEFINED_DURATION, null);
6736            }
6737
6738            /**
6739             * @param dx       Pixels to scroll horizontally
6740             * @param dy       Pixels to scroll vertically
6741             * @param duration Duration of the animation in milliseconds
6742             */
6743            public Action(int dx, int dy, int duration) {
6744                this(dx, dy, duration, null);
6745            }
6746
6747            /**
6748             * @param dx           Pixels to scroll horizontally
6749             * @param dy           Pixels to scroll vertically
6750             * @param duration     Duration of the animation in milliseconds
6751             * @param interpolator Interpolator to be used when calculating scroll position in each
6752             *                     animation step
6753             */
6754            public Action(int dx, int dy, int duration, Interpolator interpolator) {
6755                mDx = dx;
6756                mDy = dy;
6757                mDuration = duration;
6758                mInterpolator = interpolator;
6759            }
6760            private void runIfNecessary(RecyclerView recyclerView) {
6761                if (changed) {
6762                    validate();
6763                    if (mInterpolator == null) {
6764                        if (mDuration == UNDEFINED_DURATION) {
6765                            recyclerView.mViewFlinger.smoothScrollBy(mDx, mDy);
6766                        } else {
6767                            recyclerView.mViewFlinger.smoothScrollBy(mDx, mDy, mDuration);
6768                        }
6769                    } else {
6770                        recyclerView.mViewFlinger.smoothScrollBy(mDx, mDy, mDuration, mInterpolator);
6771                    }
6772                    consecutiveUpdates ++;
6773                    if (consecutiveUpdates > 10) {
6774                        // A new action is being set in every animation step. This looks like a bad
6775                        // implementation. Inform developer.
6776                        Log.e(TAG, "Smooth Scroll action is being updated too frequently. Make sure"
6777                                + " you are not changing it unless necessary");
6778                    }
6779                    changed = false;
6780                } else {
6781                    consecutiveUpdates = 0;
6782                }
6783            }
6784
6785            private void validate() {
6786                if (mInterpolator != null && mDuration < 1) {
6787                    throw new IllegalStateException("If you provide an interpolator, you must"
6788                            + " set a positive duration");
6789                } else if (mDuration < 1) {
6790                    throw new IllegalStateException("Scroll duration must be a positive number");
6791                }
6792            }
6793
6794            public int getDx() {
6795                return mDx;
6796            }
6797
6798            public void setDx(int dx) {
6799                changed = true;
6800                mDx = dx;
6801            }
6802
6803            public int getDy() {
6804                return mDy;
6805            }
6806
6807            public void setDy(int dy) {
6808                changed = true;
6809                mDy = dy;
6810            }
6811
6812            public int getDuration() {
6813                return mDuration;
6814            }
6815
6816            public void setDuration(int duration) {
6817                changed = true;
6818                mDuration = duration;
6819            }
6820
6821            public Interpolator getInterpolator() {
6822                return mInterpolator;
6823            }
6824
6825            /**
6826             * Sets the interpolator to calculate scroll steps
6827             * @param interpolator The interpolator to use. If you specify an interpolator, you must
6828             *                     also set the duration.
6829             * @see #setDuration(int)
6830             */
6831            public void setInterpolator(Interpolator interpolator) {
6832                changed = true;
6833                mInterpolator = interpolator;
6834            }
6835
6836            /**
6837             * Updates the action with given parameters.
6838             * @param dx Pixels to scroll horizontally
6839             * @param dy Pixels to scroll vertically
6840             * @param duration Duration of the animation in milliseconds
6841             * @param interpolator Interpolator to be used when calculating scroll position in each
6842             *                     animation step
6843             */
6844            public void update(int dx, int dy, int duration, Interpolator interpolator) {
6845                mDx = dx;
6846                mDy = dy;
6847                mDuration = duration;
6848                mInterpolator = interpolator;
6849                changed = true;
6850            }
6851        }
6852    }
6853
6854    static class AdapterDataObservable extends Observable<AdapterDataObserver> {
6855        public boolean hasObservers() {
6856            return !mObservers.isEmpty();
6857        }
6858
6859        public void notifyChanged() {
6860            // since onChanged() is implemented by the app, it could do anything, including
6861            // removing itself from {@link mObservers} - and that could cause problems if
6862            // an iterator is used on the ArrayList {@link mObservers}.
6863            // to avoid such problems, just march thru the list in the reverse order.
6864            for (int i = mObservers.size() - 1; i >= 0; i--) {
6865                mObservers.get(i).onChanged();
6866            }
6867        }
6868
6869        public void notifyItemRangeChanged(int positionStart, int itemCount) {
6870            // since onItemRangeChanged() is implemented by the app, it could do anything, including
6871            // removing itself from {@link mObservers} - and that could cause problems if
6872            // an iterator is used on the ArrayList {@link mObservers}.
6873            // to avoid such problems, just march thru the list in the reverse order.
6874            for (int i = mObservers.size() - 1; i >= 0; i--) {
6875                mObservers.get(i).onItemRangeChanged(positionStart, itemCount);
6876            }
6877        }
6878
6879        public void notifyItemRangeInserted(int positionStart, int itemCount) {
6880            // since onItemRangeInserted() is implemented by the app, it could do anything,
6881            // including removing itself from {@link mObservers} - and that could cause problems if
6882            // an iterator is used on the ArrayList {@link mObservers}.
6883            // to avoid such problems, just march thru the list in the reverse order.
6884            for (int i = mObservers.size() - 1; i >= 0; i--) {
6885                mObservers.get(i).onItemRangeInserted(positionStart, itemCount);
6886            }
6887        }
6888
6889        public void notifyItemRangeRemoved(int positionStart, int itemCount) {
6890            // since onItemRangeRemoved() is implemented by the app, it could do anything, including
6891            // removing itself from {@link mObservers} - and that could cause problems if
6892            // an iterator is used on the ArrayList {@link mObservers}.
6893            // to avoid such problems, just march thru the list in the reverse order.
6894            for (int i = mObservers.size() - 1; i >= 0; i--) {
6895                mObservers.get(i).onItemRangeRemoved(positionStart, itemCount);
6896            }
6897        }
6898
6899        public void notifyItemMoved(int fromPosition, int toPosition) {
6900            for (int i = mObservers.size() - 1; i >= 0; i--) {
6901                mObservers.get(i).onItemRangeMoved(fromPosition, toPosition, 1);
6902            }
6903        }
6904    }
6905
6906    static class SavedState extends android.view.View.BaseSavedState {
6907
6908        Parcelable mLayoutState;
6909
6910        /**
6911         * called by CREATOR
6912         */
6913        SavedState(Parcel in) {
6914            super(in);
6915            mLayoutState = in.readParcelable(LayoutManager.class.getClassLoader());
6916        }
6917
6918        /**
6919         * Called by onSaveInstanceState
6920         */
6921        SavedState(Parcelable superState) {
6922            super(superState);
6923        }
6924
6925        @Override
6926        public void writeToParcel(Parcel dest, int flags) {
6927            super.writeToParcel(dest, flags);
6928            dest.writeParcelable(mLayoutState, 0);
6929        }
6930
6931        private void copyFrom(SavedState other) {
6932            mLayoutState = other.mLayoutState;
6933        }
6934
6935        public static final Parcelable.Creator<SavedState> CREATOR
6936                = new Parcelable.Creator<SavedState>() {
6937            @Override
6938            public SavedState createFromParcel(Parcel in) {
6939                return new SavedState(in);
6940            }
6941
6942            @Override
6943            public SavedState[] newArray(int size) {
6944                return new SavedState[size];
6945            }
6946        };
6947    }
6948    /**
6949     * <p>Contains useful information about the current RecyclerView state like target scroll
6950     * position or view focus. State object can also keep arbitrary data, identified by resource
6951     * ids.</p>
6952     * <p>Often times, RecyclerView components will need to pass information between each other.
6953     * To provide a well defined data bus between components, RecyclerView passes the same State
6954     * object to component callbacks and these components can use it to exchange data.</p>
6955     * <p>If you implement custom components, you can use State's put/get/remove methods to pass
6956     * data between your components without needing to manage their lifecycles.</p>
6957     */
6958    public static class State {
6959
6960        private int mTargetPosition = RecyclerView.NO_POSITION;
6961        ArrayMap<ViewHolder, ItemHolderInfo> mPreLayoutHolderMap =
6962                new ArrayMap<ViewHolder, ItemHolderInfo>();
6963        ArrayMap<ViewHolder, ItemHolderInfo> mPostLayoutHolderMap =
6964                new ArrayMap<ViewHolder, ItemHolderInfo>();
6965        // nullable
6966        ArrayMap<Long, ViewHolder> mOldChangedHolders = new ArrayMap<Long, ViewHolder>();
6967
6968        private SparseArray<Object> mData;
6969
6970        /**
6971         * Number of items adapter has.
6972         */
6973        private int mItemCount = 0;
6974
6975        /**
6976         * Number of items adapter had in the previous layout.
6977         */
6978        private int mPreviousLayoutItemCount = 0;
6979
6980        /**
6981         * Number of items that were NOT laid out but has been deleted from the adapter after the
6982         * previous layout.
6983         */
6984        private int mDeletedInvisibleItemCountSincePreviousLayout = 0;
6985
6986        private boolean mStructureChanged = false;
6987
6988        private boolean mInPreLayout = false;
6989
6990        private boolean mRunSimpleAnimations = false;
6991
6992        private boolean mRunPredictiveAnimations = false;
6993
6994        State reset() {
6995            mTargetPosition = RecyclerView.NO_POSITION;
6996            if (mData != null) {
6997                mData.clear();
6998            }
6999            mItemCount = 0;
7000            mStructureChanged = false;
7001            return this;
7002        }
7003
7004        public boolean isPreLayout() {
7005            return mInPreLayout;
7006        }
7007
7008        /**
7009         * Returns whether RecyclerView will run predictive animations in this layout pass
7010         * or not.
7011         *
7012         * @return true if RecyclerView is calculating predictive animations to be run at the end
7013         *         of the layout pass.
7014         */
7015        public boolean willRunPredictiveAnimations() {
7016            return mRunPredictiveAnimations;
7017        }
7018
7019        /**
7020         * Returns whether RecyclerView will run simple animations in this layout pass
7021         * or not.
7022         *
7023         * @return true if RecyclerView is calculating simple animations to be run at the end of
7024         *         the layout pass.
7025         */
7026        public boolean willRunSimpleAnimations() {
7027            return mRunSimpleAnimations;
7028        }
7029
7030        /**
7031         * Removes the mapping from the specified id, if there was any.
7032         * @param resourceId Id of the resource you want to remove. It is suggested to use R.id.* to
7033         *                   preserve cross functionality and avoid conflicts.
7034         */
7035        public void remove(int resourceId) {
7036            if (mData == null) {
7037                return;
7038            }
7039            mData.remove(resourceId);
7040        }
7041
7042        /**
7043         * Gets the Object mapped from the specified id, or <code>null</code>
7044         * if no such data exists.
7045         *
7046         * @param resourceId Id of the resource you want to remove. It is suggested to use R.id.*
7047         *                   to
7048         *                   preserve cross functionality and avoid conflicts.
7049         */
7050        public <T> T get(int resourceId) {
7051            if (mData == null) {
7052                return null;
7053            }
7054            return (T) mData.get(resourceId);
7055        }
7056
7057        /**
7058         * Adds a mapping from the specified id to the specified value, replacing the previous
7059         * mapping from the specified key if there was one.
7060         *
7061         * @param resourceId Id of the resource you want to add. It is suggested to use R.id.* to
7062         *                   preserve cross functionality and avoid conflicts.
7063         * @param data       The data you want to associate with the resourceId.
7064         */
7065        public void put(int resourceId, Object data) {
7066            if (mData == null) {
7067                mData = new SparseArray<Object>();
7068            }
7069            mData.put(resourceId, data);
7070        }
7071
7072        /**
7073         * If scroll is triggered to make a certain item visible, this value will return the
7074         * adapter index of that item.
7075         * @return Adapter index of the target item or
7076         * {@link RecyclerView#NO_POSITION} if there is no target
7077         * position.
7078         */
7079        public int getTargetScrollPosition() {
7080            return mTargetPosition;
7081        }
7082
7083        /**
7084         * Returns if current scroll has a target position.
7085         * @return true if scroll is being triggered to make a certain position visible
7086         * @see #getTargetScrollPosition()
7087         */
7088        public boolean hasTargetScrollPosition() {
7089            return mTargetPosition != RecyclerView.NO_POSITION;
7090        }
7091
7092        /**
7093         * @return true if the structure of the data set has changed since the last call to
7094         *         onLayoutChildren, false otherwise
7095         */
7096        public boolean didStructureChange() {
7097            return mStructureChanged;
7098        }
7099
7100        /**
7101         * Returns the total number of items that can be laid out. Note that this number is not
7102         * necessarily equal to the number of items in the adapter, so you should always use this
7103         * number for your position calculations and never access the adapter directly.
7104         * <p>
7105         * RecyclerView listens for Adapter's notify events and calculates the effects of adapter
7106         * data changes on existing Views. These calculations are used to decide which animations
7107         * should be run.
7108         * <p>
7109         * To support predictive animations, RecyclerView may rewrite or reorder Adapter changes to
7110         * present the correct state to LayoutManager in pre-layout pass.
7111         * <p>
7112         * For example, a newly added item is not included in pre-layout item count because
7113         * pre-layout reflects the contents of the adapter before the item is added. Behind the
7114         * scenes, RecyclerView offsets {@link Recycler#getViewForPosition(int)} calls such that
7115         * LayoutManager does not know about the new item's existence in pre-layout. The item will
7116         * be available in second layout pass and will be included in the item count. Similar
7117         * adjustments are made for moved and removed items as well.
7118         * <p>
7119         * You can get the adapter's item count via {@link LayoutManager#getItemCount()} method.
7120         *
7121         * @return The number of items currently available
7122         * @see LayoutManager#getItemCount()
7123         */
7124        public int getItemCount() {
7125            return mInPreLayout ?
7126                    (mPreviousLayoutItemCount - mDeletedInvisibleItemCountSincePreviousLayout) :
7127                    mItemCount;
7128        }
7129
7130        public void onViewRecycled(ViewHolder holder) {
7131            mPreLayoutHolderMap.remove(holder);
7132            mPostLayoutHolderMap.remove(holder);
7133            if (mOldChangedHolders != null) {
7134                removeFrom(mOldChangedHolders, holder);
7135            }
7136            // holder cannot be in new list.
7137        }
7138
7139        public void onViewIgnored(ViewHolder holder) {
7140            onViewRecycled(holder);
7141        }
7142
7143        private void removeFrom(ArrayMap<Long, ViewHolder> holderMap, ViewHolder holder) {
7144            for (int i = holderMap.size() - 1; i >= 0; i --) {
7145                if (holder == holderMap.valueAt(i)) {
7146                    holderMap.removeAt(i);
7147                    return;
7148                }
7149            }
7150        }
7151
7152        @Override
7153        public String toString() {
7154            return "State{" +
7155                    "mTargetPosition=" + mTargetPosition +
7156                    ", mPreLayoutHolderMap=" + mPreLayoutHolderMap +
7157                    ", mPostLayoutHolderMap=" + mPostLayoutHolderMap +
7158                    ", mData=" + mData +
7159                    ", mItemCount=" + mItemCount +
7160                    ", mPreviousLayoutItemCount=" + mPreviousLayoutItemCount +
7161                    ", mDeletedInvisibleItemCountSincePreviousLayout="
7162                    + mDeletedInvisibleItemCountSincePreviousLayout +
7163                    ", mStructureChanged=" + mStructureChanged +
7164                    ", mInPreLayout=" + mInPreLayout +
7165                    ", mRunSimpleAnimations=" + mRunSimpleAnimations +
7166                    ", mRunPredictiveAnimations=" + mRunPredictiveAnimations +
7167                    '}';
7168        }
7169    }
7170
7171    /**
7172     * Internal listener that manages items after animations finish. This is how items are
7173     * retained (not recycled) during animations, but allowed to be recycled afterwards.
7174     * It depends on the contract with the ItemAnimator to call the appropriate dispatch*Finished()
7175     * method on the animator's listener when it is done animating any item.
7176     */
7177    private class ItemAnimatorRestoreListener implements ItemAnimator.ItemAnimatorListener {
7178
7179        @Override
7180        public void onRemoveFinished(ViewHolder item) {
7181            item.setIsRecyclable(true);
7182            removeAnimatingView(item.itemView);
7183            removeDetachedView(item.itemView, false);
7184        }
7185
7186        @Override
7187        public void onAddFinished(ViewHolder item) {
7188            item.setIsRecyclable(true);
7189            if (item.isRecyclable()) {
7190                removeAnimatingView(item.itemView);
7191            }
7192        }
7193
7194        @Override
7195        public void onMoveFinished(ViewHolder item) {
7196            item.setIsRecyclable(true);
7197            if (item.isRecyclable()) {
7198                removeAnimatingView(item.itemView);
7199            }
7200        }
7201
7202        @Override
7203        public void onChangeFinished(ViewHolder item) {
7204            item.setIsRecyclable(true);
7205            /**
7206             * We check both shadowed and shadowing because a ViewHolder may get both roles at the
7207             * same time.
7208             *
7209             * Assume this flow:
7210             * item X is represented by VH_1. Then itemX changes, so we create VH_2 .
7211             * RV sets the following and calls item animator:
7212             * VH_1.shadowed = VH_2;
7213             * VH_1.mChanged = true;
7214             * VH_2.shadowing =VH_1;
7215             *
7216             * Then, before the first change finishes, item changes again so we create VH_3.
7217             * RV sets the following and calls item animator:
7218             * VH_2.shadowed = VH_3
7219             * VH_2.mChanged = true
7220             * VH_3.shadowing = VH_2
7221             *
7222             * Because VH_2 already has an animation, it will be cancelled. At this point VH_2 has
7223             * both shadowing and shadowed fields set. Shadowing information is obsolete now
7224             * because the first animation where VH_2 is newViewHolder is not valid anymore.
7225             * We ended up in this case because VH_2 played both roles. On the other hand,
7226             * we DO NOT want to clear its changed flag.
7227             *
7228             * If second change was simply reverting first change, we would find VH_1 in
7229             * {@link Recycler#getScrapViewForPosition(int, int, boolean)} and recycle it before
7230             * re-using
7231             */
7232            if (item.mShadowedHolder != null && item.mShadowingHolder == null) { // old vh
7233                item.mShadowedHolder = null;
7234                item.setFlags(~ViewHolder.FLAG_CHANGED, item.mFlags);
7235            }
7236            // always null this because an OldViewHolder can never become NewViewHolder w/o being
7237            // recycled.
7238            item.mShadowingHolder = null;
7239            if (item.isRecyclable()) {
7240                removeAnimatingView(item.itemView);
7241            }
7242        }
7243    };
7244
7245    /**
7246     * This class defines the animations that take place on items as changes are made
7247     * to the adapter.
7248     *
7249     * Subclasses of ItemAnimator can be used to implement custom animations for actions on
7250     * ViewHolder items. The RecyclerView will manage retaining these items while they
7251     * are being animated, but implementors must call the appropriate "Starting"
7252     * ({@link #dispatchRemoveStarting(ViewHolder)}, {@link #dispatchMoveStarting(ViewHolder)},
7253     * {@link #dispatchChangeStarting(ViewHolder, boolean)}, or
7254     * {@link #dispatchAddStarting(ViewHolder)})
7255     * and "Finished" ({@link #dispatchRemoveFinished(ViewHolder)},
7256     * {@link #dispatchMoveFinished(ViewHolder)},
7257     * {@link #dispatchChangeFinished(ViewHolder, boolean)},
7258     * or {@link #dispatchAddFinished(ViewHolder)}) methods when each item animation is
7259     * being started and ended.
7260     *
7261     * <p>By default, RecyclerView uses {@link DefaultItemAnimator}</p>
7262     *
7263     * @see #setItemAnimator(ItemAnimator)
7264     */
7265    public static abstract class ItemAnimator {
7266
7267        private ItemAnimatorListener mListener = null;
7268        private ArrayList<ItemAnimatorFinishedListener> mFinishedListeners =
7269                new ArrayList<ItemAnimatorFinishedListener>();
7270
7271        private long mAddDuration = 120;
7272        private long mRemoveDuration = 120;
7273        private long mMoveDuration = 250;
7274        private long mChangeDuration = 250;
7275
7276        private boolean mSupportsChangeAnimations = false;
7277
7278        /**
7279         * Gets the current duration for which all move animations will run.
7280         *
7281         * @return The current move duration
7282         */
7283        public long getMoveDuration() {
7284            return mMoveDuration;
7285        }
7286
7287        /**
7288         * Sets the duration for which all move animations will run.
7289         *
7290         * @param moveDuration The move duration
7291         */
7292        public void setMoveDuration(long moveDuration) {
7293            mMoveDuration = moveDuration;
7294        }
7295
7296        /**
7297         * Gets the current duration for which all add animations will run.
7298         *
7299         * @return The current add duration
7300         */
7301        public long getAddDuration() {
7302            return mAddDuration;
7303        }
7304
7305        /**
7306         * Sets the duration for which all add animations will run.
7307         *
7308         * @param addDuration The add duration
7309         */
7310        public void setAddDuration(long addDuration) {
7311            mAddDuration = addDuration;
7312        }
7313
7314        /**
7315         * Gets the current duration for which all remove animations will run.
7316         *
7317         * @return The current remove duration
7318         */
7319        public long getRemoveDuration() {
7320            return mRemoveDuration;
7321        }
7322
7323        /**
7324         * Sets the duration for which all remove animations will run.
7325         *
7326         * @param removeDuration The remove duration
7327         */
7328        public void setRemoveDuration(long removeDuration) {
7329            mRemoveDuration = removeDuration;
7330        }
7331
7332        /**
7333         * Gets the current duration for which all change animations will run.
7334         *
7335         * @return The current change duration
7336         */
7337        public long getChangeDuration() {
7338            return mChangeDuration;
7339        }
7340
7341        /**
7342         * Sets the duration for which all change animations will run.
7343         *
7344         * @param changeDuration The change duration
7345         */
7346        public void setChangeDuration(long changeDuration) {
7347            mChangeDuration = changeDuration;
7348        }
7349
7350        /**
7351         * Returns whether this ItemAnimator supports animations of change events.
7352         *
7353         * @return true if change animations are supported, false otherwise
7354         */
7355        public boolean getSupportsChangeAnimations() {
7356            return mSupportsChangeAnimations;
7357        }
7358
7359        /**
7360         * Sets whether this ItemAnimator supports animations of item change events.
7361         * By default, ItemAnimator only supports animations when items are added or removed.
7362         * By setting this property to true, actions on the data set which change the
7363         * contents of items may also be animated. What those animations are is left
7364         * up to the discretion of the ItemAnimator subclass, in its
7365         * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} implementation.
7366         * The value of this property is false by default.
7367         *
7368         * @see Adapter#notifyItemChanged(int)
7369         * @see Adapter#notifyItemRangeChanged(int, int)
7370         *
7371         * @param supportsChangeAnimations true if change animations are supported by
7372         * this ItemAnimator, false otherwise. If the property is false, the ItemAnimator
7373         * will not receive a call to
7374         * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} when changes occur.
7375         */
7376        public void setSupportsChangeAnimations(boolean supportsChangeAnimations) {
7377            mSupportsChangeAnimations = supportsChangeAnimations;
7378        }
7379
7380        /**
7381         * Internal only:
7382         * Sets the listener that must be called when the animator is finished
7383         * animating the item (or immediately if no animation happens). This is set
7384         * internally and is not intended to be set by external code.
7385         *
7386         * @param listener The listener that must be called.
7387         */
7388        void setListener(ItemAnimatorListener listener) {
7389            mListener = listener;
7390        }
7391
7392        /**
7393         * Called when there are pending animations waiting to be started. This state
7394         * is governed by the return values from {@link #animateAdd(ViewHolder) animateAdd()},
7395         * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, and
7396         * {@link #animateRemove(ViewHolder) animateRemove()}, which inform the
7397         * RecyclerView that the ItemAnimator wants to be called later to start the
7398         * associated animations. runPendingAnimations() will be scheduled to be run
7399         * on the next frame.
7400         */
7401        abstract public void runPendingAnimations();
7402
7403        /**
7404         * Called when an item is removed from the RecyclerView. Implementors can choose
7405         * whether and how to animate that change, but must always call
7406         * {@link #dispatchRemoveFinished(ViewHolder)} when done, either
7407         * immediately (if no animation will occur) or after the animation actually finishes.
7408         * The return value indicates whether an animation has been set up and whether the
7409         * ItemAnimator's {@link #runPendingAnimations()} method should be called at the
7410         * next opportunity. This mechanism allows ItemAnimator to set up individual animations
7411         * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()},
7412         * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()},
7413         * {@link #animateRemove(ViewHolder) animateRemove()}, and
7414         * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one,
7415         * then start the animations together in the later call to {@link #runPendingAnimations()}.
7416         *
7417         * <p>This method may also be called for disappearing items which continue to exist in the
7418         * RecyclerView, but for which the system does not have enough information to animate
7419         * them out of view. In that case, the default animation for removing items is run
7420         * on those items as well.</p>
7421         *
7422         * @param holder The item that is being removed.
7423         * @return true if a later call to {@link #runPendingAnimations()} is requested,
7424         * false otherwise.
7425         */
7426        abstract public boolean animateRemove(ViewHolder holder);
7427
7428        /**
7429         * Called when an item is added to the RecyclerView. Implementors can choose
7430         * whether and how to animate that change, but must always call
7431         * {@link #dispatchAddFinished(ViewHolder)} when done, either
7432         * immediately (if no animation will occur) or after the animation actually finishes.
7433         * The return value indicates whether an animation has been set up and whether the
7434         * ItemAnimator's {@link #runPendingAnimations()} method should be called at the
7435         * next opportunity. This mechanism allows ItemAnimator to set up individual animations
7436         * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()},
7437         * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()},
7438         * {@link #animateRemove(ViewHolder) animateRemove()}, and
7439         * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one,
7440         * then start the animations together in the later call to {@link #runPendingAnimations()}.
7441         *
7442         * <p>This method may also be called for appearing items which were already in the
7443         * RecyclerView, but for which the system does not have enough information to animate
7444         * them into view. In that case, the default animation for adding items is run
7445         * on those items as well.</p>
7446         *
7447         * @param holder The item that is being added.
7448         * @return true if a later call to {@link #runPendingAnimations()} is requested,
7449         * false otherwise.
7450         */
7451        abstract public boolean animateAdd(ViewHolder holder);
7452
7453        /**
7454         * Called when an item is moved in the RecyclerView. Implementors can choose
7455         * whether and how to animate that change, but must always call
7456         * {@link #dispatchMoveFinished(ViewHolder)} when done, either
7457         * immediately (if no animation will occur) or after the animation actually finishes.
7458         * The return value indicates whether an animation has been set up and whether the
7459         * ItemAnimator's {@link #runPendingAnimations()} method should be called at the
7460         * next opportunity. This mechanism allows ItemAnimator to set up individual animations
7461         * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()},
7462         * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()},
7463         * {@link #animateRemove(ViewHolder) animateRemove()}, and
7464         * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one,
7465         * then start the animations together in the later call to {@link #runPendingAnimations()}.
7466         *
7467         * @param holder The item that is being moved.
7468         * @return true if a later call to {@link #runPendingAnimations()} is requested,
7469         * false otherwise.
7470         */
7471        abstract public boolean animateMove(ViewHolder holder, int fromX, int fromY,
7472                int toX, int toY);
7473
7474        /**
7475         * Called when an item is changed in the RecyclerView, as indicated by a call to
7476         * {@link Adapter#notifyItemChanged(int)} or
7477         * {@link Adapter#notifyItemRangeChanged(int, int)}.
7478         * <p>
7479         * Implementers can choose whether and how to animate changes, but must always call
7480         * {@link #dispatchChangeFinished(ViewHolder, boolean)} for each non-null ViewHolder,
7481         * either immediately (if no animation will occur) or after the animation actually finishes.
7482         * The return value indicates whether an animation has been set up and whether the
7483         * ItemAnimator's {@link #runPendingAnimations()} method should be called at the
7484         * next opportunity. This mechanism allows ItemAnimator to set up individual animations
7485         * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()},
7486         * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()},
7487         * {@link #animateRemove(ViewHolder) animateRemove()}, and
7488         * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one,
7489         * then start the animations together in the later call to {@link #runPendingAnimations()}.
7490         *
7491         * @param oldHolder The original item that changed.
7492         * @param newHolder The new item that was created with the changed content. Might be null
7493         * @param fromLeft  Left of the old view holder
7494         * @param fromTop   Top of the old view holder
7495         * @param toLeft    Left of the new view holder
7496         * @param toTop     Top of the new view holder
7497         * @return true if a later call to {@link #runPendingAnimations()} is requested,
7498         * false otherwise.
7499         */
7500        abstract public boolean animateChange(ViewHolder oldHolder,
7501                ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop);
7502
7503
7504        /**
7505         * Method to be called by subclasses when a remove animation is done.
7506         *
7507         * @param item The item which has been removed
7508         */
7509        public final void dispatchRemoveFinished(ViewHolder item) {
7510            onRemoveFinished(item);
7511            if (mListener != null) {
7512                mListener.onRemoveFinished(item);
7513            }
7514        }
7515
7516        /**
7517         * Method to be called by subclasses when a move animation is done.
7518         *
7519         * @param item The item which has been moved
7520         */
7521        public final void dispatchMoveFinished(ViewHolder item) {
7522            onMoveFinished(item);
7523            if (mListener != null) {
7524                mListener.onMoveFinished(item);
7525            }
7526        }
7527
7528        /**
7529         * Method to be called by subclasses when an add animation is done.
7530         *
7531         * @param item The item which has been added
7532         */
7533        public final void dispatchAddFinished(ViewHolder item) {
7534            onAddFinished(item);
7535            if (mListener != null) {
7536                mListener.onAddFinished(item);
7537            }
7538        }
7539
7540        /**
7541         * @deprecated
7542         * Use {@link #dispatchChangeFinished(ViewHolder, boolean)}
7543         */
7544        @Deprecated
7545        public final void dispatchChangeFinished(ViewHolder item) {
7546            dispatchChangeFinished(item, true);
7547        }
7548
7549        /**
7550         * Method to be called by subclasses when a change animation is done.
7551         *
7552         * @see #animateChange(ViewHolder, ViewHolder, int, int, int, int)
7553         * @param item The item which has been changed (this method must be called for
7554         * each non-null ViewHolder passed into
7555         * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}).
7556         * @param oldItem true if this is the old item that was changed, false if
7557         * it is the new item that replaced the old item.
7558         */
7559        public final void dispatchChangeFinished(ViewHolder item, boolean oldItem) {
7560            onChangeFinished(item, oldItem);
7561            if (mListener != null) {
7562                mListener.onChangeFinished(item);
7563            }
7564        }
7565
7566        /**
7567         * Method to be called by subclasses when a remove animation is being started.
7568         *
7569         * @param item The item being removed
7570         */
7571        public final void dispatchRemoveStarting(ViewHolder item) {
7572            onRemoveStarting(item);
7573        }
7574
7575        /**
7576         * Method to be called by subclasses when a move animation is being started.
7577         *
7578         * @param item The item being moved
7579         */
7580        public final void dispatchMoveStarting(ViewHolder item) {
7581            onMoveStarting(item);
7582        }
7583
7584        /**
7585         * Method to be called by subclasses when an add animation is being started.
7586         *
7587         * @param item The item being added
7588         */
7589        public final void dispatchAddStarting(ViewHolder item) {
7590            onAddStarting(item);
7591        }
7592
7593        /**
7594         * Method to be called by subclasses when a change animation is being started.
7595         *
7596         * @param item The item which has been changed (this method must be called for
7597         * each non-null ViewHolder passed into
7598         * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}).
7599         * @param oldItem true if this is the old item that was changed, false if
7600         * it is the new item that replaced the old item.
7601         */
7602        public final void dispatchChangeStarting(ViewHolder item, boolean oldItem) {
7603            onChangeStarting(item, oldItem);
7604        }
7605
7606        /**
7607         * Method called when an animation on a view should be ended immediately.
7608         * This could happen when other events, like scrolling, occur, so that
7609         * animating views can be quickly put into their proper end locations.
7610         * Implementations should ensure that any animations running on the item
7611         * are canceled and affected properties are set to their end values.
7612         * Also, appropriate dispatch methods (e.g., {@link #dispatchAddFinished(ViewHolder)}
7613         * should be called since the animations are effectively done when this
7614         * method is called.
7615         *
7616         * @param item The item for which an animation should be stopped.
7617         */
7618        abstract public void endAnimation(ViewHolder item);
7619
7620        /**
7621         * Method called when all item animations should be ended immediately.
7622         * This could happen when other events, like scrolling, occur, so that
7623         * animating views can be quickly put into their proper end locations.
7624         * Implementations should ensure that any animations running on any items
7625         * are canceled and affected properties are set to their end values.
7626         * Also, appropriate dispatch methods (e.g., {@link #dispatchAddFinished(ViewHolder)}
7627         * should be called since the animations are effectively done when this
7628         * method is called.
7629         */
7630        abstract public void endAnimations();
7631
7632        /**
7633         * Method which returns whether there are any item animations currently running.
7634         * This method can be used to determine whether to delay other actions until
7635         * animations end.
7636         *
7637         * @return true if there are any item animations currently running, false otherwise.
7638         */
7639        abstract public boolean isRunning();
7640
7641        /**
7642         * Like {@link #isRunning()}, this method returns whether there are any item
7643         * animations currently running. Addtionally, the listener passed in will be called
7644         * when there are no item animations running, either immediately (before the method
7645         * returns) if no animations are currently running, or when the currently running
7646         * animations are {@link #dispatchAnimationsFinished() finished}.
7647         *
7648         * <p>Note that the listener is transient - it is either called immediately and not
7649         * stored at all, or stored only until it is called when running animations
7650         * are finished sometime later.</p>
7651         *
7652         * @param listener A listener to be called immediately if no animations are running
7653         * or later when currently-running animations have finished. A null listener is
7654         * equivalent to calling {@link #isRunning()}.
7655         * @return true if there are any item animations currently running, false otherwise.
7656         */
7657        public final boolean isRunning(ItemAnimatorFinishedListener listener) {
7658            boolean running = isRunning();
7659            if (listener != null) {
7660                if (!running) {
7661                    listener.onAnimationsFinished();
7662                } else {
7663                    mFinishedListeners.add(listener);
7664                }
7665            }
7666            return running;
7667        }
7668
7669        /**
7670         * The interface to be implemented by listeners to animation events from this
7671         * ItemAnimator. This is used internally and is not intended for developers to
7672         * create directly.
7673         */
7674        private interface ItemAnimatorListener {
7675            void onRemoveFinished(ViewHolder item);
7676            void onAddFinished(ViewHolder item);
7677            void onMoveFinished(ViewHolder item);
7678            void onChangeFinished(ViewHolder item);
7679        }
7680
7681        /**
7682         * This method should be called by ItemAnimator implementations to notify
7683         * any listeners that all pending and active item animations are finished.
7684         */
7685        public final void dispatchAnimationsFinished() {
7686            final int count = mFinishedListeners.size();
7687            for (int i = 0; i < count; ++i) {
7688                mFinishedListeners.get(i).onAnimationsFinished();
7689            }
7690            mFinishedListeners.clear();
7691        }
7692
7693        /**
7694         * This interface is used to inform listeners when all pending or running animations
7695         * in an ItemAnimator are finished. This can be used, for example, to delay an action
7696         * in a data set until currently-running animations are complete.
7697         *
7698         * @see #isRunning(ItemAnimatorFinishedListener)
7699         */
7700        public interface ItemAnimatorFinishedListener {
7701            void onAnimationsFinished();
7702        }
7703
7704        /**
7705         * Called when a remove animation is being started on the given ViewHolder.
7706         * The default implementation does nothing. Subclasses may wish to override
7707         * this method to handle any ViewHolder-specific operations linked to animation
7708         * lifecycles.
7709         *
7710         * @param item The ViewHolder being animated.
7711         */
7712        public void onRemoveStarting(ViewHolder item) {}
7713
7714        /**
7715         * Called when a remove animation has ended on the given ViewHolder.
7716         * The default implementation does nothing. Subclasses may wish to override
7717         * this method to handle any ViewHolder-specific operations linked to animation
7718         * lifecycles.
7719         *
7720         * @param item The ViewHolder being animated.
7721         */
7722        public void onRemoveFinished(ViewHolder item) {}
7723
7724        /**
7725         * Called when an add animation is being started on the given ViewHolder.
7726         * The default implementation does nothing. Subclasses may wish to override
7727         * this method to handle any ViewHolder-specific operations linked to animation
7728         * lifecycles.
7729         *
7730         * @param item The ViewHolder being animated.
7731         */
7732        public void onAddStarting(ViewHolder item) {}
7733
7734        /**
7735         * Called when an add animation has ended on the given ViewHolder.
7736         * The default implementation does nothing. Subclasses may wish to override
7737         * this method to handle any ViewHolder-specific operations linked to animation
7738         * lifecycles.
7739         *
7740         * @param item The ViewHolder being animated.
7741         */
7742        public void onAddFinished(ViewHolder item) {}
7743
7744        /**
7745         * Called when a move animation is being started on the given ViewHolder.
7746         * The default implementation does nothing. Subclasses may wish to override
7747         * this method to handle any ViewHolder-specific operations linked to animation
7748         * lifecycles.
7749         *
7750         * @param item The ViewHolder being animated.
7751         */
7752        public void onMoveStarting(ViewHolder item) {}
7753
7754        /**
7755         * Called when a move animation has ended on the given ViewHolder.
7756         * The default implementation does nothing. Subclasses may wish to override
7757         * this method to handle any ViewHolder-specific operations linked to animation
7758         * lifecycles.
7759         *
7760         * @param item The ViewHolder being animated.
7761         */
7762        public void onMoveFinished(ViewHolder item) {}
7763
7764        /**
7765         * Called when a change animation is being started on the given ViewHolder.
7766         * The default implementation does nothing. Subclasses may wish to override
7767         * this method to handle any ViewHolder-specific operations linked to animation
7768         * lifecycles.
7769         *
7770         * @param item The ViewHolder being animated.
7771         * @param oldItem true if this is the old item that was changed, false if
7772         * it is the new item that replaced the old item.
7773         */
7774        public void onChangeStarting(ViewHolder item, boolean oldItem) {}
7775
7776        /**
7777         * Called when a change animation has ended on the given ViewHolder.
7778         * The default implementation does nothing. Subclasses may wish to override
7779         * this method to handle any ViewHolder-specific operations linked to animation
7780         * lifecycles.
7781         *
7782         * @param item The ViewHolder being animated.
7783         * @param oldItem true if this is the old item that was changed, false if
7784         * it is the new item that replaced the old item.
7785         */
7786        public void onChangeFinished(ViewHolder item, boolean oldItem) {}
7787
7788    }
7789
7790    /**
7791     * Internal data structure that holds information about an item's bounds.
7792     * This information is used in calculating item animations.
7793     */
7794    private static class ItemHolderInfo {
7795        ViewHolder holder;
7796        int left, top, right, bottom;
7797
7798        ItemHolderInfo(ViewHolder holder, int left, int top, int right, int bottom) {
7799            this.holder = holder;
7800            this.left = left;
7801            this.top = top;
7802            this.right = right;
7803            this.bottom = bottom;
7804        }
7805    }
7806}
7807