The behavior of the bottom sheet should only be displayed when you click?

I implemented the bottom sheet using appcompat-v7: 23.2.1 . Everything works fine, but the only problem is that when I drag on the layout, the bottom sheet appears. so that the bottom sheet is displayed when you drag it onto the layout when the bottom sheet is not displayed on the screen.

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/DarkGray"
tools:context=".bottom_sheets.grid.BottomSheetGridActivity">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <Button
        android:id="@+id/btnView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show view" />
  </LinearLayout>
   <LinearLayout
    android:id="@+id/bottom_sheet1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_marginBottom="8dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:background="#fff"
        />
 </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Mainactivity

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bottom_grid);
    Button btnView = (Button) findViewById(R.id.btnView);
    View bottomSheet = findViewById(R.id.bottom_sheet1);
    behavior = BottomSheetBehavior.from(bottomSheet);
    btnView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

             behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });

    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change

        }

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

    RecyclerView listRecyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
    listRecyclerView.setHasFixedSize(true);
    listRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    RecyclerItemAdapter recyclerItemAdapter = new RecyclerItemAdapter(createItems(), this);
    listRecyclerView.setAdapter(recyclerItemAdapter);
}

The above code will show the bottom sheet when clicked, but the sheet also appears when drag and drop on the layout. Something I missed there. Please help me!

+4
source share
3 answers

Thanks @oguzhand

 behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change
            if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        }

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

Now I can disable the behavior of the bottom sheet when it is not displayed.

+5

PeekHeight 0.

behavior.setPeekHeight(0);
0

Ok first in

  onCreate

After matching the bottom sheet, set the peekheight to 0 and set the state as hooked

    mBottomSheet.setPeekHeight(0);
    mBottomSheet.setState(BottomSheetBehavior.STATE_COLLAPSED);

Here, every time an action begins, it will be minimized by default! Then just handle the callback and set it on the go.

       behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        // react to events
          switch(newState){
             case BottomSheetBehavior.STATE_COLLAPSED:
                 mBottomSheet.setPeekHeight(0);
             break;
             case case BottomSheetBehavior.STATE_EXPANDED:
                 mBottomSheet.setPeekHeight(400);
                 //then do your other staff here
        }
    }

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

All Articles