NestedScrollView.java revision cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1c
11fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell/*
21fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * Copyright (C) 2015 The Android Open Source Project
31fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell *
41fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
51fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * you may not use this file except in compliance with the License.
61fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * You may obtain a copy of the License at
71fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell *
81fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
91fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell *
101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * Unless required by applicable law or agreed to in writing, software
111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * See the License for the specific language governing permissions and
141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * limitations under the License.
151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell */
161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellpackage android.support.v4.widget;
191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.content.Context;
211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.content.res.TypedArray;
221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.graphics.Canvas;
231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.graphics.Rect;
241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.os.Bundle;
251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.os.Parcel;
261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.os.Parcelable;
271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.AccessibilityDelegateCompat;
281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.InputDeviceCompat;
291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.MotionEventCompat;
301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.NestedScrollingChild;
311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.NestedScrollingChildHelper;
321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.NestedScrollingParent;
331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.NestedScrollingParentHelper;
341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.VelocityTrackerCompat;
351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.ViewCompat;
361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.accessibility.AccessibilityEventCompat;
371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.support.v4.view.accessibility.AccessibilityRecordCompat;
391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.util.AttributeSet;
401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.util.Log;
411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.util.TypedValue;
421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.FocusFinder;
431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.KeyEvent;
441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.MotionEvent;
451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.VelocityTracker;
461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.View;
471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.ViewConfiguration;
481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.ViewDebug;
491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.ViewGroup;
501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.ViewParent;
511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.accessibility.AccessibilityEvent;
521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.view.animation.AnimationUtils;
531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.widget.FrameLayout;
541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport android.widget.ScrollView;
551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellimport java.util.List;
571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell/**
591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * NestedScrollView is just like {@link android.widget.ScrollView}, but it supports acting
601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * as both a nested scrolling parent and child on both new and old versions of Android.
611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell * Nested scrolling is enabled by default.
621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell */
631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powellpublic class NestedScrollView extends FrameLayout implements NestedScrollingParent,
641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        NestedScrollingChild {
651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    static final int ANIMATED_SCROLL_GAP = 250;
661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    static final float MAX_SCROLL_FACTOR = 0.5f;
681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private static final String TAG = "NestedScrollView";
701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
71cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    /**
72cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * Interface definition for a callback to be invoked when the scroll
73cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * X or Y positions of a view change.
74cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     *
75cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * <p>This version of the interface works on all versions of Android, back to API v4.</p>
76cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     *
77cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * @see #setOnScrollChangeListener(OnScrollChangeListener)
78cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     */
79cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    public interface OnScrollChangeListener {
80cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes        /**
81cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes         * Called when the scroll position of a view changes.
82cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes         *
83cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes         * @param v The view whose scroll position has changed.
84cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes         * @param scrollX Current horizontal scroll origin.
85cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes         * @param scrollY Current vertical scroll origin.
86cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes         * @param oldScrollX Previous horizontal scroll origin.
87cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes         * @param oldScrollY Previous vertical scroll origin.
88cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes         */
89cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes        void onScrollChange(NestedScrollView v, int scrollX, int scrollY,
90cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes                int oldScrollX, int oldScrollY);
91cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    }
92cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes
931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private long mLastScroll;
941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private final Rect mTempRect = new Rect();
961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private ScrollerCompat mScroller;
971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private EdgeEffectCompat mEdgeGlowTop;
981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private EdgeEffectCompat mEdgeGlowBottom;
991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Position of the last motion event.
1021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private int mLastMotionY;
1041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * True when the layout has changed but the traversal has not come through yet.
1071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Ideally the view hierarchy would keep track of this for us.
1081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean mIsLayoutDirty = true;
1101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean mIsLaidOut = false;
1111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * The child to give focus to in the event that a child has requested focus while the
1141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * layout is dirty. This prevents the scroll from being wrong if the child has not been
1151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * laid out before requesting focus.
1161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private View mChildToScrollTo = null;
1181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * True if the user is currently dragging this ScrollView around. This is
1211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * not the same as 'is being flinged', which can be checked by
1221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * mScroller.isFinished() (flinging begins when the user lifts his finger).
1231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean mIsBeingDragged = false;
1251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Determines speed during touch scrolling
1281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private VelocityTracker mVelocityTracker;
1301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * When set to true, the scroll view measure its child to make it fill the currently
1331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * visible area.
1341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean mFillViewport;
1361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Whether arrow scrolling is animated.
1391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean mSmoothScrollingEnabled = true;
1411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private int mTouchSlop;
1431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private int mMinimumVelocity;
1441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private int mMaximumVelocity;
1451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * ID of the active pointer. This is used to retain consistency during
1481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * drags/flings if multiple pointers are used.
1491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private int mActivePointerId = INVALID_POINTER;
1511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Used during scrolling to retrieve the new offset within the window.
1541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private final int[] mScrollOffset = new int[2];
1561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private final int[] mScrollConsumed = new int[2];
1571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private int mNestedYOffset;
1581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
1601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Sentinel value for no current active pointer.
1611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Used by {@link #mActivePointerId}.
1621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
1631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private static final int INVALID_POINTER = -1;
1641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private SavedState mSavedState;
1661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private static final AccessibilityDelegate ACCESSIBILITY_DELEGATE = new AccessibilityDelegate();
1681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private static final int[] SCROLLVIEW_STYLEABLE = new int[] {
1701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            android.R.attr.fillViewport
1711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    };
1721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private final NestedScrollingParentHelper mParentHelper;
1741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private final NestedScrollingChildHelper mChildHelper;
1751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private float mVerticalScrollFactor;
1771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
178cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    private OnScrollChangeListener mOnScrollChangeListener;
179cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes
1801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public NestedScrollView(Context context) {
1811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        this(context, null);
1821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
1831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public NestedScrollView(Context context, AttributeSet attrs) {
1851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        this(context, attrs, 0);
1861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
1871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
1891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super(context, attrs, defStyleAttr);
1901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        initScrollView();
1911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final TypedArray a = context.obtainStyledAttributes(
1931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                attrs, SCROLLVIEW_STYLEABLE, defStyleAttr, 0);
1941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        setFillViewport(a.getBoolean(0, false));
1961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        a.recycle();
1981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
1991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mParentHelper = new NestedScrollingParentHelper(this);
2001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mChildHelper = new NestedScrollingChildHelper(this);
2011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // ...because why else would you be using this widget?
2031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        setNestedScrollingEnabled(true);
2041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        ViewCompat.setAccessibilityDelegate(this, ACCESSIBILITY_DELEGATE);
2061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    // NestedScrollingChild
2091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void setNestedScrollingEnabled(boolean enabled) {
2121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mChildHelper.setNestedScrollingEnabled(enabled);
2131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean isNestedScrollingEnabled() {
2171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mChildHelper.isNestedScrollingEnabled();
2181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean startNestedScroll(int axes) {
2221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mChildHelper.startNestedScroll(axes);
2231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void stopNestedScroll() {
2271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mChildHelper.stopNestedScroll();
2281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean hasNestedScrollingParent() {
2321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mChildHelper.hasNestedScrollingParent();
2331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,
2371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int dyUnconsumed, int[] offsetInWindow) {
2381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed,
2391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                offsetInWindow);
2401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
2441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
2451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
2491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
2501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
2541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);
2551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    // NestedScrollingParent
2581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
2611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
2621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
2661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mParentHelper.onNestedScrollAccepted(child, target, nestedScrollAxes);
2671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
2681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void onStopNestedScroll(View target) {
2721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        stopNestedScroll();
2731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed,
2771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int dyUnconsumed) {
2781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int oldScrollY = getScrollY();
2791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        scrollBy(0, dyUnconsumed);
2801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int myConsumed = getScrollY() - oldScrollY;
2811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int myUnconsumed = dyUnconsumed - myConsumed;
2821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        dispatchNestedScroll(0, myConsumed, 0, myUnconsumed, null);
2831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
2871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // Do nothing
2881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
2911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
2921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (!consumed) {
2931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            flingWithNestedDispatch((int) velocityY);
2941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return true;
2951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
2961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return false;
2971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
2981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
2991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
3001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
3011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // Do nothing
3021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return false;
3031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
3061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public int getNestedScrollAxes() {
3071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mParentHelper.getNestedScrollAxes();
3081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    // ScrollView import
3111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean shouldDelayChildPressedState() {
3131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return true;
3141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
3171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected float getTopFadingEdgeStrength() {
3181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() == 0) {
3191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return 0.0f;
3201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
3211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int length = getVerticalFadingEdgeLength();
3231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int scrollY = getScrollY();
3241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (scrollY < length) {
3251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return scrollY / (float) length;
3261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
3271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return 1.0f;
3291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
3321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected float getBottomFadingEdgeStrength() {
3331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() == 0) {
3341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return 0.0f;
3351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
3361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int length = getVerticalFadingEdgeLength();
3381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int bottomEdge = getHeight() - getPaddingBottom();
3391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int span = getChildAt(0).getBottom() - getScrollY() - bottomEdge;
3401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (span < length) {
3411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return span / (float) length;
3421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
3431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return 1.0f;
3451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
3481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return The maximum amount this scroll view will scroll in response to
3491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *   an arrow event.
3501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
3511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public int getMaxScrollAmount() {
3521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return (int) (MAX_SCROLL_FACTOR * getHeight());
3531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void initScrollView() {
3561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mScroller = new ScrollerCompat(getContext(), null);
3571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        setFocusable(true);
3581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
3591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        setWillNotDraw(false);
3601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
3611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mTouchSlop = configuration.getScaledTouchSlop();
3621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
3631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
3641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
3671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void addView(View child) {
3681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
3691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            throw new IllegalStateException("ScrollView can host only one direct child");
3701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
3711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.addView(child);
3731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
3761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void addView(View child, int index) {
3771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
3781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            throw new IllegalStateException("ScrollView can host only one direct child");
3791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
3801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.addView(child, index);
3821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
3851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void addView(View child, ViewGroup.LayoutParams params) {
3861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
3871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            throw new IllegalStateException("ScrollView can host only one direct child");
3881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
3891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.addView(child, params);
3911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
3921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
3941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void addView(View child, int index, ViewGroup.LayoutParams params) {
3951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
3961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            throw new IllegalStateException("ScrollView can host only one direct child");
3971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
3981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
3991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.addView(child, index, params);
4001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
4011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
403cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * Register a callback to be invoked when the scroll X or Y positions of
404cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * this view change.
405cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * <p>This version of the method works on all versions of Android, back to API v4.</p>
406cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     *
407cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * @param l The listener to notify when the scroll X or Y position changes.
408cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * @see android.view.View#getScrollX()
409cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     * @see android.view.View#getScrollY()
410cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes     */
411cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    public void setOnScrollChangeListener(OnScrollChangeListener l) {
412cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes        mOnScrollChangeListener = l;
413cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    }
414cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes
415cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    /**
4161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return Returns true this ScrollView can be scrolled
4171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
4181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean canScroll() {
4191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        View child = getChildAt(0);
4201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (child != null) {
4211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int childHeight = child.getHeight();
4221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return getHeight() < childHeight + getPaddingTop() + getPaddingBottom();
4231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
4241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return false;
4251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
4261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
4281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Indicates whether this ScrollView's content is stretched to fill the viewport.
4291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
4301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return True if the content fills the viewport, false otherwise.
4311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
4321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @attr ref android.R.styleable#ScrollView_fillViewport
4331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
4341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean isFillViewport() {
4351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mFillViewport;
4361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
4371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
4391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Indicates this ScrollView whether it should stretch its content height to fill
4401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * the viewport or not.
4411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
4421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param fillViewport True to stretch the content's height to the viewport's
4431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *        boundaries, false otherwise.
4441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
4451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @attr ref android.R.styleable#ScrollView_fillViewport
4461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
4471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void setFillViewport(boolean fillViewport) {
4481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (fillViewport != mFillViewport) {
4491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mFillViewport = fillViewport;
4501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            requestLayout();
4511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
4521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
4531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
4551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return Whether arrow scrolling will animate its transition.
4561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
4571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean isSmoothScrollingEnabled() {
4581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mSmoothScrollingEnabled;
4591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
4601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
4621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Set whether arrow scrolling will animate its transition.
4631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param smoothScrollingEnabled whether arrow scrolling will animate its transition
4641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
4651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void setSmoothScrollingEnabled(boolean smoothScrollingEnabled) {
4661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mSmoothScrollingEnabled = smoothScrollingEnabled;
4671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
4681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
470cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
471cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes        super.onScrollChanged(l, t, oldl, oldt);
472cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes
473cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes        if (mOnScrollChangeListener != null) {
474cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes            mOnScrollChangeListener.onScrollChange(this, l, t, oldl, oldt);
475cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes        }
476cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    }
477cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes
478cc82b3ee0c53b2f2da75b7fda5fe4b8f5af5dc1cChris Banes    @Override
4791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
4801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
4811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (!mFillViewport) {
4831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return;
4841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
4851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
4871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (heightMode == MeasureSpec.UNSPECIFIED) {
4881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return;
4891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
4901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
4921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final View child = getChildAt(0);
4931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int height = getMeasuredHeight();
4941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (child.getMeasuredHeight() < height) {
4951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
4961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
4971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
4981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        getPaddingLeft() + getPaddingRight(), lp.width);
4991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                height -= getPaddingTop();
5001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                height -= getPaddingBottom();
5011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                int childHeightMeasureSpec =
5021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
5031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
5051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
5061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
5071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
5081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
5101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean dispatchKeyEvent(KeyEvent event) {
5111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // Let the focused view and/or our descendants get the key first
5121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return super.dispatchKeyEvent(event) || executeKeyEvent(event);
5131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
5141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
5161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * You can call this function yourself to have the scroll view perform
5171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * scrolling from a key event, just as if the event had been dispatched to
5181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * it by the view hierarchy.
5191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
5201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param event The key event to execute.
5211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return Return true if the event was handled, else false.
5221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
5231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean executeKeyEvent(KeyEvent event) {
5241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mTempRect.setEmpty();
5251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (!canScroll()) {
5271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
5281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                View currentFocused = findFocus();
5291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (currentFocused == this) currentFocused = null;
5301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                View nextFocused = FocusFinder.getInstance().findNextFocus(this,
5311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        currentFocused, View.FOCUS_DOWN);
5321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                return nextFocused != null
5331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        && nextFocused != this
5341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        && nextFocused.requestFocus(View.FOCUS_DOWN);
5351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
5361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return false;
5371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
5381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        boolean handled = false;
5401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (event.getAction() == KeyEvent.ACTION_DOWN) {
5411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            switch (event.getKeyCode()) {
5421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                case KeyEvent.KEYCODE_DPAD_UP:
5431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (!event.isAltPressed()) {
5441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        handled = arrowScroll(View.FOCUS_UP);
5451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    } else {
5461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        handled = fullScroll(View.FOCUS_UP);
5471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
5481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    break;
5491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                case KeyEvent.KEYCODE_DPAD_DOWN:
5501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (!event.isAltPressed()) {
5511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        handled = arrowScroll(View.FOCUS_DOWN);
5521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    } else {
5531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        handled = fullScroll(View.FOCUS_DOWN);
5541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
5551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    break;
5561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                case KeyEvent.KEYCODE_SPACE:
5571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
5581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    break;
5591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
5601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
5611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return handled;
5631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
5641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean inChild(int x, int y) {
5661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
5671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int scrollY = getScrollY();
5681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final View child = getChildAt(0);
5691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return !(y < child.getTop() - scrollY
5701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    || y >= child.getBottom() - scrollY
5711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    || x < child.getLeft()
5721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    || x >= child.getRight());
5731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
5741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return false;
5751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
5761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void initOrResetVelocityTracker() {
5781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mVelocityTracker == null) {
5791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mVelocityTracker = VelocityTracker.obtain();
5801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else {
5811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mVelocityTracker.clear();
5821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
5831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
5841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void initVelocityTrackerIfNotExists() {
5861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mVelocityTracker == null) {
5871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mVelocityTracker = VelocityTracker.obtain();
5881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
5891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
5901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void recycleVelocityTracker() {
5921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mVelocityTracker != null) {
5931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mVelocityTracker.recycle();
5941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mVelocityTracker = null;
5951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
5961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
5971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
5981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
5991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
6001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (disallowIntercept) {
6011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            recycleVelocityTracker();
6021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
6031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.requestDisallowInterceptTouchEvent(disallowIntercept);
6041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
6051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
6081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean onInterceptTouchEvent(MotionEvent ev) {
6091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        /*
6101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         * This method JUST determines whether we want to intercept the motion.
6111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         * If we return true, onMotionEvent will be called and we do the actual
6121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         * scrolling there.
6131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         */
6141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        /*
6161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        * Shortcut the most recurring case: the user is in the dragging
6171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        * state and he is moving his finger.  We want to intercept this
6181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        * motion.
6191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        */
6201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int action = ev.getAction();
6211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
6221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return true;
6231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
6241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        switch (action & MotionEventCompat.ACTION_MASK) {
6261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEvent.ACTION_MOVE: {
6271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                /*
6281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
6291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 * whether the user has moved far enough from his original down touch.
6301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 */
6311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                /*
6331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                * Locally do absolute value. mLastMotionY is set to the y value
6341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                * of the down event.
6351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                */
6361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int activePointerId = mActivePointerId;
6371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (activePointerId == INVALID_POINTER) {
6381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    // If we don't have a valid id, the touch down wasn't on content.
6391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    break;
6401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
6411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
6431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (pointerIndex == -1) {
6441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    Log.e(TAG, "Invalid pointerId=" + activePointerId
6451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            + " in onInterceptTouchEvent");
6461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    break;
6471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
6481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int y = (int) MotionEventCompat.getY(ev, pointerIndex);
6501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int yDiff = Math.abs(y - mLastMotionY);
6511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (yDiff > mTouchSlop
6521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        && (getNestedScrollAxes() & ViewCompat.SCROLL_AXIS_VERTICAL) == 0) {
6531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mIsBeingDragged = true;
6541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mLastMotionY = y;
6551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    initVelocityTrackerIfNotExists();
6561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mVelocityTracker.addMovement(ev);
6571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mNestedYOffset = 0;
6581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final ViewParent parent = getParent();
6591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (parent != null) {
6601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        parent.requestDisallowInterceptTouchEvent(true);
6611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
6621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
6631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
6641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
6651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEvent.ACTION_DOWN: {
6671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int y = (int) ev.getY();
6681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (!inChild((int) ev.getX(), (int) y)) {
6691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mIsBeingDragged = false;
6701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    recycleVelocityTracker();
6711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    break;
6721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
6731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                /*
6751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 * Remember location of down touch.
6761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 * ACTION_DOWN always refers to pointer index 0.
6771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 */
6781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mLastMotionY = y;
6791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
6801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                initOrResetVelocityTracker();
6821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mVelocityTracker.addMovement(ev);
6831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                /*
6841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                * If being flinged and user touches the screen, initiate drag;
6851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                * otherwise don't.  mScroller.isFinished should be false when
6861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                * being flinged.
6871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                */
6881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mIsBeingDragged = !mScroller.isFinished();
6891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
6901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
6911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
6921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
6931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEvent.ACTION_CANCEL:
6941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEvent.ACTION_UP:
6951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                /* Release the drag */
6961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mIsBeingDragged = false;
6971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mActivePointerId = INVALID_POINTER;
6981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                recycleVelocityTracker();
69900db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
70000db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                    ViewCompat.postInvalidateOnAnimation(this);
70100db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                }
7021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                stopNestedScroll();
7031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
7041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEventCompat.ACTION_POINTER_UP:
7051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                onSecondaryPointerUp(ev);
7061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
7071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
7081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        /*
7101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        * The only time we want to intercept motion events is if we are in the
7111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        * drag mode.
7121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        */
7131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mIsBeingDragged;
7141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
7151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
7171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean onTouchEvent(MotionEvent ev) {
7181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        initVelocityTrackerIfNotExists();
7191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        MotionEvent vtev = MotionEvent.obtain(ev);
7211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int actionMasked = MotionEventCompat.getActionMasked(ev);
7231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (actionMasked == MotionEvent.ACTION_DOWN) {
7251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mNestedYOffset = 0;
7261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
7271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        vtev.offsetLocation(0, mNestedYOffset);
7281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        switch (actionMasked) {
7301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEvent.ACTION_DOWN: {
7311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (getChildCount() == 0) {
7321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    return false;
7331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
7341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if ((mIsBeingDragged = !mScroller.isFinished())) {
7351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final ViewParent parent = getParent();
7361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (parent != null) {
7371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        parent.requestDisallowInterceptTouchEvent(true);
7381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
7391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
7401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                /*
7421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 * If being flinged and user touches, stop the fling. isFinished
7431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 * will be false if being flinged.
7441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 */
7451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (!mScroller.isFinished()) {
7461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mScroller.abortAnimation();
7471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
7481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                // Remember where the motion event started
7501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mLastMotionY = (int) ev.getY();
7511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
7521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
7531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
7541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
7551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEvent.ACTION_MOVE:
7561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int activePointerIndex = MotionEventCompat.findPointerIndex(ev,
7571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        mActivePointerId);
7581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (activePointerIndex == -1) {
7591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
7601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    break;
7611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
7621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int y = (int) MotionEventCompat.getY(ev, activePointerIndex);
7641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                int deltaY = mLastMotionY - y;
7651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
7661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    deltaY -= mScrollConsumed[1];
7671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    vtev.offsetLocation(0, mScrollOffset[1]);
7681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mNestedYOffset += mScrollOffset[1];
7691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
7701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
7711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final ViewParent parent = getParent();
7721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (parent != null) {
7731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        parent.requestDisallowInterceptTouchEvent(true);
7741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
7751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mIsBeingDragged = true;
7761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (deltaY > 0) {
7771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        deltaY -= mTouchSlop;
7781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    } else {
7791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        deltaY += mTouchSlop;
7801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
7811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
7821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (mIsBeingDragged) {
7831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    // Scroll to follow the motion event
7841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mLastMotionY = y - mScrollOffset[1];
7851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int oldY = getScrollY();
7871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int range = getScrollRange();
7881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int overscrollMode = ViewCompat.getOverScrollMode(this);
7891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    boolean canOverscroll = overscrollMode == ViewCompat.OVER_SCROLL_ALWAYS ||
7901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            (overscrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS &&
7911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                    range > 0);
7921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
7931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    // Calling overScrollByCompat will call onOverScrolled, which
7941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    // calls onScrollChanged if applicable.
7951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (overScrollByCompat(0, deltaY, 0, getScrollY(), 0, range, 0,
7961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            0, true) && !hasNestedScrollingParent()) {
7971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        // Break our velocity if we hit a scroll barrier.
7981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        mVelocityTracker.clear();
7991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
8001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
8011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int scrolledDeltaY = getScrollY() - oldY;
8021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int unconsumedY = deltaY - scrolledDeltaY;
8031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
8041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        mLastMotionY -= mScrollOffset[1];
8051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        vtev.offsetLocation(0, mScrollOffset[1]);
8061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        mNestedYOffset += mScrollOffset[1];
8071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    } else if (canOverscroll) {
8081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        ensureGlows();
8091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        final int pulledToY = oldY + deltaY;
8101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        if (pulledToY < 0) {
8111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            mEdgeGlowTop.onPull((float) deltaY / getHeight(),
8121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                    MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
8131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            if (!mEdgeGlowBottom.isFinished()) {
8141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                mEdgeGlowBottom.onRelease();
8151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            }
8161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        } else if (pulledToY > range) {
8171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            mEdgeGlowBottom.onPull((float) deltaY / getHeight(),
8181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                    1.f - MotionEventCompat.getX(ev, activePointerIndex)
8191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                            / getWidth());
8201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            if (!mEdgeGlowTop.isFinished()) {
8211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                mEdgeGlowTop.onRelease();
8221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            }
8231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        }
8241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        if (mEdgeGlowTop != null
8251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
8261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            ViewCompat.postInvalidateOnAnimation(this);
8271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        }
8281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
8291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
8301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
8311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEvent.ACTION_UP:
8321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (mIsBeingDragged) {
8331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final VelocityTracker velocityTracker = mVelocityTracker;
8341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
8351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker,
8361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            mActivePointerId);
8371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
8381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
8391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        flingWithNestedDispatch(-initialVelocity);
84000db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                    } else if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0,
84100db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                            getScrollRange())) {
84200db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                        ViewCompat.postInvalidateOnAnimation(this);
8431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
8441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
8451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mActivePointerId = INVALID_POINTER;
8461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    endDrag();
8471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
8481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
8491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEvent.ACTION_CANCEL:
8501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (mIsBeingDragged && getChildCount() > 0) {
85100db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                    if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0,
85200db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                            getScrollRange())) {
85300db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                        ViewCompat.postInvalidateOnAnimation(this);
85400db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes                    }
8551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mActivePointerId = INVALID_POINTER;
8561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    endDrag();
8571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
8581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
8591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEventCompat.ACTION_POINTER_DOWN: {
8601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int index = MotionEventCompat.getActionIndex(ev);
8611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mLastMotionY = (int) MotionEventCompat.getY(ev, index);
8621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mActivePointerId = MotionEventCompat.getPointerId(ev, index);
8631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
8641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
8651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            case MotionEventCompat.ACTION_POINTER_UP:
8661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                onSecondaryPointerUp(ev);
8671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mLastMotionY = (int) MotionEventCompat.getY(ev,
8681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        MotionEventCompat.findPointerIndex(ev, mActivePointerId));
8691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                break;
8701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
8711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
8721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mVelocityTracker != null) {
8731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mVelocityTracker.addMovement(vtev);
8741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
8751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        vtev.recycle();
8761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return true;
8771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
8781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
8791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void onSecondaryPointerUp(MotionEvent ev) {
8801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int pointerIndex = (ev.getAction() & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >>
8811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                MotionEventCompat.ACTION_POINTER_INDEX_SHIFT;
8821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
8831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (pointerId == mActivePointerId) {
8841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // This was our active pointer going up. Choose a new
8851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // active pointer and adjust accordingly.
8861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // TODO: Make this decision more intelligent.
8871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
8881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mLastMotionY = (int) MotionEventCompat.getY(ev, newPointerIndex);
8891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
8901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (mVelocityTracker != null) {
8911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mVelocityTracker.clear();
8921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
8931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
8941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
8951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
8961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean onGenericMotionEvent(MotionEvent event) {
8971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if ((MotionEventCompat.getSource(event) & InputDeviceCompat.SOURCE_CLASS_POINTER) != 0) {
8981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            switch (event.getAction()) {
8991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                case MotionEventCompat.ACTION_SCROLL: {
9001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (!mIsBeingDragged) {
9011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        final float vscroll = MotionEventCompat.getAxisValue(event,
9021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                MotionEventCompat.AXIS_VSCROLL);
9031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        if (vscroll != 0) {
9041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            final int delta = (int) (vscroll * getVerticalScrollFactorCompat());
9051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            final int range = getScrollRange();
9061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            int oldScrollY = getScrollY();
9071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            int newScrollY = oldScrollY - delta;
9081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            if (newScrollY < 0) {
9091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                newScrollY = 0;
9101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            } else if (newScrollY > range) {
9111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                newScrollY = range;
9121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            }
9131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            if (newScrollY != oldScrollY) {
9141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                super.scrollTo(getScrollX(), newScrollY);
9151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                return true;
9161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            }
9171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        }
9181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
9191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
9201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
9211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
9221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return false;
9231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
9241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
9251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private float getVerticalScrollFactorCompat() {
9261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mVerticalScrollFactor == 0) {
9271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            TypedValue outValue = new TypedValue();
9281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final Context context = getContext();
9291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (!context.getTheme().resolveAttribute(
9301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    android.R.attr.listPreferredItemHeight, outValue, true)) {
9311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                throw new IllegalStateException(
9321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        "Expected theme to define listPreferredItemHeight.");
9331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
9341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mVerticalScrollFactor = outValue.getDimension(
9351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    context.getResources().getDisplayMetrics());
9361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
9371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return mVerticalScrollFactor;
9381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
9391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
9401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected void onOverScrolled(int scrollX, int scrollY,
9411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            boolean clampedX, boolean clampedY) {
9421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.scrollTo(scrollX, scrollY);
9431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
9441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
9451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    boolean overScrollByCompat(int deltaX, int deltaY,
9461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int scrollX, int scrollY,
9471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int scrollRangeX, int scrollRangeY,
9481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int maxOverScrollX, int maxOverScrollY,
9491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            boolean isTouchEvent) {
9501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int overScrollMode = ViewCompat.getOverScrollMode(this);
9511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final boolean canScrollHorizontal =
9521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                computeHorizontalScrollRange() > computeHorizontalScrollExtent();
9531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final boolean canScrollVertical =
9541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                computeVerticalScrollRange() > computeVerticalScrollExtent();
9551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final boolean overScrollHorizontal = overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS ||
9561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
9571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final boolean overScrollVertical = overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS ||
9581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
9591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
9601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int newScrollX = scrollX + deltaX;
9611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (!overScrollHorizontal) {
9621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            maxOverScrollX = 0;
9631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
9641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
9651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int newScrollY = scrollY + deltaY;
9661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (!overScrollVertical) {
9671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            maxOverScrollY = 0;
9681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
9691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
9701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // Clamp values if at the limits and record
9711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int left = -maxOverScrollX;
9721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int right = maxOverScrollX + scrollRangeX;
9731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int top = -maxOverScrollY;
9741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int bottom = maxOverScrollY + scrollRangeY;
9751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
9761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        boolean clampedX = false;
9771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (newScrollX > right) {
9781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            newScrollX = right;
9791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            clampedX = true;
9801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else if (newScrollX < left) {
9811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            newScrollX = left;
9821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            clampedX = true;
9831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
9841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
9851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        boolean clampedY = false;
9861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (newScrollY > bottom) {
9871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            newScrollY = bottom;
9881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            clampedY = true;
9891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else if (newScrollY < top) {
9901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            newScrollY = top;
9911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            clampedY = true;
9921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
9931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
99400db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes        if (clampedY) {
99500db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes            mScroller.springBack(newScrollX, newScrollY, 0, 0, 0, getScrollRange());
99600db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes        }
99700db92e217c3bc08acd09143cac8e3d3b0d0e813Chris Banes
9981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
9991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return clampedX || clampedY;
10011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
10021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private int getScrollRange() {
10041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int scrollRange = 0;
10051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
10061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            View child = getChildAt(0);
10071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollRange = Math.max(0,
10081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    child.getHeight() - (getHeight() - getPaddingBottom() - getPaddingTop()));
10091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
10101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return scrollRange;
10111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
10121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
10141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * <p>
10151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Finds the next focusable component that fits in the specified bounds.
10161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * </p>
10171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
10181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param topFocus look for a candidate is the one at the top of the bounds
10191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                 if topFocus is true, or at the bottom of the bounds if topFocus is
10201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                 false
10211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param top      the top offset of the bounds in which a focusable must be
10221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                 found
10231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param bottom   the bottom offset of the bounds in which a focusable must
10241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                 be found
10251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return the next focusable component in the bounds or null if none can
10261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *         be found
10271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
10281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private View findFocusableViewInBounds(boolean topFocus, int top, int bottom) {
10291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        List<View> focusables = getFocusables(View.FOCUS_FORWARD);
10311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        View focusCandidate = null;
10321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        /*
10341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         * A fully contained focusable is one where its top is below the bound's
10351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         * top, and its bottom is above the bound's bottom. A partially
10361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         * contained focusable is one where some part of it is within the
10371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         * bounds, but it also has some part that is not within bounds.  A fully contained
10381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         * focusable is preferred to a partially contained focusable.
10391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell         */
10401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        boolean foundFullyContainedFocusable = false;
10411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int count = focusables.size();
10431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        for (int i = 0; i < count; i++) {
10441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            View view = focusables.get(i);
10451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int viewTop = view.getTop();
10461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int viewBottom = view.getBottom();
10471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (top < viewBottom && viewTop < bottom) {
10491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                /*
10501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 * the focusable is in the target area, it is a candidate for
10511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 * focusing
10521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                 */
10531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final boolean viewIsFullyContained = (top < viewTop) &&
10551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        (viewBottom < bottom);
10561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (focusCandidate == null) {
10581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    /* No candidate, take this one */
10591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    focusCandidate = view;
10601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    foundFullyContainedFocusable = viewIsFullyContained;
10611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                } else {
10621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final boolean viewIsCloserToBoundary =
10631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            (topFocus && viewTop < focusCandidate.getTop()) ||
10641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                    (!topFocus && viewBottom > focusCandidate
10651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                                            .getBottom());
10661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (foundFullyContainedFocusable) {
10681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        if (viewIsFullyContained && viewIsCloserToBoundary) {
10691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            /*
10701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                             * We're dealing with only fully contained views, so
10711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                             * it has to be closer to the boundary to beat our
10721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                             * candidate
10731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                             */
10741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            focusCandidate = view;
10751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        }
10761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    } else {
10771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        if (viewIsFullyContained) {
10781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            /* Any fully contained view beats a partially contained view */
10791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            focusCandidate = view;
10801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            foundFullyContainedFocusable = true;
10811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        } else if (viewIsCloserToBoundary) {
10821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            /*
10831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                             * Partially contained view beats another partially
10841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                             * contained view if it's closer
10851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                             */
10861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            focusCandidate = view;
10871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        }
10881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
10891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
10901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
10911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
10921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return focusCandidate;
10941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
10951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
10961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
10971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * <p>Handles scrolling in response to a "page up/down" shortcut press. This
10981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * method will scroll the view by one page up or down and give the focus
10991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * to the topmost/bottommost component in the new visible area. If no
11001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * component is a good candidate for focus, this scrollview reclaims the
11011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * focus.</p>
11021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
11031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
11041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                  to go one page up or
11051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                  {@link android.view.View#FOCUS_DOWN} to go one page down
11061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return true if the key event is consumed by this method, false otherwise
11071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
11081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean pageScroll(int direction) {
11091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        boolean down = direction == View.FOCUS_DOWN;
11101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int height = getHeight();
11111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (down) {
11131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mTempRect.top = getScrollY() + height;
11141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int count = getChildCount();
11151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (count > 0) {
11161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                View view = getChildAt(count - 1);
11171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (mTempRect.top + height > view.getBottom()) {
11181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    mTempRect.top = view.getBottom() - height;
11191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
11201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
11211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else {
11221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mTempRect.top = getScrollY() - height;
11231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (mTempRect.top < 0) {
11241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mTempRect.top = 0;
11251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
11261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
11271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mTempRect.bottom = mTempRect.top + height;
11281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom);
11301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
11311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
11331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * <p>Handles scrolling in response to a "home/end" shortcut press. This
11341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * method will scroll the view to the top or bottom and give the focus
11351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * to the topmost/bottommost component in the new visible area. If no
11361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * component is a good candidate for focus, this scrollview reclaims the
11371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * focus.</p>
11381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
11391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
11401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                  to go the top of the view or
11411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                  {@link android.view.View#FOCUS_DOWN} to go the bottom
11421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return true if the key event is consumed by this method, false otherwise
11431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
11441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean fullScroll(int direction) {
11451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        boolean down = direction == View.FOCUS_DOWN;
11461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int height = getHeight();
11471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mTempRect.top = 0;
11491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mTempRect.bottom = height;
11501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (down) {
11521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int count = getChildCount();
11531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (count > 0) {
11541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                View view = getChildAt(count - 1);
11551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mTempRect.bottom = view.getBottom() + getPaddingBottom();
11561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mTempRect.top = mTempRect.bottom - height;
11571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
11581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
11591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom);
11611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
11621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
11641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * <p>Scrolls the view to make the area defined by <code>top</code> and
11651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * <code>bottom</code> visible. This method attempts to give the focus
11661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * to a component visible in this area. If no component can be focused in
11671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * the new visible area, the focus is reclaimed by this ScrollView.</p>
11681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
11691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
11701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                  to go upward, {@link android.view.View#FOCUS_DOWN} to downward
11711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param top       the top offset of the new area to be made visible
11721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param bottom    the bottom offset of the new area to be made visible
11731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return true if the key event is consumed by this method, false otherwise
11741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
11751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean scrollAndFocus(int direction, int top, int bottom) {
11761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        boolean handled = true;
11771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int height = getHeight();
11791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int containerTop = getScrollY();
11801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int containerBottom = containerTop + height;
11811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        boolean up = direction == View.FOCUS_UP;
11821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        View newFocused = findFocusableViewInBounds(up, top, bottom);
11841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (newFocused == null) {
11851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            newFocused = this;
11861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
11871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (top >= containerTop && bottom <= containerBottom) {
11891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            handled = false;
11901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else {
11911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int delta = up ? (top - containerTop) : (bottom - containerBottom);
11921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            doScrollY(delta);
11931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
11941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (newFocused != findFocus()) newFocused.requestFocus(direction);
11961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
11971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return handled;
11981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
11991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
12011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Handle scrolling in response to an up or down arrow click.
12021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
12031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param direction The direction corresponding to the arrow key that was
12041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                  pressed
12051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return True if we consumed the event, false otherwise
12061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
12071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean arrowScroll(int direction) {
12081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        View currentFocused = findFocus();
12101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (currentFocused == this) currentFocused = null;
12111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
12131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int maxJump = getMaxScrollAmount();
12151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (nextFocused != null && isWithinDeltaOfScreen(nextFocused, maxJump, getHeight())) {
12171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            nextFocused.getDrawingRect(mTempRect);
12181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            offsetDescendantRectToMyCoords(nextFocused, mTempRect);
12191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
12201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            doScrollY(scrollDelta);
12211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            nextFocused.requestFocus(direction);
12221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else {
12231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // no new focus
12241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int scrollDelta = maxJump;
12251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (direction == View.FOCUS_UP && getScrollY() < scrollDelta) {
12271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollDelta = getScrollY();
12281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            } else if (direction == View.FOCUS_DOWN) {
12291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (getChildCount() > 0) {
12301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    int daBottom = getChildAt(0).getBottom();
12311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    int screenBottom = getScrollY() + getHeight() - getPaddingBottom();
12321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (daBottom - screenBottom < maxJump) {
12331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        scrollDelta = daBottom - screenBottom;
12341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
12351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
12361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
12371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (scrollDelta == 0) {
12381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                return false;
12391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
12401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            doScrollY(direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta);
12411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
12421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (currentFocused != null && currentFocused.isFocused()
12441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                && isOffScreen(currentFocused)) {
12451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // previously focused item still has focus and is off screen, give
12461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // it up (take it back to ourselves)
12471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // (also, need to temporarily force FOCUS_BEFORE_DESCENDANTS so we are
12481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // sure to
12491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // get it)
12501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int descendantFocusability = getDescendantFocusability();  // save
12511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
12521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            requestFocus();
12531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            setDescendantFocusability(descendantFocusability);  // restore
12541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
12551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return true;
12561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
12571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
12591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return whether the descendant of this scroll view is scrolled off
12601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *  screen.
12611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
12621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean isOffScreen(View descendant) {
12631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return !isWithinDeltaOfScreen(descendant, 0, getHeight());
12641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
12651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
12671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return whether the descendant of this scroll view is within delta
12681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *  pixels of being on the screen.
12691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
12701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean isWithinDeltaOfScreen(View descendant, int delta, int height) {
12711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        descendant.getDrawingRect(mTempRect);
12721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        offsetDescendantRectToMyCoords(descendant, mTempRect);
12731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return (mTempRect.bottom + delta) >= getScrollY()
12751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                && (mTempRect.top - delta) <= (getScrollY() + height);
12761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
12771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
12791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Smooth scroll by a Y delta
12801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
12811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param delta the number of pixels to scroll by on the Y axis
12821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
12831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void doScrollY(int delta) {
12841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (delta != 0) {
12851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (mSmoothScrollingEnabled) {
12861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                smoothScrollBy(0, delta);
12871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            } else {
12881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollBy(0, delta);
12891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
12901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
12911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
12921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
12931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
12941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Like {@link View#scrollBy}, but scroll smoothly instead of immediately.
12951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
12961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param dx the number of pixels to scroll by on the X axis
12971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param dy the number of pixels to scroll by on the Y axis
12981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
12991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public final void smoothScrollBy(int dx, int dy) {
13001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() == 0) {
13011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // Nothing to do.
13021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return;
13031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
13041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
13051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (duration > ANIMATED_SCROLL_GAP) {
13061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int height = getHeight() - getPaddingBottom() - getPaddingTop();
13071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int bottom = getChildAt(0).getHeight();
13081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int maxY = Math.max(0, bottom - height);
13091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int scrollY = getScrollY();
13101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            dy = Math.max(0, Math.min(scrollY + dy, maxY)) - scrollY;
13111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mScroller.startScroll(getScrollX(), scrollY, 0, dy);
13131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            ViewCompat.postInvalidateOnAnimation(this);
13141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else {
13151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (!mScroller.isFinished()) {
13161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mScroller.abortAnimation();
13171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
13181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollBy(dx, dy);
13191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
13201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mLastScroll = AnimationUtils.currentAnimationTimeMillis();
13211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
13221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
13241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Like {@link #scrollTo}, but scroll smoothly instead of immediately.
13251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
13261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param x the position where to scroll on the X axis
13271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param y the position where to scroll on the Y axis
13281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
13291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public final void smoothScrollTo(int x, int y) {
13301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        smoothScrollBy(x - getScrollX(), y - getScrollY());
13311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
13321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
13341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * <p>The scroll range of a scroll view is the overall height of all of its
13351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * children.</p>
13361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
13371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
13381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected int computeVerticalScrollRange() {
13391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int count = getChildCount();
13401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int contentHeight = getHeight() - getPaddingBottom() - getPaddingTop();
13411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (count == 0) {
13421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return contentHeight;
13431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
13441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int scrollRange = getChildAt(0).getBottom();
13461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int scrollY = getScrollY();
13471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int overscrollBottom = Math.max(0, scrollRange - contentHeight);
13481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (scrollY < 0) {
13491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollRange -= scrollY;
13501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else if (scrollY > overscrollBottom) {
13511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollRange += scrollY - overscrollBottom;
13521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
13531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return scrollRange;
13551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
13561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
13581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected int computeVerticalScrollOffset() {
13591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return Math.max(0, super.computeVerticalScrollOffset());
13601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
13611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
13631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
13641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        ViewGroup.LayoutParams lp = child.getLayoutParams();
13651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int childWidthMeasureSpec;
13671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int childHeightMeasureSpec;
13681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, getPaddingLeft()
13701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                + getPaddingRight(), lp.width);
13711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
13731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
13751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
13761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
13781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
13791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int parentHeightMeasureSpec, int heightUsed) {
13801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
13811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
13831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
13841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        + widthUsed, lp.width);
13851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
13861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
13871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
13891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
13901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
13921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void computeScroll() {
13931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mScroller.computeScrollOffset()) {
13941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int oldX = getScrollX();
13951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int oldY = getScrollY();
13961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int x = mScroller.getCurrX();
13971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int y = mScroller.getCurrY();
13981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
13991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (oldX != x || oldY != y) {
14001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int range = getScrollRange();
14011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int overscrollMode = ViewCompat.getOverScrollMode(this);
14021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final boolean canOverscroll = overscrollMode == ViewCompat.OVER_SCROLL_ALWAYS ||
14031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        (overscrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);
14041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                overScrollByCompat(x - oldX, y - oldY, oldX, oldY, 0, range,
14061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        0, 0, false);
14071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (canOverscroll) {
14091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    ensureGlows();
14101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (y <= 0 && oldY > 0) {
14111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        mEdgeGlowTop.onAbsorb((int) mScroller.getCurrVelocity());
14121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    } else if (y >= range && oldY < range) {
14131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        mEdgeGlowBottom.onAbsorb((int) mScroller.getCurrVelocity());
14141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
14151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
14161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
14171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
14181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
14191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
14211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Scrolls the view to the given child.
14221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
14231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param child the View to scroll to
14241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
14251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void scrollToChild(View child) {
14261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        child.getDrawingRect(mTempRect);
14271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        /* Offset from child's local coordinates to ScrollView coordinates */
14291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        offsetDescendantRectToMyCoords(child, mTempRect);
14301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
14321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (scrollDelta != 0) {
14341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollBy(0, scrollDelta);
14351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
14361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
14371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
14391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * If rect is off screen, scroll just enough to get it (or at least the
14401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * first screen size chunk of it) on screen.
14411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
14421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param rect      The rectangle.
14431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param immediate True to scroll immediately without animation
14441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return true if scrolling was performed
14451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
14461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private boolean scrollToChildRect(Rect rect, boolean immediate) {
14471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int delta = computeScrollDeltaToGetChildRectOnScreen(rect);
14481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final boolean scroll = delta != 0;
14491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (scroll) {
14501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (immediate) {
14511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollBy(0, delta);
14521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            } else {
14531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                smoothScrollBy(0, delta);
14541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
14551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
14561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return scroll;
14571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
14581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
14601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Compute the amount to scroll in the Y direction in order to get
14611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * a rectangle completely on the screen (or, if taller than the screen,
14621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * at least the first screen size chunk of it).
14631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
14641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param rect The rect.
14651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @return The scroll delta.
14661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
14671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
14681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() == 0) return 0;
14691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int height = getHeight();
14711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int screenTop = getScrollY();
14721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int screenBottom = screenTop + height;
14731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int fadingEdge = getVerticalFadingEdgeLength();
14751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // leave room for top fading edge as long as rect isn't at very top
14771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (rect.top > 0) {
14781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            screenTop += fadingEdge;
14791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
14801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // leave room for bottom fading edge as long as rect isn't at very bottom
14821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (rect.bottom < getChildAt(0).getHeight()) {
14831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            screenBottom -= fadingEdge;
14841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
14851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        int scrollYDelta = 0;
14871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (rect.bottom > screenBottom && rect.top > screenTop) {
14891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // need to move down to get it in view: move down just enough so
14901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // that the entire rectangle is in view (or at least the first
14911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // screen size chunk).
14921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
14931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (rect.height() > height) {
14941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                // just enough to get screen size chunk on
14951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollYDelta += (rect.top - screenTop);
14961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            } else {
14971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                // get entire rect at bottom of screen
14981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollYDelta += (rect.bottom - screenBottom);
14991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
15001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // make sure we aren't scrolling beyond the end of our content
15021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int bottom = getChildAt(0).getBottom();
15031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int distanceToBottom = bottom - screenBottom;
15041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
15051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else if (rect.top < screenTop && rect.bottom < screenBottom) {
15071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // need to move up to get it in view: move up just enough so that
15081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // entire rectangle is in view (or at least the first screen
15091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // size chunk of it).
15101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (rect.height() > height) {
15121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                // screen size chunk
15131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollYDelta -= (screenBottom - rect.bottom);
15141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            } else {
15151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                // entire rect at top
15161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollYDelta -= (screenTop - rect.top);
15171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
15181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // make sure we aren't scrolling any further than the top our content
15201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollYDelta = Math.max(scrollYDelta, -getScrollY());
15211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
15221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return scrollYDelta;
15231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
15241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
15261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void requestChildFocus(View child, View focused) {
15271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (!mIsLayoutDirty) {
15281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollToChild(focused);
15291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else {
15301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // The child may not be laid out yet, we can't compute the scroll yet
15311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mChildToScrollTo = focused;
15321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
15331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.requestChildFocus(child, focused);
15341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
15351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
15381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * When looking for focus in children of a scroll view, need to be a little
15391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * more careful not to give focus to something that is scrolled off screen.
15401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
15411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * This is more expensive than the default {@link android.view.ViewGroup}
15421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * implementation, otherwise this behavior might have been made the default.
15431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
15441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
15451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected boolean onRequestFocusInDescendants(int direction,
15461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            Rect previouslyFocusedRect) {
15471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // convert from forward / backward notation to up / down / left / right
15491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // (ugh).
15501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (direction == View.FOCUS_FORWARD) {
15511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            direction = View.FOCUS_DOWN;
15521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else if (direction == View.FOCUS_BACKWARD) {
15531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            direction = View.FOCUS_UP;
15541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
15551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final View nextFocus = previouslyFocusedRect == null ?
15571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                FocusFinder.getInstance().findNextFocus(this, null, direction) :
15581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                FocusFinder.getInstance().findNextFocusFromRect(this,
15591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        previouslyFocusedRect, direction);
15601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (nextFocus == null) {
15621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return false;
15631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
15641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (isOffScreen(nextFocus)) {
15661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return false;
15671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
15681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return nextFocus.requestFocus(direction, previouslyFocusedRect);
15701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
15711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
15731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
15741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            boolean immediate) {
15751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // offset into coordinate space of this scroll view
15761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        rectangle.offset(child.getLeft() - child.getScrollX(),
15771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                child.getTop() - child.getScrollY());
15781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return scrollToChildRect(rectangle, immediate);
15801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
15811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
15831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void requestLayout() {
15841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mIsLayoutDirty = true;
15851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.requestLayout();
15861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
15871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
15891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected void onLayout(boolean changed, int l, int t, int r, int b) {
15901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.onLayout(changed, l, t, r, b);
15911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mIsLayoutDirty = false;
15921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // Give a child focus if it needs it
15931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this)) {
15941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollToChild(mChildToScrollTo);
15951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
15961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mChildToScrollTo = null;
15971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
15981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (!mIsLaidOut) {
15991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (mSavedState != null) {
16001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollTo(getScrollX(), mSavedState.scrollPosition);
16011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mSavedState = null;
16021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            } // mScrollY default value is "0"
16031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int childHeight = (getChildCount() > 0) ? getChildAt(0).getMeasuredHeight() : 0;
16051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int scrollRange = Math.max(0,
16061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    childHeight - (b - t - getPaddingBottom() - getPaddingTop()));
16071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            // Don't forget to clamp
16091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (getScrollY() > scrollRange) {
16101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollTo(getScrollX(), scrollRange);
16111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            } else if (getScrollY() < 0) {
16121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                scrollTo(getScrollX(), 0);
16131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
16141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
16151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // Calling this with the present values causes it to re-claim them
16171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        scrollTo(getScrollX(), getScrollY());
16181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mIsLaidOut = true;
16191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
16201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
16221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void onAttachedToWindow() {
16231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mIsLaidOut = false;
16241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
16251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
16271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
16281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.onSizeChanged(w, h, oldw, oldh);
16291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        View currentFocused = findFocus();
16311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (null == currentFocused || this == currentFocused)
16321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return;
16331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // If the currently-focused view was visible on the screen when the
16351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // screen was at the old height, then scroll the screen to make that
16361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // view visible with the new screen height.
16371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (isWithinDeltaOfScreen(currentFocused, 0, oldh)) {
16381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            currentFocused.getDrawingRect(mTempRect);
16391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            offsetDescendantRectToMyCoords(currentFocused, mTempRect);
16401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
16411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            doScrollY(scrollDelta);
16421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
16431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
16441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
16461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Return true if child is a descendant of parent, (or equal to the parent).
16471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
16481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private static boolean isViewDescendantOf(View child, View parent) {
16491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (child == parent) {
16501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return true;
16511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
16521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final ViewParent theParent = child.getParent();
16541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
16551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
16561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
16581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * Fling the scroll view
16591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
16601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * @param velocityY The initial velocity in the Y direction. Positive
16611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                  numbers mean that the finger/cursor is moving down the screen,
16621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *                  which means we want to scroll towards the top.
16631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
16641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void fling(int velocityY) {
16651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
16661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int height = getHeight() - getPaddingBottom() - getPaddingTop();
16671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            int bottom = getChildAt(0).getHeight();
16681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mScroller.fling(getScrollX(), getScrollY(), 0, velocityY, 0, 0, 0,
16701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    Math.max(0, bottom - height), 0, height/2);
16711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            ViewCompat.postInvalidateOnAnimation(this);
16731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
16741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
16751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void flingWithNestedDispatch(int velocityY) {
16771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final int scrollY = getScrollY();
16781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        final boolean canFling = (scrollY > 0 || velocityY > 0) &&
16791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                (scrollY < getScrollRange() || velocityY < 0);
16801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (!dispatchNestedPreFling(0, velocityY)) {
16811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            dispatchNestedFling(0, velocityY, canFling);
16821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (canFling) {
16831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                fling(velocityY);
16841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
16851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
16861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
16871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void endDrag() {
16891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mIsBeingDragged = false;
16901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        recycleVelocityTracker();
1692d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes        stopNestedScroll();
16931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
16941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mEdgeGlowTop != null) {
16951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mEdgeGlowTop.onRelease();
16961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mEdgeGlowBottom.onRelease();
16971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
16981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
16991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
17001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    /**
17011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * {@inheritDoc}
17021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     *
17031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     * <p>This version also clamps the scrolling to the bounds of our child.
17041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell     */
17051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
17061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void scrollTo(int x, int y) {
17071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        // we rely on the fact the View.scrollBy calls scrollTo.
17081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (getChildCount() > 0) {
17091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            View child = getChildAt(0);
17101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth());
17111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            y = clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(), child.getHeight());
17121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (x != getScrollX() || y != getScrollY()) {
17131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                super.scrollTo(x, y);
17141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
17151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
17161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
17171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
17181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private void ensureGlows() {
17191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (ViewCompat.getOverScrollMode(this) != ViewCompat.OVER_SCROLL_NEVER) {
17201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (mEdgeGlowTop == null) {
17211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                Context context = getContext();
17221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mEdgeGlowTop = new EdgeEffectCompat(context);
17231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mEdgeGlowBottom = new EdgeEffectCompat(context);
17241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
17251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        } else {
17261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mEdgeGlowTop = null;
17271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            mEdgeGlowBottom = null;
17281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
17291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
17301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
17311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
17321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    public void draw(Canvas canvas) {
17331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.draw(canvas);
17341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (mEdgeGlowTop != null) {
17351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final int scrollY = getScrollY();
17361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (!mEdgeGlowTop.isFinished()) {
17371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int restoreCount = canvas.save();
17381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int width = getWidth() - getPaddingLeft() - getPaddingRight();
17391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
17401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                canvas.translate(getPaddingLeft(), Math.min(0, scrollY));
17411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mEdgeGlowTop.setSize(width, getHeight());
17421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (mEdgeGlowTop.draw(canvas)) {
17431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    ViewCompat.postInvalidateOnAnimation(this);
17441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
17451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                canvas.restoreToCount(restoreCount);
17461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
17471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (!mEdgeGlowBottom.isFinished()) {
17481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int restoreCount = canvas.save();
17491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int width = getWidth() - getPaddingLeft() - getPaddingRight();
17501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int height = getHeight();
17511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
17521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                canvas.translate(-width + getPaddingLeft(),
17531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        Math.max(getScrollRange(), scrollY) + height);
17541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                canvas.rotate(180, width, 0);
17551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                mEdgeGlowBottom.setSize(width, height);
17561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (mEdgeGlowBottom.draw(canvas)) {
17571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    ViewCompat.postInvalidateOnAnimation(this);
17581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
17591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                canvas.restoreToCount(restoreCount);
17601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
17611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
17621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
17631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
17641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    private static int clamp(int n, int my, int child) {
17651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if (my >= child || n < 0) {
17661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            /* my >= child is this case:
17671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *                    |--------------- me ---------------|
17681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *     |------ child ------|
17691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             * or
17701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *     |--------------- me ---------------|
17711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *            |------ child ------|
17721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             * or
17731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *     |--------------- me ---------------|
17741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *                                  |------ child ------|
17751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *
17761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             * n < 0 is this case:
17771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *     |------ me ------|
17781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *                    |-------- child --------|
17791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *     |-- mScrollX --|
17801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             */
17811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return 0;
17821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
17831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        if ((my+n) > child) {
17841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            /* this case:
17851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *                    |------ me ------|
17861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *     |------ child ------|
17871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             *     |-- mScrollX --|
17881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell             */
17891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return child-my;
17901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
17911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return n;
17921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
17931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
17941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
17951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected void onRestoreInstanceState(Parcelable state) {
17961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        SavedState ss = (SavedState) state;
17971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        super.onRestoreInstanceState(ss.getSuperState());
17981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        mSavedState = ss;
17991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        requestLayout();
18001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
18011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    @Override
18031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    protected Parcelable onSaveInstanceState() {
18041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        Parcelable superState = super.onSaveInstanceState();
18051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        SavedState ss = new SavedState(superState);
18061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        ss.scrollPosition = getScrollY();
18071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        return ss;
18081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
18091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    static class SavedState extends BaseSavedState {
18111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        public int scrollPosition;
18121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        SavedState(Parcelable superState) {
18141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            super(superState);
18151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
18161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18171fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        public SavedState(Parcel source) {
18181fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            super(source);
18191fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            scrollPosition = source.readInt();
18201fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
18211fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18221fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        @Override
18231fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        public void writeToParcel(Parcel dest, int flags) {
18241fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            super.writeToParcel(dest, flags);
18251fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            dest.writeInt(scrollPosition);
18261fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
18271fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18281fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        @Override
18291fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        public String toString() {
18301fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return "HorizontalScrollView.SavedState{"
18311fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    + Integer.toHexString(System.identityHashCode(this))
18321fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    + " scrollPosition=" + scrollPosition + "}";
18331fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
18341fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18351fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        public static final Parcelable.Creator<SavedState> CREATOR
18361fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                = new Parcelable.Creator<SavedState>() {
18371fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            public SavedState createFromParcel(Parcel in) {
18381fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                return new SavedState(in);
18391fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
18401fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18411fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            public SavedState[] newArray(int size) {
18421fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                return new SavedState[size];
18431fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
18441fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        };
18451fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
18461fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18471fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    static class AccessibilityDelegate extends AccessibilityDelegateCompat {
18481fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        @Override
18491fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        public boolean performAccessibilityAction(View host, int action, Bundle arguments) {
18501fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (super.performAccessibilityAction(host, action, arguments)) {
18511fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                return true;
18521fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
18531fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final NestedScrollView nsvHost = (NestedScrollView) host;
18541fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (!nsvHost.isEnabled()) {
18551fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                return false;
18561fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
18571fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            switch (action) {
18581fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                case AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD: {
18591fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int viewportHeight = nsvHost.getHeight() - nsvHost.getPaddingBottom()
18601fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            - nsvHost.getPaddingTop();
18611fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int targetScrollY = Math.min(nsvHost.getScrollY() + viewportHeight,
18621fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            nsvHost.getScrollRange());
18631fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (targetScrollY != nsvHost.getScrollY()) {
18641fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        nsvHost.smoothScrollTo(0, targetScrollY);
18651fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        return true;
18661fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
18671fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
18681fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                return false;
18691fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                case AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD: {
18701fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int viewportHeight = nsvHost.getHeight() - nsvHost.getPaddingBottom()
18711fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                            - nsvHost.getPaddingTop();
18721fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    final int targetScrollY = Math.max(nsvHost.getScrollY() - viewportHeight, 0);
18731fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (targetScrollY != nsvHost.getScrollY()) {
18741fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        nsvHost.smoothScrollTo(0, targetScrollY);
18751fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        return true;
18761fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
18771fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
18781fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                return false;
18791fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
18801fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            return false;
18811fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
18821fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
18831fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        @Override
18841fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
18851fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            super.onInitializeAccessibilityNodeInfo(host, info);
18861fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final NestedScrollView nsvHost = (NestedScrollView) host;
18871fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            info.setClassName(ScrollView.class.getName());
18881fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            if (nsvHost.isEnabled()) {
18891fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                final int scrollRange = nsvHost.getScrollRange();
18901fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                if (scrollRange > 0) {
18911fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    info.setScrollable(true);
18921fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (nsvHost.getScrollY() > 0) {
18931fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
18941fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
18951fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    if (nsvHost.getScrollY() < scrollRange) {
18961fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                        info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
18971fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                    }
18981fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell                }
18991fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            }
19001fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
19011fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell
19021fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        @Override
19031fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
19041fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            super.onInitializeAccessibilityEvent(host, event);
19051fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final NestedScrollView nsvHost = (NestedScrollView) host;
19061fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            event.setClassName(ScrollView.class.getName());
19071fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
19081fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            final boolean scrollable = nsvHost.getScrollRange() > 0;
19091fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            record.setScrollable(scrollable);
19101fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            record.setScrollX(nsvHost.getScrollX());
19111fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            record.setScrollY(nsvHost.getScrollY());
19121fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            record.setMaxScrollX(nsvHost.getScrollX());
19131fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell            record.setMaxScrollY(nsvHost.getScrollRange());
19141fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell        }
19151fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell    }
19161fcce4485ef99aca928ebfb877859c5ecd47716cAdam Powell}
1917