ViewPager.java revision 5de8804b248eebaf7c29113ec80a0dff3a03c97a
1/*
2 * Copyright (C) 2011 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
17package android.support.v4.view;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.database.DataSetObserver;
22import android.graphics.Canvas;
23import android.graphics.Rect;
24import android.graphics.drawable.Drawable;
25import android.os.Build;
26import android.os.Parcel;
27import android.os.Parcelable;
28import android.os.SystemClock;
29import android.support.v4.os.ParcelableCompat;
30import android.support.v4.os.ParcelableCompatCreatorCallbacks;
31import android.support.v4.widget.EdgeEffectCompat;
32import android.util.AttributeSet;
33import android.util.Log;
34import android.view.FocusFinder;
35import android.view.Gravity;
36import android.view.KeyEvent;
37import android.view.MotionEvent;
38import android.view.SoundEffectConstants;
39import android.view.VelocityTracker;
40import android.view.View;
41import android.view.ViewConfiguration;
42import android.view.ViewGroup;
43import android.view.ViewParent;
44import android.view.accessibility.AccessibilityEvent;
45import android.view.animation.Interpolator;
46import android.widget.Scroller;
47
48import java.util.ArrayList;
49import java.util.Collections;
50import java.util.Comparator;
51
52/**
53 * Layout manager that allows the user to flip left and right
54 * through pages of data.  You supply an implementation of a
55 * {@link PagerAdapter} to generate the pages that the view shows.
56 *
57 * <p>Note this class is currently under early design and
58 * development.  The API will likely change in later updates of
59 * the compatibility library, requiring changes to the source code
60 * of apps when they are compiled against the newer version.</p>
61 */
62public class ViewPager extends ViewGroup {
63    private static final String TAG = "ViewPager";
64    private static final boolean DEBUG = false;
65
66    private static final boolean USE_CACHE = false;
67
68    private static final int DEFAULT_OFFSCREEN_PAGES = 1;
69    private static final int MAX_SETTLE_DURATION = 600; // ms
70
71    private static final int[] LAYOUT_ATTRS = new int[] {
72        android.R.attr.layout_gravity
73    };
74
75    static class ItemInfo {
76        Object object;
77        int position;
78        boolean scrolling;
79    }
80
81    private static final Comparator<ItemInfo> COMPARATOR = new Comparator<ItemInfo>(){
82        @Override
83        public int compare(ItemInfo lhs, ItemInfo rhs) {
84            return lhs.position - rhs.position;
85        }};
86
87    private static final Interpolator sInterpolator = new Interpolator() {
88        public float getInterpolation(float t) {
89            // _o(t) = t * t * ((tension + 1) * t + tension)
90            // o(t) = _o(t - 1) + 1
91            t -= 1.0f;
92            return t * t * t + 1.0f;
93        }
94    };
95
96    private final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
97
98    private PagerAdapter mAdapter;
99    private int mCurItem;   // Index of currently displayed page.
100    private int mRestoredCurItem = -1;
101    private Parcelable mRestoredAdapterState = null;
102    private ClassLoader mRestoredClassLoader = null;
103    private Scroller mScroller;
104    private PagerObserver mObserver;
105
106    private int mPageMargin;
107    private Drawable mMarginDrawable;
108    private int mTopPageBounds;
109    private int mBottomPageBounds;
110
111    private int mChildWidthMeasureSpec;
112    private int mChildHeightMeasureSpec;
113    private boolean mInLayout;
114
115    private boolean mScrollingCacheEnabled;
116
117    private boolean mPopulatePending;
118    private boolean mIsPopulating;
119    private boolean mScrolling;
120    private int mOffscreenPageLimit = DEFAULT_OFFSCREEN_PAGES;
121
122    private boolean mIsBeingDragged;
123    private boolean mIsUnableToDrag;
124    private int mTouchSlop;
125    private float mInitialMotionX;
126    /**
127     * Position of the last motion event.
128     */
129    private float mLastMotionX;
130    private float mLastMotionY;
131    /**
132     * ID of the active pointer. This is used to retain consistency during
133     * drags/flings if multiple pointers are used.
134     */
135    private int mActivePointerId = INVALID_POINTER;
136    /**
137     * Sentinel value for no current active pointer.
138     * Used by {@link #mActivePointerId}.
139     */
140    private static final int INVALID_POINTER = -1;
141
142    /**
143     * Determines speed during touch scrolling
144     */
145    private VelocityTracker mVelocityTracker;
146    private int mMinimumVelocity;
147    private int mMaximumVelocity;
148    private float mBaseLineFlingVelocity;
149    private float mFlingVelocityInfluence;
150
151    private boolean mFakeDragging;
152    private long mFakeDragBeginTime;
153
154    private EdgeEffectCompat mLeftEdge;
155    private EdgeEffectCompat mRightEdge;
156
157    private boolean mFirstLayout = true;
158    private boolean mCalledSuper;
159    private int mDecorChildCount;
160
161    private OnPageChangeListener mOnPageChangeListener;
162    private OnPageChangeListener mInternalPageChangeListener;
163    private OnAdapterChangeListener mAdapterChangeListener;
164
165    /**
166     * Indicates that the pager is in an idle, settled state. The current page
167     * is fully in view and no animation is in progress.
168     */
169    public static final int SCROLL_STATE_IDLE = 0;
170
171    /**
172     * Indicates that the pager is currently being dragged by the user.
173     */
174    public static final int SCROLL_STATE_DRAGGING = 1;
175
176    /**
177     * Indicates that the pager is in the process of settling to a final position.
178     */
179    public static final int SCROLL_STATE_SETTLING = 2;
180
181    private int mScrollState = SCROLL_STATE_IDLE;
182
183    /**
184     * Callback interface for responding to changing state of the selected page.
185     */
186    public interface OnPageChangeListener {
187
188        /**
189         * This method will be invoked when the current page is scrolled, either as part
190         * of a programmatically initiated smooth scroll or a user initiated touch scroll.
191         *
192         * @param position Position index of the first page currently being displayed.
193         *                 Page position+1 will be visible if positionOffset is nonzero.
194         * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
195         * @param positionOffsetPixels Value in pixels indicating the offset from position.
196         */
197        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);
198
199        /**
200         * This method will be invoked when a new page becomes selected. Animation is not
201         * necessarily complete.
202         *
203         * @param position Position index of the new selected page.
204         */
205        public void onPageSelected(int position);
206
207        /**
208         * Called when the scroll state changes. Useful for discovering when the user
209         * begins dragging, when the pager is automatically settling to the current page,
210         * or when it is fully stopped/idle.
211         *
212         * @param state The new scroll state.
213         * @see ViewPager#SCROLL_STATE_IDLE
214         * @see ViewPager#SCROLL_STATE_DRAGGING
215         * @see ViewPager#SCROLL_STATE_SETTLING
216         */
217        public void onPageScrollStateChanged(int state);
218    }
219
220    /**
221     * Simple implementation of the {@link OnPageChangeListener} interface with stub
222     * implementations of each method. Extend this if you do not intend to override
223     * every method of {@link OnPageChangeListener}.
224     */
225    public static class SimpleOnPageChangeListener implements OnPageChangeListener {
226        @Override
227        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
228            // This space for rent
229        }
230
231        @Override
232        public void onPageSelected(int position) {
233            // This space for rent
234        }
235
236        @Override
237        public void onPageScrollStateChanged(int state) {
238            // This space for rent
239        }
240    }
241
242    /**
243     * Used internally to monitor when adapters are switched.
244     */
245    interface OnAdapterChangeListener {
246        public void onAdapterChanged(PagerAdapter oldAdapter, PagerAdapter newAdapter);
247    }
248
249    /**
250     * Used internally to tag special types of child views that should be added as
251     * pager decorations by default.
252     */
253    interface Decor {}
254
255    public ViewPager(Context context) {
256        super(context);
257        initViewPager();
258    }
259
260    public ViewPager(Context context, AttributeSet attrs) {
261        super(context, attrs);
262        initViewPager();
263    }
264
265    void initViewPager() {
266        setWillNotDraw(false);
267        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
268        setFocusable(true);
269        final Context context = getContext();
270        mScroller = new Scroller(context, sInterpolator);
271        final ViewConfiguration configuration = ViewConfiguration.get(context);
272        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
273        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
274        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
275        mLeftEdge = new EdgeEffectCompat(context);
276        mRightEdge = new EdgeEffectCompat(context);
277
278        float density = context.getResources().getDisplayMetrics().density;
279        mBaseLineFlingVelocity = 2500.0f * density;
280        mFlingVelocityInfluence = 0.4f;
281    }
282
283    private void setScrollState(int newState) {
284        if (mScrollState == newState) {
285            return;
286        }
287
288        mScrollState = newState;
289        if (mOnPageChangeListener != null) {
290            mOnPageChangeListener.onPageScrollStateChanged(newState);
291        }
292    }
293
294    /**
295     * Set a PagerAdapter that will supply views for this pager as needed.
296     *
297     * @param adapter Adapter to use
298     */
299    public void setAdapter(PagerAdapter adapter) {
300        if (mAdapter != null) {
301            mAdapter.unregisterDataSetObserver(mObserver);
302            mAdapter.startUpdate(this);
303            for (int i = 0; i < mItems.size(); i++) {
304                final ItemInfo ii = mItems.get(i);
305                mAdapter.destroyItem(this, ii.position, ii.object);
306            }
307            mAdapter.finishUpdate(this);
308            mItems.clear();
309            removeNonDecorViews();
310            mCurItem = 0;
311            scrollTo(0, 0);
312        }
313
314        final PagerAdapter oldAdapter = mAdapter;
315        mAdapter = adapter;
316
317        if (mAdapter != null) {
318            if (mObserver == null) {
319                mObserver = new PagerObserver();
320            }
321            mAdapter.registerDataSetObserver(mObserver);
322            mPopulatePending = false;
323            if (mRestoredCurItem >= 0) {
324                mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader);
325                setCurrentItemInternal(mRestoredCurItem, false, true);
326                mRestoredCurItem = -1;
327                mRestoredAdapterState = null;
328                mRestoredClassLoader = null;
329            } else {
330                populate();
331            }
332        }
333
334        if (mAdapterChangeListener != null && oldAdapter != adapter) {
335            mAdapterChangeListener.onAdapterChanged(oldAdapter, adapter);
336        }
337    }
338
339    private void removeNonDecorViews() {
340        for (int i = 0; i < getChildCount(); i++) {
341            final View child = getChildAt(i);
342            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
343            if (!lp.isDecor) {
344                removeViewAt(i);
345                i--;
346            }
347        }
348    }
349
350    /**
351     * Retrieve the current adapter supplying pages.
352     *
353     * @return The currently registered PagerAdapter
354     */
355    public PagerAdapter getAdapter() {
356        return mAdapter;
357    }
358
359    void setOnAdapterChangeListener(OnAdapterChangeListener listener) {
360        mAdapterChangeListener = listener;
361    }
362
363    /**
364     * Set the currently selected page. If the ViewPager has already been through its first
365     * layout there will be a smooth animated transition between the current item and the
366     * specified item.
367     *
368     * @param item Item index to select
369     */
370    public void setCurrentItem(int item) {
371        mPopulatePending = false;
372        setCurrentItemInternal(item, !mFirstLayout, false);
373    }
374
375    /**
376     * Set the currently selected page.
377     *
378     * @param item Item index to select
379     * @param smoothScroll True to smoothly scroll to the new item, false to transition immediately
380     */
381    public void setCurrentItem(int item, boolean smoothScroll) {
382        mPopulatePending = false;
383        setCurrentItemInternal(item, smoothScroll, false);
384    }
385
386    public int getCurrentItem() {
387        return mCurItem;
388    }
389
390    void setCurrentItemInternal(int item, boolean smoothScroll, boolean always) {
391        setCurrentItemInternal(item, smoothScroll, always, 0);
392    }
393
394    void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {
395        if (mAdapter == null || mAdapter.getCount() <= 0) {
396            setScrollingCacheEnabled(false);
397            return;
398        }
399        if (!always && mCurItem == item && mItems.size() != 0) {
400            setScrollingCacheEnabled(false);
401            return;
402        }
403        if (item < 0) {
404            item = 0;
405        } else if (item >= mAdapter.getCount()) {
406            item = mAdapter.getCount() - 1;
407        }
408        final int pageLimit = mOffscreenPageLimit;
409        if (item > (mCurItem + pageLimit) || item < (mCurItem - pageLimit)) {
410            // We are doing a jump by more than one page.  To avoid
411            // glitches, we want to keep all current pages in the view
412            // until the scroll ends.
413            for (int i=0; i<mItems.size(); i++) {
414                mItems.get(i).scrolling = true;
415            }
416        }
417        final boolean dispatchSelected = mCurItem != item;
418        mCurItem = item;
419        populate();
420        final int destX = (getWidth() + mPageMargin) * item;
421        if (smoothScroll) {
422            smoothScrollTo(destX, 0, velocity);
423            if (dispatchSelected && mOnPageChangeListener != null) {
424                mOnPageChangeListener.onPageSelected(item);
425            }
426            if (dispatchSelected && mInternalPageChangeListener != null) {
427                mInternalPageChangeListener.onPageSelected(item);
428            }
429        } else {
430            if (dispatchSelected && mOnPageChangeListener != null) {
431                mOnPageChangeListener.onPageSelected(item);
432            }
433            if (dispatchSelected && mInternalPageChangeListener != null) {
434                mInternalPageChangeListener.onPageSelected(item);
435            }
436            completeScroll();
437            scrollTo(destX, 0);
438        }
439    }
440
441    /**
442     * Set a listener that will be invoked whenever the page changes or is incrementally
443     * scrolled. See {@link OnPageChangeListener}.
444     *
445     * @param listener Listener to set
446     */
447    public void setOnPageChangeListener(OnPageChangeListener listener) {
448        mOnPageChangeListener = listener;
449    }
450
451    /**
452     * Set a separate OnPageChangeListener for internal use by the support library.
453     *
454     * @param listener Listener to set
455     * @return The old listener that was set, if any.
456     */
457    OnPageChangeListener setInternalPageChangeListener(OnPageChangeListener listener) {
458        OnPageChangeListener oldListener = mInternalPageChangeListener;
459        mInternalPageChangeListener = listener;
460        return oldListener;
461    }
462
463    /**
464     * Returns the number of pages that will be retained to either side of the
465     * current page in the view hierarchy in an idle state. Defaults to 1.
466     *
467     * @return How many pages will be kept offscreen on either side
468     * @see #setOffscreenPageLimit(int)
469     */
470    public int getOffscreenPageLimit() {
471        return mOffscreenPageLimit;
472    }
473
474    /**
475     * Set the number of pages that should be retained to either side of the
476     * current page in the view hierarchy in an idle state. Pages beyond this
477     * limit will be recreated from the adapter when needed.
478     *
479     * <p>This is offered as an optimization. If you know in advance the number
480     * of pages you will need to support or have lazy-loading mechanisms in place
481     * on your pages, tweaking this setting can have benefits in perceived smoothness
482     * of paging animations and interaction. If you have a small number of pages (3-4)
483     * that you can keep active all at once, less time will be spent in layout for
484     * newly created view subtrees as the user pages back and forth.</p>
485     *
486     * <p>You should keep this limit low, especially if your pages have complex layouts.
487     * This setting defaults to 1.</p>
488     *
489     * @param limit How many pages will be kept offscreen in an idle state.
490     */
491    public void setOffscreenPageLimit(int limit) {
492        if (limit < DEFAULT_OFFSCREEN_PAGES) {
493            Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " +
494                    DEFAULT_OFFSCREEN_PAGES);
495            limit = DEFAULT_OFFSCREEN_PAGES;
496        }
497        if (limit != mOffscreenPageLimit) {
498            mOffscreenPageLimit = limit;
499            populate();
500        }
501    }
502
503    /**
504     * Set the margin between pages.
505     *
506     * @param marginPixels Distance between adjacent pages in pixels
507     * @see #getPageMargin()
508     * @see #setPageMarginDrawable(Drawable)
509     * @see #setPageMarginDrawable(int)
510     */
511    public void setPageMargin(int marginPixels) {
512        final int oldMargin = mPageMargin;
513        mPageMargin = marginPixels;
514
515        final int width = getWidth();
516        recomputeScrollPosition(width, width, marginPixels, oldMargin);
517
518        requestLayout();
519    }
520
521    /**
522     * Return the margin between pages.
523     *
524     * @return The size of the margin in pixels
525     */
526    public int getPageMargin() {
527        return mPageMargin;
528    }
529
530    /**
531     * Set a drawable that will be used to fill the margin between pages.
532     *
533     * @param d Drawable to display between pages
534     */
535    public void setPageMarginDrawable(Drawable d) {
536        mMarginDrawable = d;
537        if (d != null) refreshDrawableState();
538        setWillNotDraw(d == null);
539        invalidate();
540    }
541
542    /**
543     * Set a drawable that will be used to fill the margin between pages.
544     *
545     * @param resId Resource ID of a drawable to display between pages
546     */
547    public void setPageMarginDrawable(int resId) {
548        setPageMarginDrawable(getContext().getResources().getDrawable(resId));
549    }
550
551    @Override
552    protected boolean verifyDrawable(Drawable who) {
553        return super.verifyDrawable(who) || who == mMarginDrawable;
554    }
555
556    @Override
557    protected void drawableStateChanged() {
558        super.drawableStateChanged();
559        final Drawable d = mMarginDrawable;
560        if (d != null && d.isStateful()) {
561            d.setState(getDrawableState());
562        }
563    }
564
565    // We want the duration of the page snap animation to be influenced by the distance that
566    // the screen has to travel, however, we don't want this duration to be effected in a
567    // purely linear fashion. Instead, we use this method to moderate the effect that the distance
568    // of travel has on the overall snap duration.
569    float distanceInfluenceForSnapDuration(float f) {
570        f -= 0.5f; // center the values about 0.
571        f *= 0.3f * Math.PI / 2.0f;
572        return (float) Math.sin(f);
573    }
574
575    /**
576     * Like {@link View#scrollBy}, but scroll smoothly instead of immediately.
577     *
578     * @param x the number of pixels to scroll by on the X axis
579     * @param y the number of pixels to scroll by on the Y axis
580     */
581    void smoothScrollTo(int x, int y) {
582        smoothScrollTo(x, y, 0);
583    }
584
585    /**
586     * Like {@link View#scrollBy}, but scroll smoothly instead of immediately.
587     *
588     * @param x the number of pixels to scroll by on the X axis
589     * @param y the number of pixels to scroll by on the Y axis
590     * @param velocity the velocity associated with a fling, if applicable. (0 otherwise)
591     */
592    void smoothScrollTo(int x, int y, int velocity) {
593        if (getChildCount() == 0) {
594            // Nothing to do.
595            setScrollingCacheEnabled(false);
596            return;
597        }
598        int sx = getScrollX();
599        int sy = getScrollY();
600        int dx = x - sx;
601        int dy = y - sy;
602        if (dx == 0 && dy == 0) {
603            completeScroll();
604            setScrollState(SCROLL_STATE_IDLE);
605            return;
606        }
607
608        setScrollingCacheEnabled(true);
609        mScrolling = true;
610        setScrollState(SCROLL_STATE_SETTLING);
611
612        final float pageDelta = (float) Math.abs(dx) / (getWidth() + mPageMargin);
613        int duration = (int) (pageDelta * 100);
614
615        velocity = Math.abs(velocity);
616        if (velocity > 0) {
617            duration += (duration / (velocity / mBaseLineFlingVelocity)) * mFlingVelocityInfluence;
618        } else {
619            duration += 100;
620        }
621        duration = Math.min(duration, MAX_SETTLE_DURATION);
622
623        mScroller.startScroll(sx, sy, dx, dy, duration);
624        invalidate();
625    }
626
627    void addNewItem(int position, int index) {
628        ItemInfo ii = new ItemInfo();
629        ii.position = position;
630        ii.object = mAdapter.instantiateItem(this, position);
631        if (index < 0) {
632            mItems.add(ii);
633        } else {
634            mItems.add(index, ii);
635        }
636    }
637
638    void dataSetChanged() {
639        // This method only gets called if our observer is attached, so mAdapter is non-null.
640
641        boolean needPopulate = mItems.size() < 3 && mItems.size() < mAdapter.getCount();
642        int newCurrItem = -1;
643
644        boolean isUpdating = false;
645        for (int i = 0; i < mItems.size(); i++) {
646            final ItemInfo ii = mItems.get(i);
647            final int newPos = mAdapter.getItemPosition(ii.object);
648
649            if (newPos == PagerAdapter.POSITION_UNCHANGED) {
650                continue;
651            }
652
653            if (newPos == PagerAdapter.POSITION_NONE) {
654                mItems.remove(i);
655                i--;
656
657                if (!isUpdating) {
658                    mAdapter.startUpdate(this);
659                    isUpdating = true;
660                }
661
662                mAdapter.destroyItem(this, ii.position, ii.object);
663                needPopulate = true;
664
665                if (mCurItem == ii.position) {
666                    // Keep the current item in the valid range
667                    newCurrItem = Math.max(0, Math.min(mCurItem, mAdapter.getCount() - 1));
668                }
669                continue;
670            }
671
672            if (ii.position != newPos) {
673                if (ii.position == mCurItem) {
674                    // Our current item changed position. Follow it.
675                    newCurrItem = newPos;
676                }
677
678                ii.position = newPos;
679                needPopulate = true;
680            }
681        }
682
683        if (isUpdating) {
684            mAdapter.finishUpdate(this);
685        }
686
687        Collections.sort(mItems, COMPARATOR);
688
689        if (newCurrItem >= 0) {
690            // TODO This currently causes a jump.
691            setCurrentItemInternal(newCurrItem, false, true);
692            needPopulate = true;
693        }
694        if (needPopulate) {
695            populate();
696            requestLayout();
697        }
698    }
699
700    void populate() {
701        if (mAdapter == null) {
702            return;
703        }
704
705        // Bail now if we are waiting to populate.  This is to hold off
706        // on creating views from the time the user releases their finger to
707        // fling to a new position until we have finished the scroll to
708        // that position, avoiding glitches from happening at that point.
709        if (mPopulatePending) {
710            if (DEBUG) Log.i(TAG, "populate is pending, skipping for now...");
711            return;
712        }
713
714        // Also, don't populate until we are attached to a window.  This is to
715        // avoid trying to populate before we have restored our view hierarchy
716        // state and conflicting with what is restored.
717        if (getWindowToken() == null) {
718            return;
719        }
720
721        mIsPopulating = true;
722        mAdapter.startUpdate(this);
723
724        final int pageLimit = mOffscreenPageLimit;
725        final int startPos = Math.max(0, mCurItem - pageLimit);
726        final int N = mAdapter.getCount();
727        final int endPos = Math.min(N-1, mCurItem + pageLimit);
728
729        if (DEBUG) Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);
730
731        // Add and remove pages in the existing list.
732        int lastPos = -1;
733        for (int i=0; i<mItems.size(); i++) {
734            ItemInfo ii = mItems.get(i);
735            if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
736                if (DEBUG) Log.i(TAG, "removing: " + ii.position + " @ " + i);
737                mItems.remove(i);
738                i--;
739                mAdapter.destroyItem(this, ii.position, ii.object);
740            } else if (lastPos < endPos && ii.position > startPos) {
741                // The next item is outside of our range, but we have a gap
742                // between it and the last item where we want to have a page
743                // shown.  Fill in the gap.
744                lastPos++;
745                if (lastPos < startPos) {
746                    lastPos = startPos;
747                }
748                while (lastPos <= endPos && lastPos < ii.position) {
749                    if (DEBUG) Log.i(TAG, "inserting: " + lastPos + " @ " + i);
750                    addNewItem(lastPos, i);
751                    lastPos++;
752                    i++;
753                }
754            }
755            lastPos = ii.position;
756        }
757
758        // Add any new pages we need at the end.
759        lastPos = mItems.size() > 0 ? mItems.get(mItems.size()-1).position : -1;
760        if (lastPos < endPos) {
761            lastPos++;
762            lastPos = lastPos > startPos ? lastPos : startPos;
763            while (lastPos <= endPos) {
764                if (DEBUG) Log.i(TAG, "appending: " + lastPos);
765                addNewItem(lastPos, -1);
766                lastPos++;
767            }
768        }
769
770        if (DEBUG) {
771            Log.i(TAG, "Current page list:");
772            for (int i=0; i<mItems.size(); i++) {
773                Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
774            }
775        }
776
777        ItemInfo curItem = null;
778        for (int i=0; i<mItems.size(); i++) {
779            if (mItems.get(i).position == mCurItem) {
780                curItem = mItems.get(i);
781                break;
782            }
783        }
784        mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);
785
786        mAdapter.finishUpdate(this);
787        mIsPopulating = false;
788
789        if (hasFocus()) {
790            View currentFocused = findFocus();
791            ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
792            if (ii == null || ii.position != mCurItem) {
793                for (int i=0; i<getChildCount(); i++) {
794                    View child = getChildAt(i);
795                    ii = infoForChild(child);
796                    if (ii != null && ii.position == mCurItem) {
797                        if (child.requestFocus(FOCUS_FORWARD)) {
798                            break;
799                        }
800                    }
801                }
802            }
803        }
804    }
805
806    public static class SavedState extends BaseSavedState {
807        int position;
808        Parcelable adapterState;
809        ClassLoader loader;
810
811        public SavedState(Parcelable superState) {
812            super(superState);
813        }
814
815        @Override
816        public void writeToParcel(Parcel out, int flags) {
817            super.writeToParcel(out, flags);
818            out.writeInt(position);
819            out.writeParcelable(adapterState, flags);
820        }
821
822        @Override
823        public String toString() {
824            return "FragmentPager.SavedState{"
825                    + Integer.toHexString(System.identityHashCode(this))
826                    + " position=" + position + "}";
827        }
828
829        public static final Parcelable.Creator<SavedState> CREATOR
830                = ParcelableCompat.newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {
831                    @Override
832                    public SavedState createFromParcel(Parcel in, ClassLoader loader) {
833                        return new SavedState(in, loader);
834                    }
835                    @Override
836                    public SavedState[] newArray(int size) {
837                        return new SavedState[size];
838                    }
839                });
840
841        SavedState(Parcel in, ClassLoader loader) {
842            super(in);
843            if (loader == null) {
844                loader = getClass().getClassLoader();
845            }
846            position = in.readInt();
847            adapterState = in.readParcelable(loader);
848            this.loader = loader;
849        }
850    }
851
852    @Override
853    public Parcelable onSaveInstanceState() {
854        Parcelable superState = super.onSaveInstanceState();
855        SavedState ss = new SavedState(superState);
856        ss.position = mCurItem;
857        if (mAdapter != null) {
858            ss.adapterState = mAdapter.saveState();
859        }
860        return ss;
861    }
862
863    @Override
864    public void onRestoreInstanceState(Parcelable state) {
865        if (!(state instanceof SavedState)) {
866            super.onRestoreInstanceState(state);
867            return;
868        }
869
870        SavedState ss = (SavedState)state;
871        super.onRestoreInstanceState(ss.getSuperState());
872
873        if (mAdapter != null) {
874            mAdapter.restoreState(ss.adapterState, ss.loader);
875            setCurrentItemInternal(ss.position, false, true);
876        } else {
877            mRestoredCurItem = ss.position;
878            mRestoredAdapterState = ss.adapterState;
879            mRestoredClassLoader = ss.loader;
880        }
881    }
882
883    @Override
884    public void addView(View child, int index, ViewGroup.LayoutParams params) {
885        if (!checkLayoutParams(params)) {
886            params = generateLayoutParams(params);
887        }
888        final LayoutParams lp = (LayoutParams) params;
889        lp.isDecor |= child instanceof Decor;
890        if (mInLayout) {
891            if (lp != null && lp.isDecor) {
892                throw new IllegalStateException("Cannot add pager decor view during layout");
893            }
894            addViewInLayout(child, index, params);
895            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
896        } else {
897            super.addView(child, index, params);
898        }
899
900        if (USE_CACHE) {
901            if (child.getVisibility() != GONE) {
902                child.setDrawingCacheEnabled(mScrollingCacheEnabled);
903            } else {
904                child.setDrawingCacheEnabled(false);
905            }
906        }
907    }
908
909    ItemInfo infoForChild(View child) {
910        for (int i=0; i<mItems.size(); i++) {
911            ItemInfo ii = mItems.get(i);
912            if (mAdapter.isViewFromObject(child, ii.object)) {
913                return ii;
914            }
915        }
916        return null;
917    }
918
919    ItemInfo infoForAnyChild(View child) {
920        ViewParent parent;
921        while ((parent=child.getParent()) != this) {
922            if (parent == null || !(parent instanceof View)) {
923                return null;
924            }
925            child = (View)parent;
926        }
927        return infoForChild(child);
928    }
929
930    @Override
931    protected void onAttachedToWindow() {
932        super.onAttachedToWindow();
933        mFirstLayout = true;
934    }
935
936    @Override
937    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
938        // For simple implementation, or internal size is always 0.
939        // We depend on the container to specify the layout size of
940        // our view.  We can't really know what it is since we will be
941        // adding and removing different arbitrary views and do not
942        // want the layout to change as this happens.
943        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
944                getDefaultSize(0, heightMeasureSpec));
945
946        // Children are just made to fill our space.
947        int childWidthSize = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
948        int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
949
950        /*
951         * Make sure all children have been properly measured. Decor views first.
952         * Right now we cheat and make this less complicated by assuming decor
953         * views won't intersect. We will pin to edges based on gravity.
954         */
955        int size = getChildCount();
956        for (int i = 0; i < size; ++i) {
957            final View child = getChildAt(i);
958            if (child.getVisibility() != GONE) {
959                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
960                if (lp != null && lp.isDecor) {
961                    final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
962                    final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
963                    Log.d(TAG, "gravity: " + lp.gravity + " hgrav: " + hgrav + " vgrav: " + vgrav);
964                    int widthMode = MeasureSpec.AT_MOST;
965                    int heightMode = MeasureSpec.AT_MOST;
966                    boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
967                    boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;
968
969                    if (consumeVertical) {
970                        widthMode = MeasureSpec.EXACTLY;
971                    } else if (consumeHorizontal) {
972                        heightMode = MeasureSpec.EXACTLY;
973                    }
974
975                    final int widthSpec = MeasureSpec.makeMeasureSpec(childWidthSize, widthMode);
976                    final int heightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, heightMode);
977                    child.measure(widthSpec, heightSpec);
978
979                    if (consumeVertical) {
980                        childHeightSize -= child.getMeasuredHeight();
981                    } else if (consumeHorizontal) {
982                        childWidthSize -= child.getMeasuredWidth();
983                    }
984                }
985            }
986        }
987
988        mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
989        mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);
990
991        // Make sure we have created all fragments that we need to have shown.
992        mInLayout = true;
993        populate();
994        mInLayout = false;
995
996        // Page views next.
997        size = getChildCount();
998        for (int i = 0; i < size; ++i) {
999            final View child = getChildAt(i);
1000            if (child.getVisibility() != GONE) {
1001                if (DEBUG) Log.v(TAG, "Measuring #" + i + " " + child
1002                        + ": " + mChildWidthMeasureSpec);
1003
1004                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1005                if (lp == null || !lp.isDecor) {
1006                    child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
1007                }
1008            }
1009        }
1010    }
1011
1012    @Override
1013    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1014        super.onSizeChanged(w, h, oldw, oldh);
1015
1016        // Make sure scroll position is set correctly.
1017        if (w != oldw) {
1018            recomputeScrollPosition(w, oldw, mPageMargin, mPageMargin);
1019        }
1020    }
1021
1022    private void recomputeScrollPosition(int width, int oldWidth, int margin, int oldMargin) {
1023        final int widthWithMargin = width + margin;
1024        if (oldWidth > 0) {
1025            final int oldScrollPos = getScrollX();
1026            final int oldwwm = oldWidth + oldMargin;
1027            final int oldScrollItem = oldScrollPos / oldwwm;
1028            final float scrollOffset = (float) (oldScrollPos % oldwwm) / oldwwm;
1029            final int scrollPos = (int) ((oldScrollItem + scrollOffset) * widthWithMargin);
1030            scrollTo(scrollPos, getScrollY());
1031            if (!mScroller.isFinished()) {
1032                // We now return to your regularly scheduled scroll, already in progress.
1033                final int newDuration = mScroller.getDuration() - mScroller.timePassed();
1034                mScroller.startScroll(scrollPos, 0, mCurItem * widthWithMargin, 0, newDuration);
1035            }
1036        } else {
1037            int scrollPos = mCurItem * widthWithMargin;
1038            if (scrollPos != getScrollX()) {
1039                completeScroll();
1040                scrollTo(scrollPos, getScrollY());
1041            }
1042        }
1043    }
1044
1045    @Override
1046    protected void onLayout(boolean changed, int l, int t, int r, int b) {
1047        mInLayout = true;
1048        populate();
1049        mInLayout = false;
1050
1051        final int count = getChildCount();
1052        int width = r - l;
1053        int height = b - t;
1054        int paddingLeft = getPaddingLeft();
1055        int paddingTop = getPaddingTop();
1056        int paddingRight = getPaddingRight();
1057        int paddingBottom = getPaddingBottom();
1058        final int scrollX = getScrollX();
1059
1060        int decorCount = 0;
1061
1062        for (int i = 0; i < count; i++) {
1063            final View child = getChildAt(i);
1064            if (child.getVisibility() != GONE) {
1065                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1066                ItemInfo ii;
1067                int childLeft = 0;
1068                int childTop = 0;
1069                if (lp.isDecor) {
1070                    final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
1071                    final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
1072                    switch (hgrav) {
1073                        default:
1074                            childLeft = paddingLeft;
1075                            break;
1076                        case Gravity.LEFT:
1077                            childLeft = paddingLeft;
1078                            paddingLeft += child.getMeasuredWidth();
1079                            break;
1080                        case Gravity.CENTER_HORIZONTAL:
1081                            childLeft = Math.max((width - child.getMeasuredWidth()) / 2,
1082                                    paddingLeft);
1083                            break;
1084                        case Gravity.RIGHT:
1085                            childLeft = width - paddingRight - child.getMeasuredWidth();
1086                            paddingRight += child.getMeasuredWidth();
1087                            break;
1088                    }
1089                    switch (vgrav) {
1090                        default:
1091                            childTop = paddingTop;
1092                            break;
1093                        case Gravity.TOP:
1094                            childTop = paddingTop;
1095                            paddingTop += child.getMeasuredHeight();
1096                            break;
1097                        case Gravity.CENTER_VERTICAL:
1098                            childTop = Math.max((height - child.getMeasuredHeight()) / 2,
1099                                    paddingTop);
1100                            break;
1101                        case Gravity.BOTTOM:
1102                            childTop = height - paddingBottom - child.getMeasuredHeight();
1103                            paddingBottom += child.getMeasuredHeight();
1104                            break;
1105                    }
1106                    childLeft += scrollX;
1107                    decorCount++;
1108                    child.layout(childLeft, childTop,
1109                            childLeft + child.getMeasuredWidth(),
1110                            childTop + child.getMeasuredHeight());
1111                } else if ((ii = infoForChild(child)) != null) {
1112                    int loff = (width + mPageMargin) * ii.position;
1113                    childLeft = paddingLeft + loff;
1114                    childTop = paddingTop;
1115                    if (DEBUG) Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object
1116                            + ":" + childLeft + "," + childTop + " " + child.getMeasuredWidth()
1117                            + "x" + child.getMeasuredHeight());
1118                    child.layout(childLeft, childTop,
1119                            childLeft + child.getMeasuredWidth(),
1120                            childTop + child.getMeasuredHeight());
1121                }
1122            }
1123        }
1124        mTopPageBounds = paddingTop;
1125        mBottomPageBounds = height - paddingBottom;
1126        mDecorChildCount = decorCount;
1127        mFirstLayout = false;
1128    }
1129
1130    @Override
1131    public void computeScroll() {
1132        if (DEBUG) Log.i(TAG, "computeScroll: finished=" + mScroller.isFinished());
1133        if (!mScroller.isFinished()) {
1134            if (mScroller.computeScrollOffset()) {
1135                if (DEBUG) Log.i(TAG, "computeScroll: still scrolling");
1136                int oldX = getScrollX();
1137                int oldY = getScrollY();
1138                int x = mScroller.getCurrX();
1139                int y = mScroller.getCurrY();
1140
1141                if (oldX != x || oldY != y) {
1142                    scrollTo(x, y);
1143                    pageScrolled(x);
1144                }
1145
1146                // Keep on drawing until the animation has finished.
1147                invalidate();
1148                return;
1149            }
1150        }
1151
1152        // Done with scroll, clean up state.
1153        completeScroll();
1154    }
1155
1156    private void pageScrolled(int xpos) {
1157        final int widthWithMargin = getWidth() + mPageMargin;
1158        final int position = xpos / widthWithMargin;
1159        final int offsetPixels = xpos % widthWithMargin;
1160        final float offset = (float) offsetPixels / widthWithMargin;
1161
1162        mCalledSuper = false;
1163        onPageScrolled(position, offset, offsetPixels);
1164        if (!mCalledSuper) {
1165            throw new IllegalStateException(
1166                    "onPageScrolled did not call superclass implementation");
1167        }
1168    }
1169
1170    /**
1171     * This method will be invoked when the current page is scrolled, either as part
1172     * of a programmatically initiated smooth scroll or a user initiated touch scroll.
1173     * If you override this method you must call through to the superclass implementation
1174     * (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
1175     * returns.
1176     *
1177     * @param position Position index of the first page currently being displayed.
1178     *                 Page position+1 will be visible if positionOffset is nonzero.
1179     * @param offset Value from [0, 1) indicating the offset from the page at position.
1180     * @param offsetPixels Value in pixels indicating the offset from position.
1181     */
1182    protected void onPageScrolled(int position, float offset, int offsetPixels) {
1183        // Offset any decor views if needed - keep them on-screen at all times.
1184        if (mDecorChildCount > 0) {
1185            final int scrollX = getScrollX();
1186            int paddingLeft = getPaddingLeft();
1187            int paddingRight = getPaddingRight();
1188            final int width = getWidth();
1189            final int childCount = getChildCount();
1190            for (int i = 0; i < childCount; i++) {
1191                final View child = getChildAt(i);
1192                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1193                if (!lp.isDecor) continue;
1194
1195                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
1196                int childLeft = 0;
1197                switch (hgrav) {
1198                    default:
1199                        childLeft = paddingLeft;
1200                        break;
1201                    case Gravity.LEFT:
1202                        childLeft = paddingLeft;
1203                        paddingLeft += child.getWidth();
1204                        break;
1205                    case Gravity.CENTER_HORIZONTAL:
1206                        childLeft = Math.max((width - child.getMeasuredWidth()) / 2,
1207                                paddingLeft);
1208                        break;
1209                    case Gravity.RIGHT:
1210                        childLeft = width - paddingRight - child.getMeasuredWidth();
1211                        paddingRight += child.getMeasuredWidth();
1212                        break;
1213                }
1214                childLeft += scrollX;
1215
1216                final int childOffset = childLeft - child.getLeft();
1217                if (childOffset != 0) {
1218                    child.offsetLeftAndRight(childOffset);
1219                }
1220            }
1221        }
1222
1223        if (mOnPageChangeListener != null) {
1224            mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
1225        }
1226        if (mInternalPageChangeListener != null) {
1227            mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
1228        }
1229        mCalledSuper = true;
1230    }
1231
1232    private void completeScroll() {
1233        boolean needPopulate = mScrolling;
1234        if (needPopulate) {
1235            // Done with scroll, no longer want to cache view drawing.
1236            setScrollingCacheEnabled(false);
1237            mScroller.abortAnimation();
1238            int oldX = getScrollX();
1239            int oldY = getScrollY();
1240            int x = mScroller.getCurrX();
1241            int y = mScroller.getCurrY();
1242            if (oldX != x || oldY != y) {
1243                scrollTo(x, y);
1244            }
1245            setScrollState(SCROLL_STATE_IDLE);
1246        }
1247        mPopulatePending = false;
1248        mScrolling = false;
1249        for (int i=0; i<mItems.size(); i++) {
1250            ItemInfo ii = mItems.get(i);
1251            if (ii.scrolling) {
1252                needPopulate = true;
1253                ii.scrolling = false;
1254            }
1255        }
1256        if (needPopulate) {
1257            populate();
1258        }
1259    }
1260
1261    @Override
1262    public boolean onInterceptTouchEvent(MotionEvent ev) {
1263        /*
1264         * This method JUST determines whether we want to intercept the motion.
1265         * If we return true, onMotionEvent will be called and we do the actual
1266         * scrolling there.
1267         */
1268
1269        final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
1270
1271        // Always take care of the touch gesture being complete.
1272        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
1273            // Release the drag.
1274            if (DEBUG) Log.v(TAG, "Intercept done!");
1275            mIsBeingDragged = false;
1276            mIsUnableToDrag = false;
1277            mActivePointerId = INVALID_POINTER;
1278            if (mVelocityTracker != null) {
1279                mVelocityTracker.recycle();
1280                mVelocityTracker = null;
1281            }
1282            return false;
1283        }
1284
1285        // Nothing more to do here if we have decided whether or not we
1286        // are dragging.
1287        if (action != MotionEvent.ACTION_DOWN) {
1288            if (mIsBeingDragged) {
1289                if (DEBUG) Log.v(TAG, "Intercept returning true!");
1290                return true;
1291            }
1292            if (mIsUnableToDrag) {
1293                if (DEBUG) Log.v(TAG, "Intercept returning false!");
1294                return false;
1295            }
1296        }
1297
1298        switch (action) {
1299            case MotionEvent.ACTION_MOVE: {
1300                /*
1301                 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1302                 * whether the user has moved far enough from his original down touch.
1303                 */
1304
1305                /*
1306                * Locally do absolute value. mLastMotionY is set to the y value
1307                * of the down event.
1308                */
1309                final int activePointerId = mActivePointerId;
1310                if (activePointerId == INVALID_POINTER) {
1311                    // If we don't have a valid id, the touch down wasn't on content.
1312                    break;
1313                }
1314
1315                final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
1316                final float x = MotionEventCompat.getX(ev, pointerIndex);
1317                final float dx = x - mLastMotionX;
1318                final float xDiff = Math.abs(dx);
1319                final float y = MotionEventCompat.getY(ev, pointerIndex);
1320                final float yDiff = Math.abs(y - mLastMotionY);
1321                final int scrollX = getScrollX();
1322                if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
1323
1324                if (canScroll(this, false, (int) dx, (int) x, (int) y)) {
1325                    // Nested view has scrollable area under this point. Let it be handled there.
1326                    mInitialMotionX = mLastMotionX = x;
1327                    mLastMotionY = y;
1328                    return false;
1329                }
1330                if (xDiff > mTouchSlop && xDiff > yDiff) {
1331                    if (DEBUG) Log.v(TAG, "Starting drag!");
1332                    mIsBeingDragged = true;
1333                    setScrollState(SCROLL_STATE_DRAGGING);
1334                    mLastMotionX = x;
1335                    setScrollingCacheEnabled(true);
1336                } else {
1337                    if (yDiff > mTouchSlop) {
1338                        // The finger has moved enough in the vertical
1339                        // direction to be counted as a drag...  abort
1340                        // any attempt to drag horizontally, to work correctly
1341                        // with children that have scrolling containers.
1342                        if (DEBUG) Log.v(TAG, "Starting unable to drag!");
1343                        mIsUnableToDrag = true;
1344                    }
1345                }
1346                break;
1347            }
1348
1349            case MotionEvent.ACTION_DOWN: {
1350                /*
1351                 * Remember location of down touch.
1352                 * ACTION_DOWN always refers to pointer index 0.
1353                 */
1354                mLastMotionX = mInitialMotionX = ev.getX();
1355                mLastMotionY = ev.getY();
1356                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
1357
1358                if (mScrollState == SCROLL_STATE_SETTLING) {
1359                    // Let the user 'catch' the pager as it animates.
1360                    mIsBeingDragged = true;
1361                    mIsUnableToDrag = false;
1362                    setScrollState(SCROLL_STATE_DRAGGING);
1363                } else {
1364                    completeScroll();
1365                    mIsBeingDragged = false;
1366                    mIsUnableToDrag = false;
1367                }
1368
1369                if (DEBUG) Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY
1370                        + " mIsBeingDragged=" + mIsBeingDragged
1371                        + "mIsUnableToDrag=" + mIsUnableToDrag);
1372                break;
1373            }
1374
1375            case MotionEventCompat.ACTION_POINTER_UP:
1376                onSecondaryPointerUp(ev);
1377                break;
1378        }
1379
1380        if (!mIsBeingDragged) {
1381            // Track the velocity as long as we aren't dragging.
1382            // Once we start a real drag we will track in onTouchEvent.
1383            if (mVelocityTracker == null) {
1384                mVelocityTracker = VelocityTracker.obtain();
1385            }
1386            mVelocityTracker.addMovement(ev);
1387        }
1388
1389        /*
1390         * The only time we want to intercept motion events is if we are in the
1391         * drag mode.
1392         */
1393        return mIsBeingDragged;
1394    }
1395
1396    @Override
1397    public boolean onTouchEvent(MotionEvent ev) {
1398        if (mFakeDragging) {
1399            // A fake drag is in progress already, ignore this real one
1400            // but still eat the touch events.
1401            // (It is likely that the user is multi-touching the screen.)
1402            return true;
1403        }
1404
1405        if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
1406            // Don't handle edge touches immediately -- they may actually belong to one of our
1407            // descendants.
1408            return false;
1409        }
1410
1411        if (mAdapter == null || mAdapter.getCount() == 0) {
1412            // Nothing to present or scroll; nothing to touch.
1413            return false;
1414        }
1415
1416        if (mVelocityTracker == null) {
1417            mVelocityTracker = VelocityTracker.obtain();
1418        }
1419        mVelocityTracker.addMovement(ev);
1420
1421        final int action = ev.getAction();
1422        boolean needsInvalidate = false;
1423
1424        switch (action & MotionEventCompat.ACTION_MASK) {
1425            case MotionEvent.ACTION_DOWN: {
1426                /*
1427                 * If being flinged and user touches, stop the fling. isFinished
1428                 * will be false if being flinged.
1429                 */
1430                completeScroll();
1431
1432                // Remember where the motion event started
1433                mLastMotionX = mInitialMotionX = ev.getX();
1434                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
1435                break;
1436            }
1437            case MotionEvent.ACTION_MOVE:
1438                if (!mIsBeingDragged) {
1439                    final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
1440                    final float x = MotionEventCompat.getX(ev, pointerIndex);
1441                    final float xDiff = Math.abs(x - mLastMotionX);
1442                    final float y = MotionEventCompat.getY(ev, pointerIndex);
1443                    final float yDiff = Math.abs(y - mLastMotionY);
1444                    if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
1445                    if (xDiff > mTouchSlop && xDiff > yDiff) {
1446                        if (DEBUG) Log.v(TAG, "Starting drag!");
1447                        mIsBeingDragged = true;
1448                        mLastMotionX = x;
1449                        setScrollState(SCROLL_STATE_DRAGGING);
1450                        setScrollingCacheEnabled(true);
1451                    }
1452                }
1453                if (mIsBeingDragged) {
1454                    // Scroll to follow the motion event
1455                    final int activePointerIndex = MotionEventCompat.findPointerIndex(
1456                            ev, mActivePointerId);
1457                    final float x = MotionEventCompat.getX(ev, activePointerIndex);
1458                    final float deltaX = mLastMotionX - x;
1459                    mLastMotionX = x;
1460                    float oldScrollX = getScrollX();
1461                    float scrollX = oldScrollX + deltaX;
1462                    final int width = getWidth();
1463                    final int widthWithMargin = width + mPageMargin;
1464
1465                    final int lastItemIndex = mAdapter.getCount() - 1;
1466                    final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);
1467                    final float rightBound =
1468                            Math.min(mCurItem + 1, lastItemIndex) * widthWithMargin;
1469                    if (scrollX < leftBound) {
1470                        if (leftBound == 0) {
1471                            float over = -scrollX;
1472                            needsInvalidate = mLeftEdge.onPull(over / width);
1473                        }
1474                        scrollX = leftBound;
1475                    } else if (scrollX > rightBound) {
1476                        if (rightBound == lastItemIndex * widthWithMargin) {
1477                            float over = scrollX - rightBound;
1478                            needsInvalidate = mRightEdge.onPull(over / width);
1479                        }
1480                        scrollX = rightBound;
1481                    }
1482                    // Don't lose the rounded component
1483                    mLastMotionX += scrollX - (int) scrollX;
1484                    scrollTo((int) scrollX, getScrollY());
1485                    pageScrolled((int) scrollX);
1486                }
1487                break;
1488            case MotionEvent.ACTION_UP:
1489                if (mIsBeingDragged) {
1490                    final VelocityTracker velocityTracker = mVelocityTracker;
1491                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1492                    int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
1493                            velocityTracker, mActivePointerId);
1494                    mPopulatePending = true;
1495                    final int widthWithMargin = getWidth() + mPageMargin;
1496                    final int scrollX = getScrollX();
1497                    final int currentPage = scrollX / widthWithMargin;
1498                    int nextPage = initialVelocity > 0 ? currentPage : currentPage + 1;
1499                    setCurrentItemInternal(nextPage, true, true, initialVelocity);
1500
1501                    mActivePointerId = INVALID_POINTER;
1502                    endDrag();
1503                    needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
1504                }
1505                break;
1506            case MotionEvent.ACTION_CANCEL:
1507                if (mIsBeingDragged) {
1508                    setCurrentItemInternal(mCurItem, true, true);
1509                    mActivePointerId = INVALID_POINTER;
1510                    endDrag();
1511                    needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
1512                }
1513                break;
1514            case MotionEventCompat.ACTION_POINTER_DOWN: {
1515                final int index = MotionEventCompat.getActionIndex(ev);
1516                final float x = MotionEventCompat.getX(ev, index);
1517                mLastMotionX = x;
1518                mActivePointerId = MotionEventCompat.getPointerId(ev, index);
1519                break;
1520            }
1521            case MotionEventCompat.ACTION_POINTER_UP:
1522                onSecondaryPointerUp(ev);
1523                mLastMotionX = MotionEventCompat.getX(ev,
1524                        MotionEventCompat.findPointerIndex(ev, mActivePointerId));
1525                break;
1526        }
1527        if (needsInvalidate) {
1528            invalidate();
1529        }
1530        return true;
1531    }
1532
1533    @Override
1534    public void draw(Canvas canvas) {
1535        super.draw(canvas);
1536        boolean needsInvalidate = false;
1537
1538        final int overScrollMode = ViewCompat.getOverScrollMode(this);
1539        if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS ||
1540                (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS &&
1541                        mAdapter != null && mAdapter.getCount() > 1)) {
1542            if (!mLeftEdge.isFinished()) {
1543                final int restoreCount = canvas.save();
1544                final int height = getHeight() - getPaddingTop() - getPaddingBottom();
1545
1546                canvas.rotate(270);
1547                canvas.translate(-height + getPaddingTop(), 0);
1548                mLeftEdge.setSize(height, getWidth());
1549                needsInvalidate |= mLeftEdge.draw(canvas);
1550                canvas.restoreToCount(restoreCount);
1551            }
1552            if (!mRightEdge.isFinished()) {
1553                final int restoreCount = canvas.save();
1554                final int width = getWidth();
1555                final int height = getHeight() - getPaddingTop() - getPaddingBottom();
1556                final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;
1557
1558                canvas.rotate(90);
1559                canvas.translate(-getPaddingTop(),
1560                        -itemCount * (width + mPageMargin) + mPageMargin);
1561                mRightEdge.setSize(height, width);
1562                needsInvalidate |= mRightEdge.draw(canvas);
1563                canvas.restoreToCount(restoreCount);
1564            }
1565        } else {
1566            mLeftEdge.finish();
1567            mRightEdge.finish();
1568        }
1569
1570        if (needsInvalidate) {
1571            // Keep animating
1572            invalidate();
1573        }
1574    }
1575
1576    @Override
1577    protected void onDraw(Canvas canvas) {
1578        super.onDraw(canvas);
1579
1580        // Draw the margin drawable if needed.
1581        if (mPageMargin > 0 && mMarginDrawable != null) {
1582            final int scrollX = getScrollX();
1583            final int width = getWidth();
1584            final int offset = scrollX % (width + mPageMargin);
1585            if (offset != 0) {
1586                // Pages fit completely when settled; we only need to draw when in between
1587                final int left = scrollX - offset + width;
1588                mMarginDrawable.setBounds(left, mTopPageBounds, left + mPageMargin,
1589                        mBottomPageBounds);
1590                mMarginDrawable.draw(canvas);
1591            }
1592        }
1593    }
1594
1595    /**
1596     * Start a fake drag of the pager.
1597     *
1598     * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
1599     * with the touch scrolling of another view, while still letting the ViewPager
1600     * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
1601     * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
1602     * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
1603     *
1604     * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
1605     * is already in progress, this method will return false.
1606     *
1607     * @return true if the fake drag began successfully, false if it could not be started.
1608     *
1609     * @see #fakeDragBy(float)
1610     * @see #endFakeDrag()
1611     */
1612    public boolean beginFakeDrag() {
1613        if (mIsBeingDragged) {
1614            return false;
1615        }
1616        mFakeDragging = true;
1617        setScrollState(SCROLL_STATE_DRAGGING);
1618        mInitialMotionX = mLastMotionX = 0;
1619        if (mVelocityTracker == null) {
1620            mVelocityTracker = VelocityTracker.obtain();
1621        } else {
1622            mVelocityTracker.clear();
1623        }
1624        final long time = SystemClock.uptimeMillis();
1625        final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
1626        mVelocityTracker.addMovement(ev);
1627        ev.recycle();
1628        mFakeDragBeginTime = time;
1629        return true;
1630    }
1631
1632    /**
1633     * End a fake drag of the pager.
1634     *
1635     * @see #beginFakeDrag()
1636     * @see #fakeDragBy(float)
1637     */
1638    public void endFakeDrag() {
1639        if (!mFakeDragging) {
1640            throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
1641        }
1642
1643        final VelocityTracker velocityTracker = mVelocityTracker;
1644        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1645        int initialVelocity = (int)VelocityTrackerCompat.getYVelocity(
1646                velocityTracker, mActivePointerId);
1647        mPopulatePending = true;
1648        if ((Math.abs(initialVelocity) > mMinimumVelocity)
1649                || Math.abs(mInitialMotionX-mLastMotionX) >= (getWidth()/3)) {
1650            if (mLastMotionX > mInitialMotionX) {
1651                setCurrentItemInternal(mCurItem-1, true, true);
1652            } else {
1653                setCurrentItemInternal(mCurItem+1, true, true);
1654            }
1655        } else {
1656            setCurrentItemInternal(mCurItem, true, true);
1657        }
1658        endDrag();
1659
1660        mFakeDragging = false;
1661    }
1662
1663    /**
1664     * Fake drag by an offset in pixels. You must have called {@link #beginFakeDrag()} first.
1665     *
1666     * @param xOffset Offset in pixels to drag by.
1667     * @see #beginFakeDrag()
1668     * @see #endFakeDrag()
1669     */
1670    public void fakeDragBy(float xOffset) {
1671        if (!mFakeDragging) {
1672            throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
1673        }
1674
1675        mLastMotionX += xOffset;
1676        float scrollX = getScrollX() - xOffset;
1677        final int width = getWidth();
1678        final int widthWithMargin = width + mPageMargin;
1679
1680        final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);
1681        final float rightBound =
1682                Math.min(mCurItem + 1, mAdapter.getCount() - 1) * widthWithMargin;
1683        if (scrollX < leftBound) {
1684            scrollX = leftBound;
1685        } else if (scrollX > rightBound) {
1686            scrollX = rightBound;
1687        }
1688        // Don't lose the rounded component
1689        mLastMotionX += scrollX - (int) scrollX;
1690        scrollTo((int) scrollX, getScrollY());
1691        pageScrolled((int) scrollX);
1692
1693        // Synthesize an event for the VelocityTracker.
1694        final long time = SystemClock.uptimeMillis();
1695        final MotionEvent ev = MotionEvent.obtain(mFakeDragBeginTime, time, MotionEvent.ACTION_MOVE,
1696                mLastMotionX, 0, 0);
1697        mVelocityTracker.addMovement(ev);
1698        ev.recycle();
1699    }
1700
1701    /**
1702     * Returns true if a fake drag is in progress.
1703     *
1704     * @return true if currently in a fake drag, false otherwise.
1705     *
1706     * @see #beginFakeDrag()
1707     * @see #fakeDragBy(float)
1708     * @see #endFakeDrag()
1709     */
1710    public boolean isFakeDragging() {
1711        return mFakeDragging;
1712    }
1713
1714    private void onSecondaryPointerUp(MotionEvent ev) {
1715        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
1716        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
1717        if (pointerId == mActivePointerId) {
1718            // This was our active pointer going up. Choose a new
1719            // active pointer and adjust accordingly.
1720            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1721            mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
1722            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
1723            if (mVelocityTracker != null) {
1724                mVelocityTracker.clear();
1725            }
1726        }
1727    }
1728
1729    private void endDrag() {
1730        mIsBeingDragged = false;
1731        mIsUnableToDrag = false;
1732
1733        if (mVelocityTracker != null) {
1734            mVelocityTracker.recycle();
1735            mVelocityTracker = null;
1736        }
1737    }
1738
1739    private void setScrollingCacheEnabled(boolean enabled) {
1740        if (mScrollingCacheEnabled != enabled) {
1741            mScrollingCacheEnabled = enabled;
1742            if (USE_CACHE) {
1743                final int size = getChildCount();
1744                for (int i = 0; i < size; ++i) {
1745                    final View child = getChildAt(i);
1746                    if (child.getVisibility() != GONE) {
1747                        child.setDrawingCacheEnabled(enabled);
1748                    }
1749                }
1750            }
1751        }
1752    }
1753
1754    /**
1755     * Tests scrollability within child views of v given a delta of dx.
1756     *
1757     * @param v View to test for horizontal scrollability
1758     * @param checkV Whether the view v passed should itself be checked for scrollability (true),
1759     *               or just its children (false).
1760     * @param dx Delta scrolled in pixels
1761     * @param x X coordinate of the active touch point
1762     * @param y Y coordinate of the active touch point
1763     * @return true if child views of v can be scrolled by delta of dx.
1764     */
1765    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
1766        if (v instanceof ViewGroup) {
1767            final ViewGroup group = (ViewGroup) v;
1768            final int scrollX = v.getScrollX();
1769            final int scrollY = v.getScrollY();
1770            final int count = group.getChildCount();
1771            // Count backwards - let topmost views consume scroll distance first.
1772            for (int i = count - 1; i >= 0; i--) {
1773                // TODO: Add versioned support here for transformed views.
1774                // This will not work for transformed views in Honeycomb+
1775                final View child = group.getChildAt(i);
1776                if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
1777                        y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
1778                        canScroll(child, true, dx, x + scrollX - child.getLeft(),
1779                                y + scrollY - child.getTop())) {
1780                    return true;
1781                }
1782            }
1783        }
1784
1785        return checkV && ViewCompat.canScrollHorizontally(v, -dx);
1786    }
1787
1788    @Override
1789    public boolean dispatchKeyEvent(KeyEvent event) {
1790        // Let the focused view and/or our descendants get the key first
1791        return super.dispatchKeyEvent(event) || executeKeyEvent(event);
1792    }
1793
1794    /**
1795     * You can call this function yourself to have the scroll view perform
1796     * scrolling from a key event, just as if the event had been dispatched to
1797     * it by the view hierarchy.
1798     *
1799     * @param event The key event to execute.
1800     * @return Return true if the event was handled, else false.
1801     */
1802    public boolean executeKeyEvent(KeyEvent event) {
1803        boolean handled = false;
1804        if (event.getAction() == KeyEvent.ACTION_DOWN) {
1805            switch (event.getKeyCode()) {
1806                case KeyEvent.KEYCODE_DPAD_LEFT:
1807                    handled = arrowScroll(FOCUS_LEFT);
1808                    break;
1809                case KeyEvent.KEYCODE_DPAD_RIGHT:
1810                    handled = arrowScroll(FOCUS_RIGHT);
1811                    break;
1812                case KeyEvent.KEYCODE_TAB:
1813                    if (Build.VERSION.SDK_INT >= 11) {
1814                        // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
1815                        // before Android 3.0. Ignore the tab key on those devices.
1816                        if (KeyEventCompat.hasNoModifiers(event)) {
1817                            handled = arrowScroll(FOCUS_FORWARD);
1818                        } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
1819                            handled = arrowScroll(FOCUS_BACKWARD);
1820                        }
1821                    }
1822                    break;
1823            }
1824        }
1825        return handled;
1826    }
1827
1828    public boolean arrowScroll(int direction) {
1829        View currentFocused = findFocus();
1830        if (currentFocused == this) currentFocused = null;
1831
1832        boolean handled = false;
1833
1834        View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
1835                direction);
1836        if (nextFocused != null && nextFocused != currentFocused) {
1837            if (direction == View.FOCUS_LEFT) {
1838                // If there is nothing to the left, or this is causing us to
1839                // jump to the right, then what we really want to do is page left.
1840                if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {
1841                    handled = pageLeft();
1842                } else {
1843                    handled = nextFocused.requestFocus();
1844                }
1845            } else if (direction == View.FOCUS_RIGHT) {
1846                // If there is nothing to the right, or this is causing us to
1847                // jump to the left, then what we really want to do is page right.
1848                if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
1849                    handled = pageRight();
1850                } else {
1851                    handled = nextFocused.requestFocus();
1852                }
1853            }
1854        } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
1855            // Trying to move left and nothing there; try to page.
1856            handled = pageLeft();
1857        } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
1858            // Trying to move right and nothing there; try to page.
1859            handled = pageRight();
1860        }
1861        if (handled) {
1862            playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
1863        }
1864        return handled;
1865    }
1866
1867    boolean pageLeft() {
1868        if (mCurItem > 0) {
1869            setCurrentItem(mCurItem-1, true);
1870            return true;
1871        }
1872        return false;
1873    }
1874
1875    boolean pageRight() {
1876        if (mAdapter != null && mCurItem < (mAdapter.getCount()-1)) {
1877            setCurrentItem(mCurItem+1, true);
1878            return true;
1879        }
1880        return false;
1881    }
1882
1883    /**
1884     * We only want the current page that is being shown to be focusable.
1885     */
1886    @Override
1887    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
1888        final int focusableCount = views.size();
1889
1890        final int descendantFocusability = getDescendantFocusability();
1891
1892        if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
1893            for (int i = 0; i < getChildCount(); i++) {
1894                final View child = getChildAt(i);
1895                if (child.getVisibility() == VISIBLE) {
1896                    ItemInfo ii = infoForChild(child);
1897                    if (ii != null && ii.position == mCurItem) {
1898                        child.addFocusables(views, direction, focusableMode);
1899                    }
1900                }
1901            }
1902        }
1903
1904        // we add ourselves (if focusable) in all cases except for when we are
1905        // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable.  this is
1906        // to avoid the focus search finding layouts when a more precise search
1907        // among the focusable children would be more interesting.
1908        if (
1909            descendantFocusability != FOCUS_AFTER_DESCENDANTS ||
1910                // No focusable descendants
1911                (focusableCount == views.size())) {
1912            // Note that we can't call the superclass here, because it will
1913            // add all views in.  So we need to do the same thing View does.
1914            if (!isFocusable()) {
1915                return;
1916            }
1917            if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
1918                    isInTouchMode() && !isFocusableInTouchMode()) {
1919                return;
1920            }
1921            if (views != null) {
1922                views.add(this);
1923            }
1924        }
1925    }
1926
1927    /**
1928     * We only want the current page that is being shown to be touchable.
1929     */
1930    @Override
1931    public void addTouchables(ArrayList<View> views) {
1932        // Note that we don't call super.addTouchables(), which means that
1933        // we don't call View.addTouchables().  This is okay because a ViewPager
1934        // is itself not touchable.
1935        for (int i = 0; i < getChildCount(); i++) {
1936            final View child = getChildAt(i);
1937            if (child.getVisibility() == VISIBLE) {
1938                ItemInfo ii = infoForChild(child);
1939                if (ii != null && ii.position == mCurItem) {
1940                    child.addTouchables(views);
1941                }
1942            }
1943        }
1944    }
1945
1946    /**
1947     * We only want the current page that is being shown to be focusable.
1948     */
1949    @Override
1950    protected boolean onRequestFocusInDescendants(int direction,
1951            Rect previouslyFocusedRect) {
1952        int index;
1953        int increment;
1954        int end;
1955        int count = getChildCount();
1956        if ((direction & FOCUS_FORWARD) != 0) {
1957            index = 0;
1958            increment = 1;
1959            end = count;
1960        } else {
1961            index = count - 1;
1962            increment = -1;
1963            end = -1;
1964        }
1965        for (int i = index; i != end; i += increment) {
1966            View child = getChildAt(i);
1967            if (child.getVisibility() == VISIBLE) {
1968                ItemInfo ii = infoForChild(child);
1969                if (ii != null && ii.position == mCurItem) {
1970                    if (child.requestFocus(direction, previouslyFocusedRect)) {
1971                        return true;
1972                    }
1973                }
1974            }
1975        }
1976        return false;
1977    }
1978
1979    @Override
1980    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1981        // ViewPagers should only report accessibility info for the current page,
1982        // otherwise things get very confusing.
1983
1984        // TODO: Should this note something about the paging container?
1985
1986        final int childCount = getChildCount();
1987        for (int i = 0; i < childCount; i++) {
1988            final View child = getChildAt(i);
1989            if (child.getVisibility() == VISIBLE) {
1990                final ItemInfo ii = infoForChild(child);
1991                if (ii != null && ii.position == mCurItem &&
1992                        child.dispatchPopulateAccessibilityEvent(event)) {
1993                    return true;
1994                }
1995            }
1996        }
1997
1998        return false;
1999    }
2000
2001    @Override
2002    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
2003        return new LayoutParams();
2004    }
2005
2006    @Override
2007    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2008        return generateDefaultLayoutParams();
2009    }
2010
2011    @Override
2012    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2013        return p instanceof LayoutParams && super.checkLayoutParams(p);
2014    }
2015
2016    @Override
2017    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2018        return new LayoutParams(getContext(), attrs);
2019    }
2020
2021    private class PagerObserver extends DataSetObserver {
2022        @Override
2023        public void onChanged() {
2024            dataSetChanged();
2025        }
2026        @Override
2027        public void onInvalidated() {
2028            dataSetChanged();
2029        }
2030    }
2031
2032    public static class LayoutParams extends ViewGroup.LayoutParams {
2033        /**
2034         * true if this view is a decoration on the pager itself and not
2035         * a view supplied by the adapter.
2036         */
2037        public boolean isDecor;
2038
2039        public int gravity;
2040
2041        public LayoutParams() {
2042            super(FILL_PARENT, FILL_PARENT);
2043        }
2044
2045        public LayoutParams(Context context, AttributeSet attrs) {
2046            super(context, attrs);
2047
2048            final TypedArray a = context.obtainStyledAttributes(attrs, LAYOUT_ATTRS);
2049            gravity = a.getInteger(0, Gravity.NO_GRAVITY);
2050            a.recycle();
2051        }
2052    }
2053}
2054