使用BottomSheetBehaviour的视图始终可见,但状态已折叠

时间:2017-12-01 07:26:45

标签: android fragment bottom-sheet

我尝试将一个带有BottomSheetBehaviour的视图添加到我的片段中。不知何故,视图会显示,但状态BottomSheetBehaviour已折叠。

我尝试使用setState(BottomSheetBehavior.STATE_EXPANDED)设置状态,但操作无效:状态仍为STATE_COLLAPSED

有人可以告诉我为什么setState()方法没有效果吗?

// called in onViewCreated in my Fragement

private void initLayerButton(View view) {
    View bottomSheet = view.findViewById(R.id.bottom_sheet1);
    mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

    layerButton = (ImageButton) view.findViewById(R.id.layerButton);
    layerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mBottomSheetBehavior.getState() != 
BottomSheetBehavior.STATE_EXPANDED) {

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                Log.d(TAG, "expanded");
            }
            else {

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                Log.d(TAG, "collapsed");
            }
        }
    });
}

我片段的布局:

<?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">

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- some layouts of the view -->

    </LinearLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottom_sheet1"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@android:color/holo_blue_bright"
        android:clipToPadding="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="16dp"
                android:text="dfsd"
                android:textAllCaps="true"
                android:textSize="16sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="16dp"
                android:text="dsfa"
                android:textSize="16sp" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

试试这个......

要接收状态更改的回调,您可以添加 BottomSheetCallback

mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetCallback() {  
    @Override  
    public void onStateChanged(@NonNull View bottomSheet, int newState) {  

      // React to state change  

      if (newState == BottomSheetBehavior.STATE_EXPANDED) {
      //STATE_COLLAPSED
      }else{
      //STATE_EXPANDED;
      }

    }  
      @Override  
      public void onSlide(@NonNull View bottomSheet, float slideOffset) {  
       // React to dragging events  
   }  
 });