Android 按钮 sheet 不工作

Android Buttom sheet is not working

Android 底部 sheet 无效。 下面是我的代码,我想创建 bottom sheet 演示,但它不起作用,bottom sheet 没有显示。

    public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    private BottomSheetBehavior mBottomSheetBehavior;
    private Button button1;
    private View bottomSheet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getId();
        setListner();
        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    }

    private void getId() {
        try {
            button1 = (Button)findViewById(R.id.button1);
            bottomSheet = findViewById(R.id.bottom_sheet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void setListner() {
        try {
            button1.setOnClickListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onClick(View v)
    {
        try {
            switch (v.getId())
            {
                case R.id.button1:
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

                    Log.e("Buttom clicked",">>");
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的 xml 文件。

        <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button 1"
                android:padding="16dp"
                android:layout_margin="8dp"
                android:textColor="@android:color/white"
                android:background="@android:color/holo_green_dark"/>
        </LinearLayout>
    </ScrollView>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:clipToPadding="true"
        android:background="@android:color/holo_orange_light"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/ic_launcher"
            android:padding="16dp"
            android:textSize="16sp"/>
    </android.support.v4.widget.NestedScrollView>

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

实际上我想在不使用任何其他库的情况下使用 Android Support Library 23.2 实现底部 sheet,但它不起作用。 谢谢并提前。

使用这个依赖编译'com.cocosw:bottomsheet:1.2.0'

并添加此代码

new BottomSheet.Builder(getActivity(), R.style.BottomSheet_StyleDialog).
                    sheet(R.menu.bottom_sheet).title(displayname).listener(new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case R.id.btnOne:
                            // do something...
                            break;
                        case R.id.btnTwo:
                           // do something ...
                                  }
                             } 
            }).show();