AppBarLayout.java revision 4b25d63ff53d4676150ac54897851e4ff9f1049f
1a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes/*
2a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * Copyright (C) 2015 The Android Open Source Project
3a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
4a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * Licensed under the Apache License, Version 2.0 (the "License");
5a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * you may not use this file except in compliance with the License.
6a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * You may obtain a copy of the License at
7a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
8a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *      http://www.apache.org/licenses/LICENSE-2.0
9a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
10a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * Unless required by applicable law or agreed to in writing, software
11a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * distributed under the License is distributed on an "AS IS" BASIS,
12a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * See the License for the specific language governing permissions and
14a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * limitations under the License.
15a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes */
16a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
17a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banespackage android.support.design.widget;
18a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
198e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikasimport static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
208e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikasimport static android.support.design.widget.ViewUtils.objectEquals;
218e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas
22ae7189188acb2bc972b08d9fbd59939f341c7829Aurimas Liutikasimport android.animation.ValueAnimator;
23a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.content.Context;
24a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.content.res.TypedArray;
2500a00a7d3ba8279294f63994473afc32e05dcf10Chris Banesimport android.graphics.Rect;
261a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banesimport android.os.Build;
27e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banesimport android.os.Parcel;
28e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banesimport android.os.Parcelable;
2950dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banesimport android.support.annotation.IntDef;
3072a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banesimport android.support.annotation.NonNull;
3172a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banesimport android.support.annotation.Nullable;
328f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viveretteimport android.support.annotation.RequiresApi;
33c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo;
3413633353237d6506f4c436b459bc8be8c3d7ed68Chris Banesimport android.support.annotation.VisibleForTesting;
35a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.support.design.R;
3605f5ba020fa6caa658c75b6d77436aa980ca0fccChris Banesimport android.support.v4.view.AbsSavedState;
37a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.support.v4.view.ViewCompat;
386ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banesimport android.support.v4.view.WindowInsetsCompat;
39a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.util.AttributeSet;
40a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.view.View;
41a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.view.ViewGroup;
42a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.view.animation.Interpolator;
43a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport android.widget.LinearLayout;
44a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
4550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banesimport java.lang.annotation.Retention;
4650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banesimport java.lang.annotation.RetentionPolicy;
47d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banesimport java.lang.ref.WeakReference;
48631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banesimport java.util.ArrayList;
49a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banesimport java.util.List;
50a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
51a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes/**
52a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * AppBarLayout is a vertical {@link LinearLayout} which implements many of the features of
539fb154338a62edc2c57dc036895199d6f1769400Chris Banes * material designs app bar concept, namely scrolling gestures.
54a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * <p>
55a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * Children should provide their desired scrolling behavior through
56a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * {@link LayoutParams#setScrollFlags(int)} and the associated layout xml attribute:
57a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * {@code app:layout_scrollFlags}.
58a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
59a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * <p>
60a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * This view depends heavily on being used as a direct child within a {@link CoordinatorLayout}.
61a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * If you use AppBarLayout within a different {@link ViewGroup}, most of it's functionality will
62a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * not work.
63a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * <p>
649fb154338a62edc2c57dc036895199d6f1769400Chris Banes * AppBarLayout also requires a separate scrolling sibling in order to know when to scroll.
659fb154338a62edc2c57dc036895199d6f1769400Chris Banes * The binding is done through the {@link ScrollingViewBehavior} behavior class, meaning that you
669fb154338a62edc2c57dc036895199d6f1769400Chris Banes * should set your scrolling view's behavior to be an instance of {@link ScrollingViewBehavior}.
679fb154338a62edc2c57dc036895199d6f1769400Chris Banes * A string resource containing the full class name is available.
68a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
69a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * <pre>
70a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * &lt;android.support.design.widget.CoordinatorLayout
71a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *         xmlns:android=&quot;http://schemas.android.com/apk/res/android";
72a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *         xmlns:app=&quot;http://schemas.android.com/apk/res-auto";
73a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *         android:layout_width=&quot;match_parent&quot;
74a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *         android:layout_height=&quot;match_parent&quot;&gt;
75a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
76a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *     &lt;android.support.v4.widget.NestedScrollView
77a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *             android:layout_width=&quot;match_parent&quot;
78a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *             android:layout_height=&quot;match_parent&quot;
79a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *             app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;&gt;
80a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
81a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *         &lt;!-- Your scrolling content --&gt;
82a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
83a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *     &lt;/android.support.v4.widget.NestedScrollView&gt;
84a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
85a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *     &lt;android.support.design.widget.AppBarLayout
86a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *             android:layout_height=&quot;wrap_content&quot;
87a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *             android:layout_width=&quot;match_parent&quot;&gt;
88a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
89a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *         &lt;android.support.v7.widget.Toolbar
90a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *                 ...
91a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *                 app:layout_scrollFlags=&quot;scroll|enterAlways&quot;/&gt;
92a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
93a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *         &lt;android.support.design.widget.TabLayout
94a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *                 ...
95a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *                 app:layout_scrollFlags=&quot;scroll|enterAlways&quot;/&gt;
96a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
97a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *     &lt;/android.support.design.widget.AppBarLayout&gt;
98a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
99a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * &lt;/android.support.design.widget.CoordinatorLayout&gt;
100a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * </pre>
101a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *
102a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes * @see <a href="http://www.google.com/design/spec/layout/structure.html#structure-app-bar">
103a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes *     http://www.google.com/design/spec/layout/structure.html#structure-app-bar</a>
104a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes */
105a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes@CoordinatorLayout.DefaultBehavior(AppBarLayout.Behavior.class)
106a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banespublic class AppBarLayout extends LinearLayout {
107a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
108657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    static final int PENDING_ACTION_NONE = 0x0;
109657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    static final int PENDING_ACTION_EXPANDED = 0x1;
110657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    static final int PENDING_ACTION_COLLAPSED = 0x2;
111657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    static final int PENDING_ACTION_ANIMATE_ENABLED = 0x4;
1122bab57ed97f284a8152d180cca796739afa3a71dChris Banes    static final int PENDING_ACTION_FORCE = 0x8;
1139995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes
114a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    /**
115631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     * Interface definition for a callback to be invoked when an {@link AppBarLayout}'s vertical
116631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     * offset changes.
117a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     */
118631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    public interface OnOffsetChangedListener {
119a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
120a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * Called when the {@link AppBarLayout}'s layout offset has been changed. This allows
121a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * child views to implement custom behavior based on the offset (for instance pinning a
122a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * view at a certain y value).
123a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
124631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes         * @param appBarLayout the {@link AppBarLayout} which offset has changed
12550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes         * @param verticalOffset the vertical offset for the parent {@link AppBarLayout}, in px
126a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
127631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes        void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset);
128a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
129a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
130a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    private static final int INVALID_SCROLL_RANGE = -1;
131a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
132a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    private int mTotalScrollRange = INVALID_SCROLL_RANGE;
133a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    private int mDownPreScrollRange = INVALID_SCROLL_RANGE;
134a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    private int mDownScrollRange = INVALID_SCROLL_RANGE;
135a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1361a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    private boolean mHaveChildWithInterpolator;
13750dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
1389995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    private int mPendingAction = PENDING_ACTION_NONE;
1399995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes
1406ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes    private WindowInsetsCompat mLastInsets;
1416ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes
1421a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    private List<OnOffsetChangedListener> mListeners;
1431a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
1441a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    private boolean mCollapsible;
1451a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    private boolean mCollapsed;
1461a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
1471a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    private final int[] mTmpStatesArray = new int[2];
148631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
149a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    public AppBarLayout(Context context) {
150a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        this(context, null);
151a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
152a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
153a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    public AppBarLayout(Context context, AttributeSet attrs) {
154a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        super(context, attrs);
155a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        setOrientation(VERTICAL);
15650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
157809bb62055ad42b88f3a69308be222801b89fbd9Chris Banes        ThemeUtils.checkAppCompatTheme(context);
158809bb62055ad42b88f3a69308be222801b89fbd9Chris Banes
1591a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        if (Build.VERSION.SDK_INT >= 21) {
1601a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            // Use the bounds view outline provider so that we cast a shadow, even without a
1611a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            // background
1621a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            ViewUtilsLollipop.setBoundsViewOutlineProvider(this);
1631a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
1641a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            // If we're running on API 21+, we should reset any state list animator from our
1651a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            // default style
1661a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            ViewUtilsLollipop.setStateListAnimatorFromAttrs(this, attrs, 0,
1671a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    R.style.Widget_Design_AppBarLayout);
1681a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
1691a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
1701a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppBarLayout,
17150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                0, R.style.Widget_Design_AppBarLayout);
17247082c30c630c34829439a9eecd1cf7e8d255a86Aurimas Liutikas        ViewCompat.setBackground(this, a.getDrawable(R.styleable.AppBarLayout_android_background));
1739995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes        if (a.hasValue(R.styleable.AppBarLayout_expanded)) {
1742bab57ed97f284a8152d180cca796739afa3a71dChris Banes            setExpanded(a.getBoolean(R.styleable.AppBarLayout_expanded, false), false, false);
1759995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes        }
1761a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        if (Build.VERSION.SDK_INT >= 21 && a.hasValue(R.styleable.AppBarLayout_elevation)) {
1771a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            ViewUtilsLollipop.setDefaultAppBarLayoutStateListAnimator(
1781a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    this, a.getDimensionPixelSize(R.styleable.AppBarLayout_elevation, 0));
1791a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
18050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        a.recycle();
18150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
1826ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes        ViewCompat.setOnApplyWindowInsetsListener(this,
1836ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                new android.support.v4.view.OnApplyWindowInsetsListener() {
1846ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                    @Override
1856ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                    public WindowInsetsCompat onApplyWindowInsets(View v,
1866ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                            WindowInsetsCompat insets) {
1878818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes                        return onWindowInsetChanged(insets);
1886ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                    }
1896ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                });
190631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    }
191631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
192631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    /**
193631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     * Add a listener that will be called when the offset of this {@link AppBarLayout} changes.
194631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     *
195631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     * @param listener The listener that will be called when the offset changes.]
196631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     *
197631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     * @see #removeOnOffsetChangedListener(OnOffsetChangedListener)
198631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     */
199631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    public void addOnOffsetChangedListener(OnOffsetChangedListener listener) {
2001a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        if (mListeners == null) {
2011a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            mListeners = new ArrayList<>();
2021a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
2036f730c0acfb10a929172ea2981a1aded0e39f5c7Chris Banes        if (listener != null && !mListeners.contains(listener)) {
2046f730c0acfb10a929172ea2981a1aded0e39f5c7Chris Banes            mListeners.add(listener);
205631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes        }
206631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    }
207631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
208631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    /**
209631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     * Remove the previously added {@link OnOffsetChangedListener}.
210631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     *
211631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     * @param listener the listener to remove.
212631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     */
213631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    public void removeOnOffsetChangedListener(OnOffsetChangedListener listener) {
2141a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        if (mListeners != null && listener != null) {
2156f730c0acfb10a929172ea2981a1aded0e39f5c7Chris Banes            mListeners.remove(listener);
216631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes        }
217a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
218a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
219a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    @Override
220ea004a01f44aaf36212120c936952c1742d03d30Chris Banes    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
221ea004a01f44aaf36212120c936952c1742d03d30Chris Banes        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
222ea004a01f44aaf36212120c936952c1742d03d30Chris Banes        invalidateScrollRanges();
223ea004a01f44aaf36212120c936952c1742d03d30Chris Banes    }
224ea004a01f44aaf36212120c936952c1742d03d30Chris Banes
225ea004a01f44aaf36212120c936952c1742d03d30Chris Banes    @Override
226a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    protected void onLayout(boolean changed, int l, int t, int r, int b) {
227a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        super.onLayout(changed, l, t, r, b);
22879aa418487cdefb85d3c1fac32d6403a1ac6057dChris Banes        invalidateScrollRanges();
229a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
230a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        mHaveChildWithInterpolator = false;
231a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        for (int i = 0, z = getChildCount(); i < z; i++) {
232a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final View child = getChildAt(i);
233a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final LayoutParams childLp = (LayoutParams) child.getLayoutParams();
234a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final Interpolator interpolator = childLp.getScrollInterpolator();
235a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
236a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            if (interpolator != null) {
237a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                mHaveChildWithInterpolator = true;
238a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                break;
239a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
240a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
2411a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
2421a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        updateCollapsible();
2431a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    }
2441a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
2451a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    private void updateCollapsible() {
2461a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        boolean haveCollapsibleChild = false;
2471a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        for (int i = 0, z = getChildCount(); i < z; i++) {
2481a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            if (((LayoutParams) getChildAt(i).getLayoutParams()).isCollapsible()) {
2491a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                haveCollapsibleChild = true;
2501a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                break;
2511a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            }
2521a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
25357aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes        setCollapsibleState(haveCollapsibleChild);
254a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
255a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
256ea004a01f44aaf36212120c936952c1742d03d30Chris Banes    private void invalidateScrollRanges() {
257ea004a01f44aaf36212120c936952c1742d03d30Chris Banes        // Invalidate the scroll ranges
258ea004a01f44aaf36212120c936952c1742d03d30Chris Banes        mTotalScrollRange = INVALID_SCROLL_RANGE;
259ea004a01f44aaf36212120c936952c1742d03d30Chris Banes        mDownPreScrollRange = INVALID_SCROLL_RANGE;
260ea004a01f44aaf36212120c936952c1742d03d30Chris Banes        mDownScrollRange = INVALID_SCROLL_RANGE;
261ea004a01f44aaf36212120c936952c1742d03d30Chris Banes    }
262ea004a01f44aaf36212120c936952c1742d03d30Chris Banes
263a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    @Override
264a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    public void setOrientation(int orientation) {
265a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        if (orientation != VERTICAL) {
266a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            throw new IllegalArgumentException("AppBarLayout is always vertical and does"
267a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    + " not support horizontal orientation");
268a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
269a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        super.setOrientation(orientation);
270a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
271a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
2729995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    /**
2739995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * Sets whether this {@link AppBarLayout} is expanded or not, animating if it has already
2749995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * been laid out.
2759995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     *
2769995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * <p>As with {@link AppBarLayout}'s scrolling, this method relies on this layout being a
2779995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * direct child of a {@link CoordinatorLayout}.</p>
2789995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     *
2799995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * @param expanded true if the layout should be fully expanded, false if it should
2809995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     *                 be fully collapsed
2819995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     *
2829995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * @attr ref android.support.design.R.styleable#AppBarLayout_expanded
2839995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     */
2849995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    public void setExpanded(boolean expanded) {
2859995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes        setExpanded(expanded, ViewCompat.isLaidOut(this));
2869995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    }
2879995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes
2889995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    /**
2899995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * Sets whether this {@link AppBarLayout} is expanded or not.
2909995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     *
2919995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * <p>As with {@link AppBarLayout}'s scrolling, this method relies on this layout being a
2929995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * direct child of a {@link CoordinatorLayout}.</p>
2939995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     *
2949995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * @param expanded true if the layout should be fully expanded, false if it should
2959995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     *                 be fully collapsed
2969995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * @param animate Whether to animate to the new state
2979995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     *
2989995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     * @attr ref android.support.design.R.styleable#AppBarLayout_expanded
2999995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes     */
3009995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    public void setExpanded(boolean expanded, boolean animate) {
3012bab57ed97f284a8152d180cca796739afa3a71dChris Banes        setExpanded(expanded, animate, true);
3022bab57ed97f284a8152d180cca796739afa3a71dChris Banes    }
3032bab57ed97f284a8152d180cca796739afa3a71dChris Banes
3042bab57ed97f284a8152d180cca796739afa3a71dChris Banes    private void setExpanded(boolean expanded, boolean animate, boolean force) {
3059995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes        mPendingAction = (expanded ? PENDING_ACTION_EXPANDED : PENDING_ACTION_COLLAPSED)
3062bab57ed97f284a8152d180cca796739afa3a71dChris Banes                | (animate ? PENDING_ACTION_ANIMATE_ENABLED : 0)
3072bab57ed97f284a8152d180cca796739afa3a71dChris Banes                | (force ? PENDING_ACTION_FORCE : 0);
3089995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes        requestLayout();
3099995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    }
3109995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes
311a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    @Override
312a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
313a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        return p instanceof LayoutParams;
314a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
315a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
316a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    @Override
317a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    protected LayoutParams generateDefaultLayoutParams() {
318a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
319a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
320a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
321a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    @Override
322a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    public LayoutParams generateLayoutParams(AttributeSet attrs) {
323a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        return new LayoutParams(getContext(), attrs);
324a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
325a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
326a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    @Override
327a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3288f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viverette        if (Build.VERSION.SDK_INT >= 19 && p instanceof LinearLayout.LayoutParams) {
329a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return new LayoutParams((LinearLayout.LayoutParams) p);
330a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        } else if (p instanceof MarginLayoutParams) {
331a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return new LayoutParams((MarginLayoutParams) p);
332a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
333a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        return new LayoutParams(p);
334a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
335a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
336657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    boolean hasChildWithInterpolator() {
337a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        return mHaveChildWithInterpolator;
338a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
339a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
340a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    /**
3416ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes     * Returns the scroll range of all children.
342a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     *
343a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     * @return the scroll range in px
344a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     */
3456ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes    public final int getTotalScrollRange() {
346a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        if (mTotalScrollRange != INVALID_SCROLL_RANGE) {
347a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return mTotalScrollRange;
348a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
349a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
350a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        int range = 0;
351a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        for (int i = 0, z = getChildCount(); i < z; i++) {
352a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final View child = getChildAt(i);
353a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
354ea004a01f44aaf36212120c936952c1742d03d30Chris Banes            final int childHeight = child.getMeasuredHeight();
355a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final int flags = lp.mScrollFlags;
356a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
357a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) {
358a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // We're set to scroll so add the child's height
359b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes                range += childHeight + lp.topMargin + lp.bottomMargin;
360a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
361a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
362a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    // For a collapsing scroll, we to take the collapsed height into account.
3636ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                    // We also break straight away since later views can't scroll beneath
364a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    // us
3656ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                    range -= ViewCompat.getMinimumHeight(child);
3666ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes                    break;
367a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                }
368a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            } else {
369a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // As soon as a view doesn't have the scroll flag, we end the range calculation.
370a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // This is because views below can not scroll under a fixed view.
371a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                break;
372a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
373a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
3744832063daa6620f0ad51c4a16f31502dab1adc89Chris Banes        return mTotalScrollRange = Math.max(0, range - getTopInset());
375a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
376a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
377657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    boolean hasScrollableChildren() {
378a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        return getTotalScrollRange() != 0;
379a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
380a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
381a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    /**
382a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     * Return the scroll range when scrolling up from a nested pre-scroll.
383a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     */
384657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    int getUpNestedPreScrollRange() {
38550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        return getTotalScrollRange();
386a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
387a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
388a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    /**
389a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     * Return the scroll range when scrolling down from a nested pre-scroll.
390a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     */
391657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    int getDownNestedPreScrollRange() {
392a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        if (mDownPreScrollRange != INVALID_SCROLL_RANGE) {
393a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            // If we already have a valid value, return it
394a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return mDownPreScrollRange;
395a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
396a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
397a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        int range = 0;
398a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        for (int i = getChildCount() - 1; i >= 0; i--) {
399a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final View child = getChildAt(i);
400a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
401ea004a01f44aaf36212120c936952c1742d03d30Chris Banes            final int childHeight = child.getMeasuredHeight();
402a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final int flags = lp.mScrollFlags;
403a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
404a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            if ((flags & LayoutParams.FLAG_QUICK_RETURN) == LayoutParams.FLAG_QUICK_RETURN) {
405b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes                // First take the margin into account
406b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes                range += lp.topMargin + lp.bottomMargin;
407a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // The view has the quick return flag combination...
408a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                if ((flags & LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED) != 0) {
409a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    // If they're set to enter collapsed, use the minimum height
410a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    range += ViewCompat.getMinimumHeight(child);
41147543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes                } else if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
41247543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes                    // Only enter by the amount of the collapsed height
41347543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes                    range += childHeight - ViewCompat.getMinimumHeight(child);
414a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                } else {
415116332176fa904af2f701011de8d4f2524c10a8eChris Banes                    // Else use the full height (minus the top inset)
416116332176fa904af2f701011de8d4f2524c10a8eChris Banes                    range += childHeight - getTopInset();
417a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                }
418a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            } else if (range > 0) {
419a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // If we've hit an non-quick return scrollable view, and we've already hit a
420a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // quick return view, return now
421a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                break;
422a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
423a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
42413633353237d6506f4c436b459bc8be8c3d7ed68Chris Banes        return mDownPreScrollRange = Math.max(0, range);
425a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
426a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
427a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    /**
428a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     * Return the scroll range when scrolling down from a nested scroll.
429a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     */
430657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    int getDownNestedScrollRange() {
431a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        if (mDownScrollRange != INVALID_SCROLL_RANGE) {
432a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            // If we already have a valid value, return it
433a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return mDownScrollRange;
434a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
435a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
436a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        int range = 0;
437c1ce4f6cf4d99d6856c4259bf1bb1cb56f604737Chris Banes        for (int i = 0, z = getChildCount(); i < z; i++) {
438a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final View child = getChildAt(i);
439a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
440ea004a01f44aaf36212120c936952c1742d03d30Chris Banes            int childHeight = child.getMeasuredHeight();
441b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes            childHeight += lp.topMargin + lp.bottomMargin;
442a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
443a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final int flags = lp.mScrollFlags;
444a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
445a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) {
446a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // We're set to scroll so add the child's height
447a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                range += childHeight;
448a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
449a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
450a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    // For a collapsing exit scroll, we to take the collapsed height into account.
4514832063daa6620f0ad51c4a16f31502dab1adc89Chris Banes                    // We also break the range straight away since later views can't scroll
452a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    // beneath us
4534832063daa6620f0ad51c4a16f31502dab1adc89Chris Banes                    range -= ViewCompat.getMinimumHeight(child) + getTopInset();
4544832063daa6620f0ad51c4a16f31502dab1adc89Chris Banes                    break;
455a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                }
456a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            } else {
457a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // As soon as a view doesn't have the scroll flag, we end the range calculation.
458a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                // This is because views below can not scroll under a fixed view.
459a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                break;
460a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
461a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
4624c33be829067714342b629d29329206bc2116afeChris Banes        return mDownScrollRange = Math.max(0, range);
463a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
464a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
465657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    void dispatchOffsetUpdates(int offset) {
4661a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        // Iterate backwards through the list so that most recently added listeners
4671a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        // get the first chance to decide
4681a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        if (mListeners != null) {
4691a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            for (int i = 0, z = mListeners.size(); i < z; i++) {
4701a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                final OnOffsetChangedListener listener = mListeners.get(i);
4711a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                if (listener != null) {
4721a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    listener.onOffsetChanged(this, offset);
4731a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                }
4741a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            }
4751a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
4761a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    }
4771a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
47850dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes    final int getMinimumHeightForVisibleOverlappingContent() {
47917ed3263761329f6aa6796941358c41001fff325Chris Banes        final int topInset = getTopInset();
48050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        final int minHeight = ViewCompat.getMinimumHeight(this);
48150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        if (minHeight != 0) {
48250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes            // If this layout has a min height, use it (doubled)
4836ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes            return (minHeight * 2) + topInset;
48450dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        }
48550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
48650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        // Otherwise, we'll use twice the min height of our last child
48750dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        final int childCount = getChildCount();
488b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes        final int lastChildMinHeight = childCount >= 1
489b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes                ? ViewCompat.getMinimumHeight(getChildAt(childCount - 1)) : 0;
490b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes        if (lastChildMinHeight != 0) {
491b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes            return (lastChildMinHeight * 2) + topInset;
492b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes        }
493b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes
494b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes        // If we reach here then we don't have a min height explicitly set. Instead we'll take a
495b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes        // guess at 1/3 of our height being visible
496b75a16fb6433ab27973d419d23ed1286c073fca8Chris Banes        return getHeight() / 3;
49750dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes    }
49850dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
4991a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    @Override
5001a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    protected int[] onCreateDrawableState(int extraSpace) {
5011a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        final int[] extraStates = mTmpStatesArray;
5021a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        final int[] states = super.onCreateDrawableState(extraSpace + extraStates.length);
5031a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
5041a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        extraStates[0] = mCollapsible ? R.attr.state_collapsible : -R.attr.state_collapsible;
5051a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        extraStates[1] = mCollapsible && mCollapsed
5061a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                ? R.attr.state_collapsed : -R.attr.state_collapsed;
5071a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
5081a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        return mergeDrawableStates(states, extraStates);
5091a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    }
5101a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
511c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes    /**
512c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes     * Sets whether the AppBarLayout has collapsible children or not.
513c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes     *
514c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes     * @return true if the collapsible state changed
515c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes     */
516c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes    private boolean setCollapsibleState(boolean collapsible) {
5171a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        if (mCollapsible != collapsible) {
5181a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            mCollapsible = collapsible;
5191a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            refreshDrawableState();
520c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes            return true;
5211a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
522c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes        return false;
5231a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    }
5241a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
525c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes    /**
526c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes     * Sets whether the AppBarLayout is in a collapsed state or not.
527c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes     *
528c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes     * @return true if the collapsed state changed
529c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes     */
530657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    boolean setCollapsedState(boolean collapsed) {
5311a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        if (mCollapsed != collapsed) {
5321a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            mCollapsed = collapsed;
5331a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            refreshDrawableState();
534c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes            return true;
5351a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
536c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes        return false;
5371a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    }
5381a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
53950dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes    /**
5401a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes     * @deprecated target elevation is now deprecated. AppBarLayout's elevation is now
541926cd28257536b1206934d69585a2dab100147d5Chris Banes     * controlled via a {@link android.animation.StateListAnimator}. If a target
542c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas     * elevation is set, either by this method or the {@code app:elevation} attribute,
543926cd28257536b1206934d69585a2dab100147d5Chris Banes     * a new state list animator is created which uses the given {@code elevation} value.
544631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     *
5451a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes     * @attr ref android.support.design.R.styleable#AppBarLayout_elevation
54650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes     */
5471a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    @Deprecated
548631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    public void setTargetElevation(float elevation) {
5491a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        if (Build.VERSION.SDK_INT >= 21) {
5501a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            ViewUtilsLollipop.setDefaultAppBarLayoutStateListAnimator(this, elevation);
5511a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
552631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    }
553631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
554631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    /**
5551a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes     * @deprecated target elevation is now deprecated. AppBarLayout's elevation is now
556926cd28257536b1206934d69585a2dab100147d5Chris Banes     * controlled via a {@link android.animation.StateListAnimator}. This method now
557926cd28257536b1206934d69585a2dab100147d5Chris Banes     * always returns 0.
558631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes     */
5591a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes    @Deprecated
560631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes    public float getTargetElevation() {
5611a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        return 0;
56250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes    }
56350dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
564657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    int getPendingAction() {
5659995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes        return mPendingAction;
5669995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    }
5679995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes
568657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    void resetPendingAction() {
5699995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes        mPendingAction = PENDING_ACTION_NONE;
5709995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes    }
5719995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes
57213633353237d6506f4c436b459bc8be8c3d7ed68Chris Banes    @VisibleForTesting
57313633353237d6506f4c436b459bc8be8c3d7ed68Chris Banes    final int getTopInset() {
574ea004a01f44aaf36212120c936952c1742d03d30Chris Banes        return mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0;
575ea004a01f44aaf36212120c936952c1742d03d30Chris Banes    }
576ea004a01f44aaf36212120c936952c1742d03d30Chris Banes
577657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas    WindowInsetsCompat onWindowInsetChanged(final WindowInsetsCompat insets) {
5788818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes        WindowInsetsCompat newInsets = null;
5798818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes
5808818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes        if (ViewCompat.getFitsSystemWindows(this)) {
5818818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes            // If we're set to fit system windows, keep the insets
5828818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes            newInsets = insets;
5838818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes        }
5848818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes
5858818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes        // If our insets have changed, keep them and invalidate the scroll ranges...
58619a80415a88ecfae6bbc645cd14b653ed4337648Chris Banes        if (!objectEquals(mLastInsets, newInsets)) {
5878818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes            mLastInsets = newInsets;
5888818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes            invalidateScrollRanges();
5898818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes        }
5908818e8e12682e5df1535cd9b2d792494bd3e059dChris Banes
59117ed3263761329f6aa6796941358c41001fff325Chris Banes        return insets;
5926ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes    }
5936ba61c5c79fe025036593c9daf79cb65299bb0b6Chris Banes
594a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    public static class LayoutParams extends LinearLayout.LayoutParams {
59550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
59650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        /** @hide */
5978e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas        @RestrictTo(LIBRARY_GROUP)
59850dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        @IntDef(flag=true, value={
59950dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                SCROLL_FLAG_SCROLL,
60050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                SCROLL_FLAG_EXIT_UNTIL_COLLAPSED,
60150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                SCROLL_FLAG_ENTER_ALWAYS,
602bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED,
603bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                SCROLL_FLAG_SNAP
60450dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        })
60550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        @Retention(RetentionPolicy.SOURCE)
60650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        public @interface ScrollFlags {}
60750dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
608a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
609a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * The view will be scroll in direct relation to scroll events. This flag needs to be
610a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * set for any of the other flags to take effect. If any sibling views
611a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * before this one do not have this flag, then this value has no effect.
612a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
613a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public static final int SCROLL_FLAG_SCROLL = 0x1;
614a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
615a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
616a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * When exiting (scrolling off screen) the view will be scrolled until it is
617a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * 'collapsed'. The collapsed height is defined by the view's minimum height.
618a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
619a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @see ViewCompat#getMinimumHeight(View)
620a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @see View#setMinimumHeight(int)
621a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
622a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public static final int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED = 0x2;
623a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
624a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
625a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * When entering (scrolling on screen) the view will scroll on any downwards
626a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * scroll event, regardless of whether the scrolling view is also scrolling. This
627a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * is commonly referred to as the 'quick return' pattern.
628a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
629a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public static final int SCROLL_FLAG_ENTER_ALWAYS = 0x4;
630a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
631a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
632a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * An additional flag for 'enterAlways' which modifies the returning view to
633a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * only initially scroll back to it's collapsed height. Once the scrolling view has
634a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * reached the end of it's scroll range, the remainder of this view will be scrolled
635a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * into view. The collapsed height is defined by the view's minimum height.
636a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
637a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @see ViewCompat#getMinimumHeight(View)
638a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @see View#setMinimumHeight(int)
639a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
640a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public static final int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED = 0x8;
641a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
642a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
643bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes         * Upon a scroll ending, if the view is only partially visible then it will be snapped
644bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes         * and scrolled to it's closest edge. For example, if the view only has it's bottom 25%
645bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes         * displayed, it will be scrolled off screen completely. Conversely, if it's bottom 75%
646bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes         * is visible then it will be scrolled fully into view.
647bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes         */
648bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes        public static final int SCROLL_FLAG_SNAP = 0x10;
649bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes
650bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes        /**
651bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes         * Internal flags which allows quick checking features
652a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
653a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        static final int FLAG_QUICK_RETURN = SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS;
654bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes        static final int FLAG_SNAP = SCROLL_FLAG_SCROLL | SCROLL_FLAG_SNAP;
6551a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        static final int COLLAPSIBLE_FLAGS = SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
6561a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                | SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED;
657a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
658a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        int mScrollFlags = SCROLL_FLAG_SCROLL;
659a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        Interpolator mScrollInterpolator;
660a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
661a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public LayoutParams(Context c, AttributeSet attrs) {
662a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(c, attrs);
663c6cbbb50a06ec08e6888bd1c3dfd7fdf588d9db5Chris Banes            TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.AppBarLayout_Layout);
664c6cbbb50a06ec08e6888bd1c3dfd7fdf588d9db5Chris Banes            mScrollFlags = a.getInt(R.styleable.AppBarLayout_Layout_layout_scrollFlags, 0);
665c6cbbb50a06ec08e6888bd1c3dfd7fdf588d9db5Chris Banes            if (a.hasValue(R.styleable.AppBarLayout_Layout_layout_scrollInterpolator)) {
666a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                int resId = a.getResourceId(
667c6cbbb50a06ec08e6888bd1c3dfd7fdf588d9db5Chris Banes                        R.styleable.AppBarLayout_Layout_layout_scrollInterpolator, 0);
668a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                mScrollInterpolator = android.view.animation.AnimationUtils.loadInterpolator(
669a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                        c, resId);
670a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
671a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            a.recycle();
672a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
673a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
674a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public LayoutParams(int width, int height) {
675a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(width, height);
676a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
677a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
678a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public LayoutParams(int width, int height, float weight) {
679a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(width, height, weight);
680a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
681a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
682a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public LayoutParams(ViewGroup.LayoutParams p) {
683a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(p);
684a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
685a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
686a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public LayoutParams(MarginLayoutParams source) {
687a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(source);
688a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
689a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
6908f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viverette        @RequiresApi(19)
691a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public LayoutParams(LinearLayout.LayoutParams source) {
6928f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viverette            // The copy constructor called here only exists on API 19+.
693a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(source);
694a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
695a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
6968f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viverette        @RequiresApi(19)
697a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public LayoutParams(LayoutParams source) {
6988f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viverette            // The copy constructor called here only exists on API 19+.
699a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(source);
700a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            mScrollFlags = source.mScrollFlags;
701a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            mScrollInterpolator = source.mScrollInterpolator;
702a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
703a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
704a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
705a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * Set the scrolling flags.
706a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
707a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @param flags bitwise int of {@link #SCROLL_FLAG_SCROLL},
708bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes         *             {@link #SCROLL_FLAG_EXIT_UNTIL_COLLAPSED}, {@link #SCROLL_FLAG_ENTER_ALWAYS},
709bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes         *             {@link #SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED} and {@link #SCROLL_FLAG_SNAP }.
710a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
711a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @see #getScrollFlags()
712a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
71373e7a06fb66110495fa4e98a91831a47feab2526Chris Banes         * @attr ref android.support.design.R.styleable#AppBarLayout_Layout_layout_scrollFlags
714a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
71550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        public void setScrollFlags(@ScrollFlags int flags) {
716a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            mScrollFlags = flags;
717a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
718a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
719a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
720a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * Returns the scrolling flags.
721a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
722a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @see #setScrollFlags(int)
723a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
72473e7a06fb66110495fa4e98a91831a47feab2526Chris Banes         * @attr ref android.support.design.R.styleable#AppBarLayout_Layout_layout_scrollFlags
725a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
72650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        @ScrollFlags
727a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public int getScrollFlags() {
728a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return mScrollFlags;
729a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
730a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
731a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
732a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * Set the interpolator to when scrolling the view associated with this
733a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * {@link LayoutParams}.
734a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
735a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @param interpolator the interpolator to use, or null to use normal 1-to-1 scrolling.
736a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
73773e7a06fb66110495fa4e98a91831a47feab2526Chris Banes         * @attr ref android.support.design.R.styleable#AppBarLayout_Layout_layout_scrollInterpolator
738a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @see #getScrollInterpolator()
739a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
740a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public void setScrollInterpolator(Interpolator interpolator) {
741a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            mScrollInterpolator = interpolator;
742a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
743a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
744a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        /**
745a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * Returns the {@link Interpolator} being used for scrolling the view associated with this
746a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * {@link LayoutParams}. Null indicates 'normal' 1-to-1 scrolling.
747a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         *
74873e7a06fb66110495fa4e98a91831a47feab2526Chris Banes         * @attr ref android.support.design.R.styleable#AppBarLayout_Layout_layout_scrollInterpolator
749a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         * @see #setScrollInterpolator(Interpolator)
750a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes         */
751a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public Interpolator getScrollInterpolator() {
752a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return mScrollInterpolator;
753a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
7541a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
7551a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        /**
7561a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes         * Returns true if the scroll flags are compatible for 'collapsing'
7571a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes         */
758657ea1100fee4750f148f9d0dcb7e7e2028f105eAurimas Liutikas        boolean isCollapsible() {
7591a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            return (mScrollFlags & SCROLL_FLAG_SCROLL) == SCROLL_FLAG_SCROLL
7601a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    && (mScrollFlags & COLLAPSIBLE_FLAGS) != 0;
7611a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
762a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
763a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
764a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    /**
765a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     * The default {@link Behavior} for {@link AppBarLayout}. Implements the necessary nested
766a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     * scroll handling with offsetting.
767a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     */
768ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor    public static class Behavior extends HeaderBehavior<AppBarLayout> {
76957aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes        private static final int MAX_OFFSET_ANIMATION_DURATION = 600; // ms
770e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        private static final int INVALID_POSITION = -1;
771e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
77272a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        /**
77372a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes         * Callback to allow control over any {@link AppBarLayout} dragging.
77472a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes         */
77572a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        public static abstract class DragCallback {
77672a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            /**
77772a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes             * Allows control over whether the given {@link AppBarLayout} can be dragged or not.
77872a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes             *
77972a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes             * <p>Dragging is defined as a direct touch on the AppBarLayout with movement. This
78072a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes             * call does not affect any nested scrolling.</p>
78172a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes             *
78272a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes             * @return true if we are in a position to scroll the AppBarLayout via a drag, false
78372a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes             *         if not.
78472a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes             */
78572a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            public abstract boolean canDrag(@NonNull AppBarLayout appBarLayout);
78672a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        }
78772a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes
788e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        private int mOffsetDelta;
789a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
79050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        private boolean mSkipNestedPreScroll;
79147543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes        private boolean mWasNestedFlung;
792631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
793ae7189188acb2bc972b08d9fbd59939f341c7829Aurimas Liutikas        private ValueAnimator mOffsetAnimator;
79450dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
795e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        private int mOffsetToChildIndexOnLayout = INVALID_POSITION;
796e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        private boolean mOffsetToChildIndexOnLayoutIsMinHeight;
797e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        private float mOffsetToChildIndexOnLayoutPerc;
798e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
799d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes        private WeakReference<View> mLastNestedScrollingChildRef;
80072a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        private DragCallback mOnDragCallback;
801d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes
802a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public Behavior() {}
803a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
804a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public Behavior(Context context, AttributeSet attrs) {
805a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(context, attrs);
806a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
807a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
808a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        @Override
809ab809dd75ef2f73b312038f2c10473cfa5885a58Chris Banes        public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child,
810a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                View directTargetChild, View target, int nestedScrollAxes) {
811f57cd2fd5059c43d77bdfec57edfd5a217533103Chris Banes            // Return true if we're nested scrolling vertically, and we have scrollable children
812f57cd2fd5059c43d77bdfec57edfd5a217533103Chris Banes            // and the scrolling view is big enough to scroll
813631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes            final boolean started = (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0
814f57cd2fd5059c43d77bdfec57edfd5a217533103Chris Banes                    && child.hasScrollableChildren()
815ab809dd75ef2f73b312038f2c10473cfa5885a58Chris Banes                    && parent.getHeight() - directTargetChild.getHeight() <= child.getHeight();
816631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
817c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov            if (started && mOffsetAnimator != null) {
818631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                // Cancel any offset animation
819c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov                mOffsetAnimator.cancel();
820631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes            }
821631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
822d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes            // A new nested scroll has started so clear out the previous ref
823d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes            mLastNestedScrollingChildRef = null;
824d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes
825631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes            return started;
826a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
827a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
828a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        @Override
829a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
830a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                View target, int dx, int dy, int[] consumed) {
83150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes            if (dy != 0 && !mSkipNestedPreScroll) {
83250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                int min, max;
83350dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                if (dy < 0) {
83450dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    // We're scrolling down
83550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    min = -child.getTotalScrollRange();
83650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    max = min + child.getDownNestedPreScrollRange();
83750dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                } else {
83850dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    // We're scrolling up
83950dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    min = -child.getUpNestedPreScrollRange();
84050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    max = 0;
84150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                }
84250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                consumed[1] = scroll(coordinatorLayout, child, dy, min, max);
843a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
844a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
845a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
846a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        @Override
847a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
848a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                View target, int dxConsumed, int dyConsumed,
849a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                int dxUnconsumed, int dyUnconsumed) {
85050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes            if (dyUnconsumed < 0) {
85150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                // If the scrolling view is scrolling down but not consuming, it's probably be at
85250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                // the top of it's content
85350dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                scroll(coordinatorLayout, child, dyUnconsumed,
85450dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                        -child.getDownNestedScrollRange(), 0);
85550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                // Set the expanding flag so that onNestedPreScroll doesn't handle any events
85650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                mSkipNestedPreScroll = true;
85750dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes            } else {
85850dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                // As we're no longer handling nested scrolls, reset the skip flag
85950dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                mSkipNestedPreScroll = false;
860a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
86150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        }
862a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
86350dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes        @Override
864bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes        public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl,
86550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                View target) {
86647543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes            if (!mWasNestedFlung) {
867bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                // If we haven't been flung then let's see if the current view has been set to snap
868bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                snapToChildIfNeeded(coordinatorLayout, abl);
869bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes            }
870bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes
871bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes            // Reset the flags
87250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes            mSkipNestedPreScroll = false;
87347543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes            mWasNestedFlung = false;
874d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes            // Keep a reference to the previous nested scrolling child
875d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes            mLastNestedScrollingChildRef = new WeakReference<>(target);
876d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes        }
877d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes
878d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes        @Override
879631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes        public boolean onNestedFling(final CoordinatorLayout coordinatorLayout,
880631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                final AppBarLayout child, View target, float velocityX, float velocityY,
881631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                boolean consumed) {
882bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes            boolean flung = false;
883bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes
884631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes            if (!consumed) {
885631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                // It has been consumed so let's fling ourselves
886bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                flung = fling(coordinatorLayout, child, -child.getTotalScrollRange(),
887bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                        0, -velocityY);
888631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes            } else {
889631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                // If we're scrolling up and the child also consumed the fling. We'll fake scroll
890c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                // up to our 'collapsed' offset
891631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                if (velocityY < 0) {
892631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                    // We're scrolling down
893bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                    final int targetScroll = -child.getTotalScrollRange()
894631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                            + child.getDownNestedPreScrollRange();
895bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                    if (getTopBottomOffsetForScrollingSibling() < targetScroll) {
896bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                        // If we're currently not expanded more than the target scroll, we'll
897bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                        // animate a fling
89857aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                        animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY);
899bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                        flung = true;
900631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                    }
901631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                } else {
902631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                    // We're scrolling up
903bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                    final int targetScroll = -child.getUpNestedPreScrollRange();
904bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                    if (getTopBottomOffsetForScrollingSibling() > targetScroll) {
905bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                        // If we're currently not expanded less than the target scroll, we'll
906bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                        // animate a fling
90757aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                        animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY);
908bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                        flung = true;
909631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                    }
910631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                }
911631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes            }
912631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
91347543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes            mWasNestedFlung = flung;
914bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes            return flung;
915631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes        }
916631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
91772a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        /**
91872a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes         * Set a callback to control any {@link AppBarLayout} dragging.
91972a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes         *
92072a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes         * @param callback the callback to use, or {@code null} to use the default behavior.
92172a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes         */
92272a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        public void setDragCallback(@Nullable DragCallback callback) {
92372a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            mOnDragCallback = callback;
92472a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        }
92572a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes
926631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes        private void animateOffsetTo(final CoordinatorLayout coordinatorLayout,
92757aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                final AppBarLayout child, final int offset, float velocity) {
92857aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes            final int distance = Math.abs(getTopBottomOffsetForScrollingSibling() - offset);
92957aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes
93057aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes            final int duration;
93157aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes            velocity = Math.abs(velocity);
93257aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes            if (velocity > 0) {
93357aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                duration = 3 * Math.round(1000 * (distance / velocity));
93457aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes            } else {
93557aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                final float distanceRatio = (float) distance / child.getHeight();
93657aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                duration = (int) ((distanceRatio + 1) * 150);
93757aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes            }
93857aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes
93957aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes            animateOffsetWithDuration(coordinatorLayout, child, offset, duration);
94057aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes        }
94157aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes
94257aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes        private void animateOffsetWithDuration(final CoordinatorLayout coordinatorLayout,
94357aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                final AppBarLayout child, final int offset, final int duration) {
944dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes            final int currentOffset = getTopBottomOffsetForScrollingSibling();
945dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes            if (currentOffset == offset) {
946c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov                if (mOffsetAnimator != null && mOffsetAnimator.isRunning()) {
947c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov                    mOffsetAnimator.cancel();
948dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes                }
949dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes                return;
950dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes            }
951dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes
952c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov            if (mOffsetAnimator == null) {
953ae7189188acb2bc972b08d9fbd59939f341c7829Aurimas Liutikas                mOffsetAnimator = new ValueAnimator();
954c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov                mOffsetAnimator.setInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);
955ae7189188acb2bc972b08d9fbd59939f341c7829Aurimas Liutikas                mOffsetAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
956631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                    @Override
957ae7189188acb2bc972b08d9fbd59939f341c7829Aurimas Liutikas                    public void onAnimationUpdate(ValueAnimator animation) {
958ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor                        setHeaderTopBottomOffset(coordinatorLayout, child,
959ae7189188acb2bc972b08d9fbd59939f341c7829Aurimas Liutikas                                (int) animation.getAnimatedValue());
960631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                    }
961631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                });
962631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes            } else {
963c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov                mOffsetAnimator.cancel();
964631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes            }
965631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
96657aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes            mOffsetAnimator.setDuration(Math.min(duration, MAX_OFFSET_ANIMATION_DURATION));
967c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov            mOffsetAnimator.setIntValues(currentOffset, offset);
968c530cc115fddab8e1d9645b322424dd45f9ecd0dKirill Grouchnikov            mOffsetAnimator.start();
969631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes        }
970631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
971108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes        private int getChildIndexOnOffset(AppBarLayout abl, final int offset) {
972bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes            for (int i = 0, count = abl.getChildCount(); i < count; i++) {
973bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                View child = abl.getChildAt(i);
974bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                if (child.getTop() <= -offset && child.getBottom() >= -offset) {
975108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                    return i;
976bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                }
977bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes            }
978108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes            return -1;
979bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes        }
980bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes
981bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes        private void snapToChildIfNeeded(CoordinatorLayout coordinatorLayout, AppBarLayout abl) {
982bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes            final int offset = getTopBottomOffsetForScrollingSibling();
983108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes            final int offsetChildIndex = getChildIndexOnOffset(abl, offset);
984108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes            if (offsetChildIndex >= 0) {
985108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                final View offsetChild = abl.getChildAt(offsetChildIndex);
986bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                final LayoutParams lp = (LayoutParams) offsetChild.getLayoutParams();
987108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                final int flags = lp.getScrollFlags();
988108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes
989108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                if ((flags & LayoutParams.FLAG_SNAP) == LayoutParams.FLAG_SNAP) {
990bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                    // We're set the snap, so animate the offset to the nearest edge
991108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                    int snapTop = -offsetChild.getTop();
992108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                    int snapBottom = -offsetChild.getBottom();
993108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes
994108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                    if (offsetChildIndex == abl.getChildCount() - 1) {
995108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        // If this is the last child, we need to take the top inset into account
996108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        snapBottom += abl.getTopInset();
997108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                    }
998dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes
999108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                    if (checkFlag(flags, LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED)) {
1000108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        // If the view is set only exit until it is collapsed, we'll abide by that
1001108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        snapBottom += ViewCompat.getMinimumHeight(offsetChild);
1002108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                    } else if (checkFlag(flags, LayoutParams.FLAG_QUICK_RETURN
1003108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                            | LayoutParams.SCROLL_FLAG_ENTER_ALWAYS)) {
1004108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        // If it's set to always enter collapsed, it actually has two states. We
1005108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        // select the state and then snap within the state
1006108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        final int seam = snapBottom + ViewCompat.getMinimumHeight(offsetChild);
1007108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        if (offset < seam) {
1008108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                            snapTop = seam;
1009108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        } else {
1010108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                            snapBottom = seam;
1011108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                        }
1012dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes                    }
1013dcde7bf0c79a12f30d3853a82f4fbf54ce5161c8Chris Banes
1014108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                    final int newOffset = offset < (snapBottom + snapTop) / 2
1015108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                            ? snapBottom
1016108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes                            : snapTop;
1017bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                    animateOffsetTo(coordinatorLayout, abl,
101857aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                            MathUtils.constrain(newOffset, -abl.getTotalScrollRange(), 0), 0);
1019bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes                }
1020bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes            }
1021bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes        }
1022bc22c4fd37e99643b3e7b87e2849a9e63fb8c5fcChris Banes
1023108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes        private static boolean checkFlag(final int flags, final int check) {
1024108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes            return (flags & check) == check;
1025108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes        }
1026108a7a812404313b7b9f8daa154e657e49e0aae6Chris Banes
1027631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes        @Override
1028bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes        public boolean onMeasureChild(CoordinatorLayout parent, AppBarLayout child,
1029bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec,
1030bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                int heightUsed) {
1031bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes            final CoordinatorLayout.LayoutParams lp =
1032bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                    (CoordinatorLayout.LayoutParams) child.getLayoutParams();
1033bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes            if (lp.height == CoordinatorLayout.LayoutParams.WRAP_CONTENT) {
1034bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                // If the view is set to wrap on it's height, CoordinatorLayout by default will
1035bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                // cap the view at the CoL's height. Since the AppBarLayout can scroll, this isn't
1036bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                // what we actually want, so we measure it ourselves with an unspecified spec to
1037bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                // allow the child to be larger than it's parent
1038bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                parent.onMeasureChild(child, parentWidthMeasureSpec, widthUsed,
1039bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), heightUsed);
1040bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                return true;
1041bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes            }
1042bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes
1043bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes            // Let the parent handle it as normal
1044bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes            return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed,
1045bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes                    parentHeightMeasureSpec, heightUsed);
1046bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes        }
1047bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes
1048bca7a517ce4fb75e2571ce2d9d0263fea8e15dbeChris Banes        @Override
10499995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes        public boolean onLayoutChild(CoordinatorLayout parent, AppBarLayout abl,
105050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                int layoutDirection) {
10519995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes            boolean handled = super.onLayoutChild(parent, abl, layoutDirection);
10529995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes
10532bab57ed97f284a8152d180cca796739afa3a71dChris Banes            // The priority for for actions here is (first which is true wins):
10542bab57ed97f284a8152d180cca796739afa3a71dChris Banes            // 1. forced pending actions
10552bab57ed97f284a8152d180cca796739afa3a71dChris Banes            // 2. offsets for restorations
10562bab57ed97f284a8152d180cca796739afa3a71dChris Banes            // 3. non-forced pending actions
10579995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes            final int pendingAction = abl.getPendingAction();
10582bab57ed97f284a8152d180cca796739afa3a71dChris Banes            if (mOffsetToChildIndexOnLayout >= 0 && (pendingAction & PENDING_ACTION_FORCE) == 0) {
10592bab57ed97f284a8152d180cca796739afa3a71dChris Banes                View child = abl.getChildAt(mOffsetToChildIndexOnLayout);
10602bab57ed97f284a8152d180cca796739afa3a71dChris Banes                int offset = -child.getBottom();
10612bab57ed97f284a8152d180cca796739afa3a71dChris Banes                if (mOffsetToChildIndexOnLayoutIsMinHeight) {
10622bab57ed97f284a8152d180cca796739afa3a71dChris Banes                    offset += ViewCompat.getMinimumHeight(child) + abl.getTopInset();
10632bab57ed97f284a8152d180cca796739afa3a71dChris Banes                } else {
10642bab57ed97f284a8152d180cca796739afa3a71dChris Banes                    offset += Math.round(child.getHeight() * mOffsetToChildIndexOnLayoutPerc);
10652bab57ed97f284a8152d180cca796739afa3a71dChris Banes                }
10662bab57ed97f284a8152d180cca796739afa3a71dChris Banes                setHeaderTopBottomOffset(parent, abl, offset);
10672bab57ed97f284a8152d180cca796739afa3a71dChris Banes            } else if (pendingAction != PENDING_ACTION_NONE) {
10689995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                final boolean animate = (pendingAction & PENDING_ACTION_ANIMATE_ENABLED) != 0;
10699995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                if ((pendingAction & PENDING_ACTION_COLLAPSED) != 0) {
10709995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                    final int offset = -abl.getUpNestedPreScrollRange();
10719995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                    if (animate) {
107257aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                        animateOffsetTo(parent, abl, offset, 0);
10739995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                    } else {
1074ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor                        setHeaderTopBottomOffset(parent, abl, offset);
10759995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                    }
10769995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                } else if ((pendingAction & PENDING_ACTION_EXPANDED) != 0) {
10779995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                    if (animate) {
107857aa00c058f3278fccc7a23f3cef29142bb14dbbChris Banes                        animateOffsetTo(parent, abl, 0, 0);
10799995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                    } else {
1080ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor                        setHeaderTopBottomOffset(parent, abl, 0);
10819995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                    }
10829995f7bdb02dbe16155661545fd64046ad3d56c2Chris Banes                }
1083e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            }
108450dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
108579aa418487cdefb85d3c1fac32d6403a1ac6057dChris Banes            // Finally reset any pending states
108679aa418487cdefb85d3c1fac32d6403a1ac6057dChris Banes            abl.resetPendingAction();
108779aa418487cdefb85d3c1fac32d6403a1ac6057dChris Banes            mOffsetToChildIndexOnLayout = INVALID_POSITION;
108879aa418487cdefb85d3c1fac32d6403a1ac6057dChris Banes
108947543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes            // We may have changed size, so let's constrain the top and bottom offset correctly,
109047543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes            // just in case we're out of the bounds
109147543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes            setTopAndBottomOffset(
109247543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes                    MathUtils.constrain(getTopAndBottomOffset(), -abl.getTotalScrollRange(), 0));
109347543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes
1094df2d45b76203a9c4101629005eb27059d7f80297Chris Banes            // Update the AppBarLayout's drawable state for any elevation changes.
1095df2d45b76203a9c4101629005eb27059d7f80297Chris Banes            // This is needed so that the elevation is set in the first layout, so that
1096df2d45b76203a9c4101629005eb27059d7f80297Chris Banes            // we don't get a visual elevation jump pre-N (due to the draw dispatch skip)
1097df2d45b76203a9c4101629005eb27059d7f80297Chris Banes            updateAppBarLayoutDrawableState(parent, abl, getTopAndBottomOffset(), 0, true);
1098df2d45b76203a9c4101629005eb27059d7f80297Chris Banes
10991a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            // Make sure we dispatch the offset update
11001a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            abl.dispatchOffsetUpdates(getTopAndBottomOffset());
110150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
110250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes            return handled;
1103a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1104a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
110572a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        @Override
110672a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        boolean canDragView(AppBarLayout view) {
110772a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            if (mOnDragCallback != null) {
110872a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes                // If there is a drag callback set, it's in control
110972a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes                return mOnDragCallback.canDrag(view);
111072a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            }
111172a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes
111272a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            // Else we'll use the default behaviour of seeing if it can scroll down
1113d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes            if (mLastNestedScrollingChildRef != null) {
111472a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes                // If we have a reference to a scrolling view, check it
111572a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes                final View scrollingView = mLastNestedScrollingChildRef.get();
111672a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes                return scrollingView != null && scrollingView.isShown()
111772a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes                        && !ViewCompat.canScrollVertically(scrollingView, -1);
111872a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            } else {
111972a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes                // Otherwise we assume that the scrolling view hasn't been scrolled and can drag.
112072a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes                return true;
1121d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes            }
1122d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes        }
1123d4c72f08279df548fbc4d556fe1a8b3500fafdbcChris Banes
1124ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor        @Override
112547543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes        void onFlingFinished(CoordinatorLayout parent, AppBarLayout layout) {
112647543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes            // At the end of a manual fling, check to see if we need to snap to the edge-child
112747543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes            snapToChildIfNeeded(parent, layout);
112847543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes        }
112947543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes
113047543aa43aacb8defbbf90682fcb2b63ce1b00b5Chris Banes        @Override
113172a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        int getMaxDragOffset(AppBarLayout view) {
113272a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            return -view.getDownNestedScrollRange();
113372a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        }
113472a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes
113572a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        @Override
113672a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        int getScrollRangeForDragFling(AppBarLayout view) {
113772a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes            return view.getTotalScrollRange();
113872a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        }
113972a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes
114072a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        @Override
114172a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        int setHeaderTopBottomOffset(CoordinatorLayout coordinatorLayout,
1142bb3740d273d4b5108ab63f6d264466250a197e5bChris Banes                AppBarLayout appBarLayout, int newOffset, int minOffset, int maxOffset) {
1143e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            final int curOffset = getTopBottomOffsetForScrollingSibling();
1144a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            int consumed = 0;
1145a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
11465e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes            if (minOffset != 0 && curOffset >= minOffset && curOffset <= maxOffset) {
1147631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                // If we have some scrolling range, and we're currently within the min and max
1148631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                // offsets, calculate a new offset
114950dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                newOffset = MathUtils.constrain(newOffset, minOffset, maxOffset);
1150a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                if (curOffset != newOffset) {
1151e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                    final int interpolatedOffset = appBarLayout.hasChildWithInterpolator()
1152e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                            ? interpolateOffset(appBarLayout, newOffset)
1153e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                            : newOffset;
1154e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
11551a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    final boolean offsetChanged = setTopAndBottomOffset(interpolatedOffset);
1156631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes
1157a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    // Update how much dy we have consumed
1158a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    consumed = curOffset - newOffset;
1159a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    // Update the stored sibling offset
1160e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                    mOffsetDelta = newOffset - interpolatedOffset;
1161a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1162a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    if (!offsetChanged && appBarLayout.hasChildWithInterpolator()) {
1163a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                        // If the offset hasn't changed and we're using an interpolated scroll
1164a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                        // then we need to keep any dependent views updated. CoL will do this for
1165a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                        // us when we move, but we need to do it manually when we don't (as an
1166a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                        // interpolated scroll may finish early).
1167a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                        coordinatorLayout.dispatchDependentViewsChanged(appBarLayout);
1168a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    }
1169a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1170631f64ec9c6255f38a7f746d7949b6a537c1180fChris Banes                    // Dispatch the updates to any listeners
11711a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    appBarLayout.dispatchOffsetUpdates(getTopAndBottomOffset());
11721a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
11731a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    // Update the AppBarLayout's drawable state (for any elevation changes)
1174c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                    updateAppBarLayoutDrawableState(coordinatorLayout, appBarLayout, newOffset,
1175df2d45b76203a9c4101629005eb27059d7f80297Chris Banes                            newOffset < curOffset ? -1 : 1, false);
1176a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                }
11775e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes            } else {
11785e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes                // Reset the offset delta
11795e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes                mOffsetDelta = 0;
1180a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
1181a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1182a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return consumed;
1183a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1184a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1185bb3740d273d4b5108ab63f6d264466250a197e5bChris Banes        @VisibleForTesting
1186bb3740d273d4b5108ab63f6d264466250a197e5bChris Banes        boolean isOffsetAnimatorRunning() {
1187bb3740d273d4b5108ab63f6d264466250a197e5bChris Banes            return mOffsetAnimator != null && mOffsetAnimator.isRunning();
1188bb3740d273d4b5108ab63f6d264466250a197e5bChris Banes        }
1189bb3740d273d4b5108ab63f6d264466250a197e5bChris Banes
1190a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        private int interpolateOffset(AppBarLayout layout, final int offset) {
1191a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final int absOffset = Math.abs(offset);
1192a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1193a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            for (int i = 0, z = layout.getChildCount(); i < z; i++) {
1194a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                final View child = layout.getChildAt(i);
1195a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                final AppBarLayout.LayoutParams childLp = (LayoutParams) child.getLayoutParams();
119650dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                final Interpolator interpolator = childLp.getScrollInterpolator();
1197a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
119850dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                if (absOffset >= child.getTop() && absOffset <= child.getBottom()) {
1199a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    if (interpolator != null) {
120050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                        int childScrollableHeight = 0;
120150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                        final int flags = childLp.getScrollFlags();
120250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                        if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) {
1203b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes                            // We're set to scroll so add the child's height plus margin
1204b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes                            childScrollableHeight += child.getHeight() + childLp.topMargin
1205b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes                                    + childLp.bottomMargin;
1206b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes
120750dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                            if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
1208b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes                                // For a collapsing scroll, we to take the collapsed height
1209b263b91ba75dc1a7855186e8b0e9cf75d46a6b8dChris Banes                                // into account.
121050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                                childScrollableHeight -= ViewCompat.getMinimumHeight(child);
121150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                            }
121250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                        }
121350dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
1214ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                        if (ViewCompat.getFitsSystemWindows(child)) {
1215ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                            childScrollableHeight -= layout.getTopInset();
1216ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                        }
1217ea004a01f44aaf36212120c936952c1742d03d30Chris Banes
121850dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                        if (childScrollableHeight > 0) {
121950dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                            final int offsetForView = absOffset - child.getTop();
122050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                            final int interpolatedDiff = Math.round(childScrollableHeight *
122150dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                                    interpolator.getInterpolation(
122250dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                                            offsetForView / (float) childScrollableHeight));
122350dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
122450dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                            return Integer.signum(offset) * (child.getTop() + interpolatedDiff);
122550dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                        }
1226a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    }
122750dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes
122850dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    // If we get to here then the view on the offset isn't suitable for interpolated
122950dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    // scrolling. So break out of the loop
123050dfc33a565c4aefe6d5e844c93aa24a74cb80b3Chris Banes                    break;
1231a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                }
1232a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
1233a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1234a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return offset;
1235a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1236a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1237c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes        private void updateAppBarLayoutDrawableState(final CoordinatorLayout parent,
1238df2d45b76203a9c4101629005eb27059d7f80297Chris Banes                final AppBarLayout layout, final int offset, final int direction,
1239df2d45b76203a9c4101629005eb27059d7f80297Chris Banes                final boolean forceJump) {
12401a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            final View child = getAppBarChildOnOffset(layout, offset);
12411a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            if (child != null) {
12421a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                final AppBarLayout.LayoutParams childLp = (LayoutParams) child.getLayoutParams();
12431a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                final int flags = childLp.getScrollFlags();
12441a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                boolean collapsed = false;
12451a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
12461a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) {
12471a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    final int minHeight = ViewCompat.getMinimumHeight(child);
12481a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
12491a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    if (direction > 0 && (flags & (LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
12501a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                            | LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED)) != 0) {
12511a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                        // We're set to enter always collapsed so we are only collapsed when
12521a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                        // being scrolled down, and in a collapsed offset
12531a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                        collapsed = -offset >= child.getBottom() - minHeight - layout.getTopInset();
12541a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    } else if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
12551a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                        // We're set to exit until collapsed, so any offset which results in
12561a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                        // the minimum height (or less) being shown is collapsed
12571a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                        collapsed = -offset >= child.getBottom() - minHeight - layout.getTopInset();
12581a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    }
12591a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                }
12601a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
1261c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                final boolean changed = layout.setCollapsedState(collapsed);
1262c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes
1263df2d45b76203a9c4101629005eb27059d7f80297Chris Banes                if (Build.VERSION.SDK_INT >= 11 && (forceJump
1264df2d45b76203a9c4101629005eb27059d7f80297Chris Banes                        || (changed && shouldJumpElevationState(parent, layout)))) {
1265c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                    // If the collapsed state changed, we may need to
1266c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                    // jump to the current state if we have an overlapping view
1267c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                    layout.jumpDrawablesToCurrentState();
1268c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                }
1269c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes            }
1270c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes        }
1271c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes
1272c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes        private boolean shouldJumpElevationState(CoordinatorLayout parent, AppBarLayout layout) {
1273c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes            // We should jump the elevated state if we have a dependent scrolling view which has
1274c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes            // an overlapping top (i.e. overlaps us)
1275c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes            final List<View> dependencies = parent.getDependents(layout);
1276c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes            for (int i = 0, size = dependencies.size(); i < size; i++) {
1277c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                final View dependency = dependencies.get(i);
1278c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                final CoordinatorLayout.LayoutParams lp =
1279c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                        (CoordinatorLayout.LayoutParams) dependency.getLayoutParams();
1280c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                final CoordinatorLayout.Behavior behavior = lp.getBehavior();
1281c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes
1282c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                if (behavior instanceof ScrollingViewBehavior) {
1283c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                    return ((ScrollingViewBehavior) behavior).getOverlayTop() != 0;
1284c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes                }
12851a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            }
1286c07c8e1e74a026c0d9e3a9dd119913436380be38Chris Banes            return false;
12871a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
12881a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
12891a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        private static View getAppBarChildOnOffset(final AppBarLayout layout, final int offset) {
12901a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            final int absOffset = Math.abs(offset);
12911a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            for (int i = 0, z = layout.getChildCount(); i < z; i++) {
12921a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                final View child = layout.getChildAt(i);
12931a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                if (absOffset >= child.getTop() && absOffset <= child.getBottom()) {
12941a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                    return child;
12951a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes                }
12961a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            }
12971a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes            return null;
12981a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes        }
12991a8e4886abbc35e479cddb7b4acd601cede2fd8aChris Banes
1300ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor        @Override
130172a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        int getTopBottomOffsetForScrollingSibling() {
1302e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            return getTopAndBottomOffset() + mOffsetDelta;
1303e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        }
1304e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
1305e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        @Override
1306824644fc70d5fd36bcd4c1e155c1279aebcac007Chris Banes        public Parcelable onSaveInstanceState(CoordinatorLayout parent, AppBarLayout abl) {
1307824644fc70d5fd36bcd4c1e155c1279aebcac007Chris Banes            final Parcelable superState = super.onSaveInstanceState(parent, abl);
1308e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            final int offset = getTopAndBottomOffset();
1309e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
1310e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            // Try and find the first visible child...
1311824644fc70d5fd36bcd4c1e155c1279aebcac007Chris Banes            for (int i = 0, count = abl.getChildCount(); i < count; i++) {
1312824644fc70d5fd36bcd4c1e155c1279aebcac007Chris Banes                View child = abl.getChildAt(i);
1313e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                final int visBottom = child.getBottom() + offset;
1314e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
1315e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                if (child.getTop() + offset <= 0 && visBottom >= 0) {
1316e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                    final SavedState ss = new SavedState(superState);
1317e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                    ss.firstVisibleChildIndex = i;
1318c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                    ss.firstVisibleChildAtMinimumHeight =
1319824644fc70d5fd36bcd4c1e155c1279aebcac007Chris Banes                            visBottom == (ViewCompat.getMinimumHeight(child) + abl.getTopInset());
1320c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                    ss.firstVisibleChildPercentageShown = visBottom / (float) child.getHeight();
1321e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                    return ss;
1322e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                }
1323e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            }
1324e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
1325e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            // Else we'll just return the super state
1326e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            return superState;
1327e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        }
1328e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
1329e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        @Override
1330e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        public void onRestoreInstanceState(CoordinatorLayout parent, AppBarLayout appBarLayout,
1331e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                Parcelable state) {
1332e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            if (state instanceof SavedState) {
1333e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                final SavedState ss = (SavedState) state;
1334e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                super.onRestoreInstanceState(parent, appBarLayout, ss.getSuperState());
1335e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                mOffsetToChildIndexOnLayout = ss.firstVisibleChildIndex;
1336c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                mOffsetToChildIndexOnLayoutPerc = ss.firstVisibleChildPercentageShown;
1337c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                mOffsetToChildIndexOnLayoutIsMinHeight = ss.firstVisibleChildAtMinimumHeight;
1338e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            } else {
1339e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                super.onRestoreInstanceState(parent, appBarLayout, state);
1340e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                mOffsetToChildIndexOnLayout = INVALID_POSITION;
1341e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            }
1342e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes        }
1343e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
134405f5ba020fa6caa658c75b6d77436aa980ca0fccChris Banes        protected static class SavedState extends AbsSavedState {
1345e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            int firstVisibleChildIndex;
1346c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas            float firstVisibleChildPercentageShown;
1347c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas            boolean firstVisibleChildAtMinimumHeight;
1348e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
134962ab25d71ccfa775a8bd2add7b41277bc4e14bbcChris Banes            public SavedState(Parcel source, ClassLoader loader) {
135005f5ba020fa6caa658c75b6d77436aa980ca0fccChris Banes                super(source, loader);
1351e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                firstVisibleChildIndex = source.readInt();
1352c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                firstVisibleChildPercentageShown = source.readFloat();
1353c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                firstVisibleChildAtMinimumHeight = source.readByte() != 0;
1354e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            }
1355e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
1356e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            public SavedState(Parcelable superState) {
1357e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                super(superState);
1358e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            }
1359e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
1360e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            @Override
1361e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            public void writeToParcel(Parcel dest, int flags) {
1362e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                super.writeToParcel(dest, flags);
1363e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes                dest.writeInt(firstVisibleChildIndex);
1364c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                dest.writeFloat(firstVisibleChildPercentageShown);
1365c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas                dest.writeByte((byte) (firstVisibleChildAtMinimumHeight ? 1 : 0));
1366e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes            }
1367e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
13684b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas            public static final Creator<SavedState> CREATOR = new ClassLoaderCreator<SavedState>() {
13694b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                @Override
13704b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                public SavedState createFromParcel(Parcel source, ClassLoader loader) {
13714b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                    return new SavedState(source, loader);
13724b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                }
1373e52e16988e2500e20052ddd70920f950f4eb4f79Chris Banes
13744b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                @Override
13754b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                public SavedState createFromParcel(Parcel source) {
13764b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                    return new SavedState(source, null);
13774b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                }
13784b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas
13794b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                @Override
13804b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                public SavedState[] newArray(int size) {
13814b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                    return new SavedState[size];
13824b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas                }
13834b25d63ff53d4676150ac54897851e4ff9f1049fAurimas Liutikas            };
1384a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1385a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
1386a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1387a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    /**
1388a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     * Behavior which should be used by {@link View}s which can scroll vertically and support
1389a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     * nested scrolling to automatically scroll any {@link AppBarLayout} siblings.
1390a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes     */
1391ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor    public static class ScrollingViewBehavior extends HeaderScrollingViewBehavior {
1392a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1393a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public ScrollingViewBehavior() {}
1394a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1395a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public ScrollingViewBehavior(Context context, AttributeSet attrs) {
1396a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            super(context, attrs);
1397a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1398b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes            final TypedArray a = context.obtainStyledAttributes(attrs,
139973e7a06fb66110495fa4e98a91831a47feab2526Chris Banes                    R.styleable.ScrollingViewBehavior_Layout);
1400b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes            setOverlayTop(a.getDimensionPixelSize(
140173e7a06fb66110495fa4e98a91831a47feab2526Chris Banes                    R.styleable.ScrollingViewBehavior_Layout_behavior_overlapTop, 0));
1402a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            a.recycle();
1403a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1404a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1405a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        @Override
1406a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
1407a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            // We depend on any AppBarLayouts
1408a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return dependency instanceof AppBarLayout;
1409a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1410a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1411a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        @Override
1412a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
1413a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                View dependency) {
14145e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes            offsetChildAsNeeded(parent, child, dependency);
14150d2f46331a9fc3b21597e926ba2e061af7c00708Chris Banes            return false;
14160d2f46331a9fc3b21597e926ba2e061af7c00708Chris Banes        }
14170d2f46331a9fc3b21597e926ba2e061af7c00708Chris Banes
141800a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes        @Override
14194cf1d92509224fab3ca69b419a005c536ab75c5cChris Banes        public boolean onRequestChildRectangleOnScreen(CoordinatorLayout parent, View child,
142000a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                Rect rectangle, boolean immediate) {
142100a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes            final AppBarLayout header = findFirstDependency(parent.getDependencies(child));
142200a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes            if (header != null) {
142300a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                // Offset the rect by the child's left/top
142400a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                rectangle.offset(child.getLeft(), child.getTop());
142500a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes
142600a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                final Rect parentRect = mTempRect1;
142700a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                parentRect.set(0, 0, parent.getWidth(), parent.getHeight());
142800a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes
142900a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                if (!parentRect.contains(rectangle)) {
143000a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                    // If the rectangle can not be fully seen the visible bounds, collapse
143100a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                    // the AppBarLayout
143200a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                    header.setExpanded(false, !immediate);
143300a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                    return true;
143400a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                }
143500a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes            }
143600a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes            return false;
143700a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes        }
143800a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes
14395e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes        private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) {
1440a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            final CoordinatorLayout.Behavior behavior =
1441a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                    ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior();
1442a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            if (behavior instanceof Behavior) {
14435e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes                // Offset the child, pinning it to the bottom the header-dependency, maintaining
1444bb3740d273d4b5108ab63f6d264466250a197e5bChris Banes                // any vertical gap and overlap
14455e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes                final Behavior ablBehavior = (Behavior) behavior;
1446318baf84dade07174d71e10322e3b10ab4b0c28cChris Banes                ViewCompat.offsetTopAndBottom(child, (dependency.getBottom() - child.getTop())
14475e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes                        + ablBehavior.mOffsetDelta
14485e7673e0dbd89512b525d1bde5c912eb07885550Chris Banes                        + getVerticalLayoutGap()
1449b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes                        - getOverlapPixelsForOffset(dependency));
1450ea004a01f44aaf36212120c936952c1742d03d30Chris Banes            }
1451ea004a01f44aaf36212120c936952c1742d03d30Chris Banes        }
1452ea004a01f44aaf36212120c936952c1742d03d30Chris Banes
1453b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes        @Override
1454b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes        float getOverlapRatioForOffset(final View header) {
1455b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes            if (header instanceof AppBarLayout) {
1456b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes                final AppBarLayout abl = (AppBarLayout) header;
1457ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                final int totalScrollRange = abl.getTotalScrollRange();
1458ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                final int preScrollDown = abl.getDownNestedPreScrollRange();
1459b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes                final int offset = getAppBarLayoutOffset(abl);
1460a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1461ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                if (preScrollDown != 0 && (totalScrollRange + offset) <= preScrollDown) {
1462ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                    // If we're in a pre-scroll down. Don't use the offset at all.
1463ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                    return 0;
1464a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                } else {
1465ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                    final int availScrollRange = totalScrollRange - preScrollDown;
1466dfb857dd12b359c0bdae11a23e039f286d54dc0aChris Banes                    if (availScrollRange != 0) {
1467ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                        // Else we'll use a interpolated ratio of the overlap, depending on offset
1468b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes                        return 1f + (offset / (float) availScrollRange);
1469ea004a01f44aaf36212120c936952c1742d03d30Chris Banes                    }
1470a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                }
1471a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
1472b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes            return 0f;
1473a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1474a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1475b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes        private static int getAppBarLayoutOffset(AppBarLayout abl) {
1476b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes            final CoordinatorLayout.Behavior behavior =
1477b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes                    ((CoordinatorLayout.LayoutParams) abl.getLayoutParams()).getBehavior();
1478b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes            if (behavior instanceof Behavior) {
1479b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes                return ((Behavior) behavior).getTopBottomOffsetForScrollingSibling();
1480b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes            }
1481b2f568c9c71763ed823b1f9c274077825b5d0c9eChris Banes            return 0;
1482a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1483a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes
1484ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor        @Override
148500a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes        AppBarLayout findFirstDependency(List<View> views) {
1486a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            for (int i = 0, z = views.size(); i < z; i++) {
1487a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                View view = views.get(i);
1488a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                if (view instanceof AppBarLayout) {
148900a00a7d3ba8279294f63994473afc32e05dcf10Chris Banes                    return (AppBarLayout) view;
1490a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes                }
1491a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            }
1492a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes            return null;
1493a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes        }
1494ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor
1495ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor        @Override
149672a0913607198c5ce3fa351242ccbdfb3b93f178Chris Banes        int getScrollRange(View v) {
1497ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor            if (v instanceof AppBarLayout) {
1498ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor                return ((AppBarLayout) v).getTotalScrollRange();
1499ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor            } else {
1500ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor                return super.getScrollRange(v);
1501ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor            }
1502ec161ac1da9c8ca0e942b01e037ceb1cc51a2f3cMady Mellor        }
1503a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes    }
1504a6a508b2296730ca6954aaebcca52a9962a5cb55Chris Banes}
1505