BottomSheetBehavior is not work when design librar

2019-02-06 20:30发布

问题:

BottomSheetBehavior work properly in

compile 'com.android.support:design:24.1.1'

but when I update it to 24.2.0,it is not work.Is that a bug? This is my code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >

    <LinearLayout
        android:id="@+id/llScroll"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:background="@color/blue_1"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

回答1:

STATE_COLLAPSED : Hides a part of the bottom sheet

STATE_HIDDEN : Hides complete bottom sheet

In Support Library 24.2.0, you have to set the peek height to indicate how many pixels you want your bottom sheet to show when collapsed.

So if you want it to be collapsed and hid, you can add the code like this after you initialized your BottomSheetBehavior:

mBottomSheetBehavior.setPeekHeight(0);

that means when the bottom sheet collapsed, 0 pixel of its height will be shown.

Or your can just make it disappear if you need, use the code like this:

mBottomSheetBehavior.setHideable(true);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);


回答2:

Updated August 30th, 2016

The accepted answer explains the difference between STATE_HIDDEN and STATE_COLLAPSED and how to correctly use both in com.android.support:design:24.2.0.

As of August 20th, 2016

Although this does seem to be a bug with com.android.support:design:24.2.0, you can temporarily work around it by using BottomSheetBehavior.STATE_HIDDEN:

mBehavior.setHideable(true);
mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

This will still close the bottom sheet with an animation.

I'm not sure what the actual difference between STATE_COLLAPSED and STATE_HIDDEN is, and the documentation is less than helpful, but until it's actually fixed I think STATE_HIDDEN is okay.