BottomSheetBehavior.java revision 912d40d353f5b6d0a4fa18740ca40aa4b25b0f9c
1452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki/*
2452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * Copyright (C) 2015 The Android Open Source Project
3452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki *
4452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * Licensed under the Apache License, Version 2.0 (the "License");
5452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * you may not use this file except in compliance with the License.
6452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * You may obtain a copy of the License at
7452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki *
8452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki *      http://www.apache.org/licenses/LICENSE-2.0
9452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki *
10452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * Unless required by applicable law or agreed to in writing, software
11452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * distributed under the License is distributed on an "AS IS" BASIS,
12452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * See the License for the specific language governing permissions and
14452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * limitations under the License.
15452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki */
16452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
17452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakipackage android.support.design.widget;
18452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
19452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.content.Context;
20452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.content.res.TypedArray;
21452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.os.Parcel;
22452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.os.Parcelable;
23452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.support.annotation.IntDef;
24dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Arakiimport android.support.annotation.NonNull;
25452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.support.design.R;
26452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.support.v4.view.MotionEventCompat;
279aec720809a123c6193304730acf8b55d6ce5a7aYuichi Arakiimport android.support.v4.view.NestedScrollingChild;
28e3e7063749d095bba8743cad0aeb7164a567890dYuichi Arakiimport android.support.v4.view.VelocityTrackerCompat;
29452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.support.v4.view.ViewCompat;
30452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.support.v4.widget.ViewDragHelper;
31452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.util.AttributeSet;
32452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.view.MotionEvent;
33e3e7063749d095bba8743cad0aeb7164a567890dYuichi Arakiimport android.view.VelocityTracker;
34452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.view.View;
35e3e7063749d095bba8743cad0aeb7164a567890dYuichi Arakiimport android.view.ViewConfiguration;
36452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport android.view.ViewGroup;
37452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
38452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport java.lang.annotation.Retention;
39452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport java.lang.annotation.RetentionPolicy;
40452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakiimport java.lang.ref.WeakReference;
41452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
42452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
43452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki/**
44452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * An interaction behavior plugin for a child view of {@link CoordinatorLayout} to make it work as
45452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki * a bottom sheet.
46452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki */
47452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Arakipublic class BottomSheetBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {
48452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
49452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
504df3c15e330c2f9730d3aa712f57befec8536dffYuichi Araki     * Callback for monitoring events about bottom sheets.
51f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     */
524df3c15e330c2f9730d3aa712f57befec8536dffYuichi Araki    public abstract static class BottomSheetCallback {
53f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
54f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        /**
55f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki         * Called when the bottom sheet changes its state.
56f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki         *
57dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki         * @param bottomSheet The bottom sheet view.
58dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki         * @param newState    The new state. This will be one of {@link #STATE_DRAGGING},
59dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki         *                    {@link #STATE_SETTLING}, {@link #STATE_EXPANDED},
60dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki         *                    {@link #STATE_COLLAPSED}, or {@link #STATE_HIDDEN}.
61f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki         */
62dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki        public abstract void onStateChanged(@NonNull View bottomSheet, @State int newState);
63f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
64f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        /**
65f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki         * Called when the bottom sheet is being dragged.
66f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki         *
67dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki         * @param bottomSheet The bottom sheet view.
68f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki         * @param slideOffset The new offset of this bottom sheet within its range, from 0 to 1
69f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki         *                    when it is moving upward, and from 0 to -1 when it moving downward.
70f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki         */
71dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki        public abstract void onSlide(@NonNull View bottomSheet, float slideOffset);
72f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    }
73f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
74f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    /**
75452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * The bottom sheet is dragging.
76452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
77452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public static final int STATE_DRAGGING = 1;
78452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
79452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
80452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * The bottom sheet is settling.
81452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
82452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public static final int STATE_SETTLING = 2;
83452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
84452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
85452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * The bottom sheet is expanded.
86452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
87452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public static final int STATE_EXPANDED = 3;
88452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
89452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
90452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * The bottom sheet is collapsed.
91452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
92452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public static final int STATE_COLLAPSED = 4;
93452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
94f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    /**
95f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     * The bottom sheet is hidden.
96f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     */
97f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    public static final int STATE_HIDDEN = 5;
98f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
99452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /** @hide */
100f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    @IntDef({STATE_EXPANDED, STATE_COLLAPSED, STATE_DRAGGING, STATE_SETTLING, STATE_HIDDEN})
101452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @Retention(RetentionPolicy.SOURCE)
102452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public @interface State {}
103452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
104f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    private static final float HIDE_THRESHOLD = 0.5f;
105f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
106f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    private static final float HIDE_FRICTION = 0.1f;
107f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
108e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    private float mMaximumVelocity;
109f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
110452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private int mPeekHeight;
111452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
112452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private int mMinOffset;
113452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
114452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private int mMaxOffset;
115452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
116f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    private boolean mHideable;
117f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
118452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @State
119452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private int mState = STATE_COLLAPSED;
120452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
121452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private ViewDragHelper mViewDragHelper;
122452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
123452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private boolean mIgnoreEvents;
124452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
125e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    private int mLastNestedScrollDy;
126e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki
12749a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki    private boolean mNestedScrolled;
12849a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki
129452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private int mParentHeight;
130452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
131452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private WeakReference<V> mViewRef;
132452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
1339aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki    private WeakReference<View> mNestedScrollingChildRef;
134f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
1359aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki    private BottomSheetCallback mCallback;
136f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
137e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    private VelocityTracker mVelocityTracker;
138e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki
139e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    private int mActivePointerId;
140f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
1414eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki    private int mInitialY;
1424eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki
143b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki    private boolean mTouchingScrollingChild;
144b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki
145452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
146452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * Default constructor for instantiating BottomSheetBehaviors.
147452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
148452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public BottomSheetBehavior() {
149452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
150452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
151452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
152452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * Default constructor for inflating BottomSheetBehaviors from layout.
153452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     *
154452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @param context The {@link Context}.
155452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @param attrs   The {@link AttributeSet}.
156452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
157452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public BottomSheetBehavior(Context context, AttributeSet attrs) {
158452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        super(context, attrs);
159452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        TypedArray a = context.obtainStyledAttributes(attrs,
160452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                R.styleable.BottomSheetBehavior_Params);
161452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        setPeekHeight(a.getDimensionPixelSize(
162452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                R.styleable.BottomSheetBehavior_Params_behavior_peekHeight, 0));
163f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        setHideable(a.getBoolean(R.styleable.BottomSheetBehavior_Params_behavior_hideable, false));
164452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        a.recycle();
165e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        ViewConfiguration configuration = ViewConfiguration.get(context);
166e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
167452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
168452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
169452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @Override
170452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public Parcelable onSaveInstanceState(CoordinatorLayout parent, V child) {
171452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        return new SavedState(super.onSaveInstanceState(parent, child), mState);
172452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
173452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
174452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @Override
175452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public void onRestoreInstanceState(CoordinatorLayout parent, V child, Parcelable state) {
176452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        SavedState ss = (SavedState) state;
177452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        super.onRestoreInstanceState(parent, child, ss.getSuperState());
178452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        // Intermediate states are restored as collapsed state
179f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        if (ss.state == STATE_DRAGGING || ss.state == STATE_SETTLING) {
180452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            mState = STATE_COLLAPSED;
181f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        } else {
182f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            mState = ss.state;
183452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
184452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
185452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
186452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @Override
187452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
188452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        // First let the parent lay it out
189f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        if (mState != STATE_DRAGGING && mState != STATE_SETTLING) {
190f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            parent.onLayoutChild(child, layoutDirection);
191f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        }
192452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        // Offset the bottom sheet
193452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        mParentHeight = parent.getHeight();
194452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        mMinOffset = Math.max(0, mParentHeight - child.getHeight());
1953bd9d18c5855600254e907d6668f2ec4d7fd1c51Yuichi Araki        mMaxOffset = Math.max(mParentHeight - mPeekHeight, mMinOffset);
196452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        if (mState == STATE_EXPANDED) {
197452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            ViewCompat.offsetTopAndBottom(child, mMinOffset);
198f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        } else if (mHideable && mState == STATE_HIDDEN) {
199f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            ViewCompat.offsetTopAndBottom(child, mParentHeight);
200f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        } else if (mState == STATE_COLLAPSED) {
201452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            ViewCompat.offsetTopAndBottom(child, mMaxOffset);
202452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
203452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        if (mViewDragHelper == null) {
204452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
205452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
206452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        mViewRef = new WeakReference<>(child);
2079aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
208452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        return true;
209452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
210452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
211452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @Override
212452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
21317fc77f5ca7c91daeab2e110669d9baf9d1a8090Yuichi Araki        if (!child.isShown()) {
21417fc77f5ca7c91daeab2e110669d9baf9d1a8090Yuichi Araki            return false;
21517fc77f5ca7c91daeab2e110669d9baf9d1a8090Yuichi Araki        }
216452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        int action = MotionEventCompat.getActionMasked(event);
217e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        // Record the velocity
218e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (action == MotionEvent.ACTION_DOWN) {
219e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            reset();
220e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
221e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (mVelocityTracker == null) {
222e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            mVelocityTracker = VelocityTracker.obtain();
223e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
224e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        mVelocityTracker.addMovement(event);
225452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        switch (action) {
226452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            case MotionEvent.ACTION_UP:
227452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            case MotionEvent.ACTION_CANCEL:
228b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki                mTouchingScrollingChild = false;
229e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                mActivePointerId = MotionEvent.INVALID_POINTER_ID;
230452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                // Reset the ignore flag
231452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                if (mIgnoreEvents) {
232452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    mIgnoreEvents = false;
233452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    return false;
234452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                }
235452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                break;
236452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            case MotionEvent.ACTION_DOWN:
2374eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki                int initialX = (int) event.getX();
2384eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki                mInitialY = (int) event.getY();
2399aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                View scroll = mNestedScrollingChildRef.get();
2404eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki                if (scroll != null && parent.isPointInChildBounds(scroll, initialX, mInitialY)) {
241e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                    mActivePointerId = event.getPointerId(event.getActionIndex());
242b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki                    mTouchingScrollingChild = true;
2439aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                }
244e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                mIgnoreEvents = mActivePointerId == MotionEvent.INVALID_POINTER_ID &&
2454eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki                        !parent.isPointInChildBounds(child, initialX, mInitialY);
246452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                break;
247452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
2484eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        if (!mIgnoreEvents && mViewDragHelper.shouldInterceptTouchEvent(event)) {
2494eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki            return true;
2504eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        }
2514eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        // We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
2524eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        // it is not the top most view of its parent. This is not necessary when the touch event is
2534eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        // happening over the scrolling content as nested scrolling logic handles that case.
2544eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        View scroll = mNestedScrollingChildRef.get();
2554eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        return action == MotionEvent.ACTION_MOVE && scroll != null &&
2564eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki                !mIgnoreEvents && mState != STATE_DRAGGING &&
2574eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki                !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY()) &&
2584eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki                Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop();
259452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
260452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
261452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @Override
262452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
26317fc77f5ca7c91daeab2e110669d9baf9d1a8090Yuichi Araki        if (!child.isShown()) {
26417fc77f5ca7c91daeab2e110669d9baf9d1a8090Yuichi Araki            return false;
26517fc77f5ca7c91daeab2e110669d9baf9d1a8090Yuichi Araki        }
266e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        int action = MotionEventCompat.getActionMasked(event);
267e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
268e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            return true;
269e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
270452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        mViewDragHelper.processTouchEvent(event);
271e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        // Record the velocity
272e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (action == MotionEvent.ACTION_DOWN) {
273e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            reset();
274e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
275e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (mVelocityTracker == null) {
276e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            mVelocityTracker = VelocityTracker.obtain();
277e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
278e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        mVelocityTracker.addMovement(event);
2794eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
2804eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        // to capture the bottom sheet in case it is not captured and the touch slop is passed.
281912d40d353f5b6d0a4fa18740ca40aa4b25b0f9cYuichi Araki        if (action == MotionEvent.ACTION_MOVE && !mIgnoreEvents) {
2824eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki            if (Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
2834eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki                mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
2844eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki            }
2854eb453c75e552a2e0231a93468d9597a4069286eYuichi Araki        }
286452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        return true;
287452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
288452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
289e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    @Override
290e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child,
291e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            View directTargetChild, View target, int nestedScrollAxes) {
292e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        mLastNestedScrollDy = 0;
29349a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki        mNestedScrolled = false;
294e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
295e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    }
296e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki
297e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    @Override
298e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx,
299e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            int dy, int[] consumed) {
300e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        View scrollingChild = mNestedScrollingChildRef.get();
301e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (target != scrollingChild) {
302e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            return;
303e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
304e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        int currentTop = child.getTop();
305e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        int newTop = currentTop - dy;
306e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (dy > 0) { // Upward
307e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            if (newTop < mMinOffset) {
308e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                consumed[1] = currentTop - mMinOffset;
309e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                ViewCompat.offsetTopAndBottom(child, -consumed[1]);
310e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                setStateInternal(STATE_EXPANDED);
311e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            } else {
312e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                consumed[1] = dy;
313e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                ViewCompat.offsetTopAndBottom(child, -dy);
314e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                setStateInternal(STATE_DRAGGING);
315e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            }
316e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        } else if (dy < 0) { // Downward
317e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            if (!ViewCompat.canScrollVertically(target, -1)) {
318e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                if (newTop <= mMaxOffset || mHideable) {
319e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                    consumed[1] = dy;
320e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                    ViewCompat.offsetTopAndBottom(child, -dy);
321e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                    setStateInternal(STATE_DRAGGING);
322e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                } else {
323e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                    consumed[1] = currentTop - mMaxOffset;
324e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                    ViewCompat.offsetTopAndBottom(child, -consumed[1]);
325e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                    setStateInternal(STATE_COLLAPSED);
326e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                }
327e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            }
328e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
329e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        dispatchOnSlide(child.getTop());
330e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        mLastNestedScrollDy = dy;
33149a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki        mNestedScrolled = true;
332e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    }
333e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki
334e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    @Override
335e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target) {
336b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki        if (child.getTop() == mMinOffset) {
337b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki            setStateInternal(STATE_EXPANDED);
338b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki            return;
339b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki        }
340b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki        if (target != mNestedScrollingChildRef.get() || !mNestedScrolled) {
341e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            return;
342e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
343e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        int top;
344e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        int targetState;
345e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (mLastNestedScrollDy > 0) {
346e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            top = mMinOffset;
347e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            targetState = STATE_EXPANDED;
348e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        } else if (mHideable && shouldHide(child, getYVelocity())) {
349e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            top = mParentHeight;
350e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            targetState = STATE_HIDDEN;
35149a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki        } else if (mLastNestedScrollDy == 0) {
35249a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki            int currentTop = child.getTop();
35349a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki            if (Math.abs(currentTop - mMinOffset) < Math.abs(currentTop - mMaxOffset)) {
35449a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                top = mMinOffset;
35549a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                targetState = STATE_EXPANDED;
35649a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki            } else {
35749a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                top = mMaxOffset;
35849a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                targetState = STATE_COLLAPSED;
35949a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki            }
360e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        } else {
361e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            top = mMaxOffset;
362e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            targetState = STATE_COLLAPSED;
363e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
364e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (mViewDragHelper.smoothSlideViewTo(child, child.getLeft(), top)) {
365e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            setStateInternal(STATE_SETTLING);
366e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            ViewCompat.postOnAnimation(child, new SettleRunnable(child, targetState));
367e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        } else {
368e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            setStateInternal(targetState);
369e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
37049a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki        mNestedScrolled = false;
371e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    }
372e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki
373e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    @Override
374e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target,
375e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            float velocityX, float velocityY) {
376e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        return target == mNestedScrollingChildRef.get() &&
377e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                (mState != STATE_EXPANDED ||
378e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                        super.onNestedPreFling(coordinatorLayout, child, target,
379e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                                velocityX, velocityY));
380e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    }
381e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki
382452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
383452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * Sets the height of the bottom sheet when it is collapsed.
384452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     *
385452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @param peekHeight The height of the collapsed bottom sheet in pixels.
386452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @attr ref android.support.design.R.styleable#BottomSheetBehavior_Params_behavior_peekHeight
387452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
388452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public final void setPeekHeight(int peekHeight) {
389452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        mPeekHeight = Math.max(0, peekHeight);
390452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        mMaxOffset = mParentHeight - peekHeight;
391452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
392452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
393452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
394452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * Gets the height of the bottom sheet when it is collapsed.
395452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     *
396452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @return The height of the collapsed bottom sheet.
397452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @attr ref android.support.design.R.styleable#BottomSheetBehavior_Params_behavior_peekHeight
398452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
399452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public final int getPeekHeight() {
400452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        return mPeekHeight;
401452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
402452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
403452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
404f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     * Sets whether this bottom sheet can hide when it is swiped down.
405f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     *
406f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     * @param hideable {@code true} to make this bottom sheet hideable.
407f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     * @attr ref android.support.design.R.styleable#BottomSheetBehavior_Params_behavior_hideable
408f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     */
409f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    public void setHideable(boolean hideable) {
410f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        mHideable = hideable;
411f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    }
412f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
413f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    /**
414f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     * Gets whether this bottom sheet can hide when it is swiped down.
415f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     *
416f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     * @return {@code true} if this bottom sheet can hide.
417f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     * @attr ref android.support.design.R.styleable#BottomSheetBehavior_Params_behavior_hideable
418f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     */
419f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    public boolean isHideable() {
420f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        return mHideable;
421f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    }
422f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
423f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    /**
4244df3c15e330c2f9730d3aa712f57befec8536dffYuichi Araki     * Sets a callback to be notified of bottom sheet events.
425f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     *
4264df3c15e330c2f9730d3aa712f57befec8536dffYuichi Araki     * @param callback The callback to notify when bottom sheet events occur.
427f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     */
4284df3c15e330c2f9730d3aa712f57befec8536dffYuichi Araki    public void setBottomSheetCallback(BottomSheetCallback callback) {
4294df3c15e330c2f9730d3aa712f57befec8536dffYuichi Araki        mCallback = callback;
430f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    }
431f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
432f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    /**
433452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * Sets the state of the bottom sheet. The bottom sheet will transition to that state with
434452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * animation.
435452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     *
436f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     * @param state One of {@link #STATE_COLLAPSED}, {@link #STATE_EXPANDED}, or
437f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki     *              {@link #STATE_HIDDEN}.
438452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
439452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public final void setState(@State int state) {
440452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        V child = mViewRef.get();
441452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        if (child == null) {
442452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            return;
443452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
444452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        int top;
445452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        if (state == STATE_COLLAPSED) {
446452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            top = mMaxOffset;
447452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        } else if (state == STATE_EXPANDED) {
448452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            top = mMinOffset;
449f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        } else if (mHideable && state == STATE_HIDDEN) {
450f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            top = mParentHeight;
451452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        } else {
452452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            throw new IllegalArgumentException("Illegal state argument: " + state);
453452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
454452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        setStateInternal(STATE_SETTLING);
455452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        if (mViewDragHelper.smoothSlideViewTo(child, child.getLeft(), top)) {
456452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            ViewCompat.postOnAnimation(child, new SettleRunnable(child, state));
457452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
458452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
459452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
460452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    /**
461452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * Gets the current state of the bottom sheet.
462452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     *
463452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @return One of {@link #STATE_EXPANDED}, {@link #STATE_COLLAPSED}, {@link #STATE_DRAGGING},
464452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * and {@link #STATE_SETTLING}.
465452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
466452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @State
467452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public final int getState() {
468452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        return mState;
469452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
470452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
471452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private void setStateInternal(@State int state) {
472452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        if (mState == state) {
473452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            return;
474452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
475452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        mState = state;
476dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki        View bottomSheet = mViewRef.get();
477dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki        if (bottomSheet != null && mCallback != null) {
478dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki            mCallback.onStateChanged(bottomSheet, state);
479f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        }
480f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    }
481f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
482e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    private void reset() {
483e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        mActivePointerId = ViewDragHelper.INVALID_POINTER;
484e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        if (mVelocityTracker != null) {
485e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            mVelocityTracker.recycle();
486e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            mVelocityTracker = null;
487e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        }
488e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    }
489e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki
490f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    private boolean shouldHide(View child, float yvel) {
491f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        if (child.getTop() < mMaxOffset) {
492f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            // It should not hide, but collapse.
493f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            return false;
494f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        }
495f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        final float newTop = child.getTop() + yvel * HIDE_FRICTION;
496f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        return Math.abs(newTop - mMaxOffset) / (float) mPeekHeight > HIDE_THRESHOLD;
497f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    }
498f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
4999aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki    private View findScrollingChild(View view) {
5009aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        if (view instanceof NestedScrollingChild) {
5019aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki            return view;
5029aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        }
5039aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        if (view instanceof ViewGroup) {
5049aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki            ViewGroup group = (ViewGroup) view;
5059aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki            for (int i = 0, count = group.getChildCount(); i < count; i++) {
5069aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                View scrollingChild = findScrollingChild(group.getChildAt(i));
5079aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                if (scrollingChild != null) {
5089aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                    return scrollingChild;
5099aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                }
5109aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki            }
5119aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        }
5129aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        return null;
513452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
514452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
515e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    private float getYVelocity() {
516e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
517e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki        return VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
518e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki    }
519e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki
520452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private final ViewDragHelper.Callback mDragCallback = new ViewDragHelper.Callback() {
521452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
522452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @Override
523452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public boolean tryCaptureView(View child, int pointerId) {
524e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            if (mState == STATE_DRAGGING) {
525e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki                return false;
526e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            }
527b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki            if (mTouchingScrollingChild) {
528b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki                return false;
529b1eb1357ec33343b7271a6d3876ac4af84e15510Yuichi Araki            }
530e3e7063749d095bba8743cad0aeb7164a567890dYuichi Araki            if (mState == STATE_EXPANDED && mActivePointerId == pointerId) {
5319aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                View scroll = mNestedScrollingChildRef.get();
5329aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                if (scroll != null && ViewCompat.canScrollVertically(scroll, -1)) {
5339aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                    // Let the content scroll up
5349aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                    return false;
5359aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                }
5369aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki            }
537f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            return mViewRef != null && mViewRef.get() == child;
538f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        }
539f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
540f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        @Override
541f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
542f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            dispatchOnSlide(top);
543452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
544452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
545452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @Override
546452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public void onViewDragStateChanged(int state) {
547452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            if (state == ViewDragHelper.STATE_DRAGGING) {
548452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                setStateInternal(STATE_DRAGGING);
549452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            }
550452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
551452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
552452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @Override
553452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public void onViewReleased(View releasedChild, float xvel, float yvel) {
554452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            int top;
555452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            @State int targetState;
556f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            if (yvel < 0) { // Moving up
557452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                top = mMinOffset;
558452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                targetState = STATE_EXPANDED;
559f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            } else if (mHideable && shouldHide(releasedChild, yvel)) {
560f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki                top = mParentHeight;
561f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki                targetState = STATE_HIDDEN;
56249a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki            } else if (yvel == 0.f) {
56349a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                int currentTop = releasedChild.getTop();
56449a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                if (Math.abs(currentTop - mMinOffset) < Math.abs(currentTop - mMaxOffset)) {
56549a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                    top = mMinOffset;
56649a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                    targetState = STATE_EXPANDED;
56749a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                } else {
56849a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                    top = mMaxOffset;
56949a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                    targetState = STATE_COLLAPSED;
57049a5904a773a7cfac127c5938671ed078d8ec8f3Yuichi Araki                }
571452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            } else {
572452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                top = mMaxOffset;
573452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                targetState = STATE_COLLAPSED;
574452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            }
575452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            if (mViewDragHelper.settleCapturedViewAt(releasedChild.getLeft(), top)) {
576f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki                setStateInternal(STATE_SETTLING);
577452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                ViewCompat.postOnAnimation(releasedChild,
578452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                        new SettleRunnable(releasedChild, targetState));
579f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            } else {
580f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki                setStateInternal(targetState);
581452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            }
582452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
583452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
584452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @Override
585452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public int clampViewPositionVertical(View child, int top, int dy) {
586f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            return MathUtils.constrain(top, mMinOffset, mHideable ? mParentHeight : mMaxOffset);
587452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
588452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
589452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @Override
590452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public int clampViewPositionHorizontal(View child, int left, int dx) {
591452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            return child.getLeft();
592452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
593f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
5949aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        @Override
5959aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        public int getViewVerticalDragRange(View child) {
5969aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki            if (mHideable) {
5979aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                return mParentHeight - mMinOffset;
5989aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki            } else {
5999aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki                return mMaxOffset - mMinOffset;
6009aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki            }
6019aec720809a123c6193304730acf8b55d6ce5a7aYuichi Araki        }
602452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    };
603452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
604f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    private void dispatchOnSlide(int top) {
605dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki        View bottomSheet = mViewRef.get();
606dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki        if (bottomSheet != null && mCallback != null) {
607f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            if (top > mMaxOffset) {
608dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki                mCallback.onSlide(bottomSheet, (float) (mMaxOffset - top) / mPeekHeight);
609f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            } else {
610dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki                mCallback.onSlide(bottomSheet,
611dffd8d4be91b2e5e0ce66ad96867182db0c02fd0Yuichi Araki                        (float) (mMaxOffset - top) / ((mMaxOffset - mMinOffset)));
612f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki            }
613f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki        }
614f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    }
615f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki
616452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    private class SettleRunnable implements Runnable {
617452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
618452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        private final View mView;
619452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
620452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @State
621452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        private final int mTargetState;
622452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
623452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        SettleRunnable(View view, @State int targetState) {
624452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            mView = view;
625452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            mTargetState = targetState;
626452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
627452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
628452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @Override
629452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public void run() {
630452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            if (mViewDragHelper != null && mViewDragHelper.continueSettling(true)) {
631452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                ViewCompat.postOnAnimation(mView, this);
632452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            } else {
633452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                setStateInternal(mTargetState);
634452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            }
635452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
636452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
637452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
638452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    protected static class SavedState extends View.BaseSavedState {
639452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
640452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @State
641452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        final int state;
642452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
643452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public SavedState(Parcel source) {
644452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            super(source);
645452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            //noinspection ResourceType
646452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            state = source.readInt();
647452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
648452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
649452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public SavedState(Parcelable superState, @State int state) {
650452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            super(superState);
651452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            this.state = state;
652452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
653452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
654452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        @Override
655452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public void writeToParcel(Parcel out, int flags) {
656452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            super.writeToParcel(out, flags);
657452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            out.writeInt(state);
658452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
659452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
660452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        public static final Parcelable.Creator<SavedState> CREATOR =
661452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                new Parcelable.Creator<SavedState>() {
662452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    @Override
663452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    public SavedState createFromParcel(Parcel source) {
664452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                        return new SavedState(source);
665452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    }
666452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
667452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    @Override
668452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    public SavedState[] newArray(int size) {
669452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                        return new SavedState[size];
670452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    }
671452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                };
672452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
673452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
674f28a0f76c7a46d61785ef7cbc407c19942ecab46Yuichi Araki    /**
675452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * A utility function to get the {@link BottomSheetBehavior} associated with the {@code view}.
676452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     *
677452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @param view The {@link View} with {@link BottomSheetBehavior}.
678452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     * @return The {@link BottomSheetBehavior} associated with the {@code view}.
679452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki     */
680452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    @SuppressWarnings("unchecked")
681452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    public static <V extends View> BottomSheetBehavior<V> from(V view) {
682452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        ViewGroup.LayoutParams params = view.getLayoutParams();
683452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        if (!(params instanceof CoordinatorLayout.LayoutParams)) {
684452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
685452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
686452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
687452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                .getBehavior();
688452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        if (!(behavior instanceof BottomSheetBehavior)) {
689452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki            throw new IllegalArgumentException(
690452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki                    "The view is not associated with BottomSheetBehavior");
691452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        }
692452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki        return (BottomSheetBehavior<V>) behavior;
693452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki    }
694452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki
695452f94d1c58e2a7476019b98c3bf0e4b322d1525Yuichi Araki}
696