5

I have implemented bottom sheet using appcompat-v7:23.2.1.Everything is working fine,but the only problem is that when I drag up on the layout,the bottom sheet appears.I dont want the bottom sheet to be shown while dragging up on the layout when the bottom sheet is not shown 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 bottom sheet on click,but also the sheet appears while dragging up on the layout.Is there something i missing up.Please help me!

Selvam
  • 111
  • 1
  • 1
  • 9

4 Answers4

6

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
        }
    });

I can now disable the bottom sheet behavior when it is not shown.

Selvam
  • 111
  • 1
  • 1
  • 9
  • 4
    I don't understand why this answer is so common. This doesn't solve the issue. The dragging begins and it just stops the dragging. This is a hack, if you can even call it that. This is not a solution. – ᴛʜᴇᴘᴀᴛᴇʟ Mar 23 '17 at 23:30
  • what can I do if I want my `modal bottom sheet` to just be draggable till half the screen.., I mean it should never cover whole screen till the top. – eRaisedToX Apr 10 '17 at 10:32
0

You can Set the PeekHeight to 0.

behavior.setPeekHeight(0);
AHS12
  • 1
  • 4
-1

Well first in your

  onCreate

After mapping the bottomSheet then set the peekheight to 0 and set the state as collpsed

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

Here means every time the activity begins it's gonna be collapsed as default! Then just handle the callback set it and unset 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
    }
});
Nasz Njoka Sr.
  • 1,138
  • 16
  • 27
-1

In Kotlin

   view.mtv_title_registration.setOnClickListener {
                if (bottomSheetBehavior.state != BottomSheetBehavior.STATE_EXPANDED){
                    bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
                    val childLayoutParams = view.bottomSheet.layoutParams
                    val displayMetrics = DisplayMetrics()
                    activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
                    childLayoutParams.height = displayMetrics.heightPixels
                    view.bottomSheet.layoutParams = childLayoutParams
                }else {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }

            }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jose alves
  • 31
  • 4
  • Please don't post only code as an answer, but include an explanation what your code does and how it solves the problem of the question. Answers with an explanation are generally of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Dec 08 '19 at 09:43