Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BottomSheet: How can I know if user is dragging up or down the bottomsheet?

I have an app with bottomsheet opened upto 90% of the screen height. How can I know if the user is dragging up/down the bottomsheet. I read we can use onSlide() but the offset values are NaN and sometimes outright weird, so it seems kinda unreliable. Any help is appreciated. Thanks in advance.

like image 681
Kanika Avatar asked Feb 14 '20 00:02

Kanika


People also ask

How do I stop my android from dragging the bottom sheet?

set bottomSheet onClickListener to null. bottomSheet. setOnClickListener(null); this line disables all action about bottomSheet only and does not effect on inner view.

What is bottom sheet behavior?

Bottom Sheet is a well-written material design component that performs enter/exit animations, respond to dragging/swiping gestures, etc. For using it in your project you should implement next dependencies into your app build.gradle file: implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"

When to use bottom sheet?

Android BottomSheet is a kind of view that is used as supplementary surfaces in mobile apps. This component is a part of the android design support library and is used to expose more data or information, menus, deep linked content, and in place of dialogs.

What is BottomSheetDialogFragment?

↳ com.google.android.material.bottomsheet.BottomSheetDialogFragment. Modal bottom sheet. This is a version of DialogFragment that shows a bottom sheet using BottomSheetDialog instead of a floating dialog.


2 Answers

bottomsheet documention doens't work but i think i figured it out to clear up the documentation read:

Hidden state is -1.0, collapsed state is 0.0 and expanded state is 1.0.

for me i see it only going from 1 to 0, and 0 to 1. That is because i did not have a hidden state, my sheeet was always to be shown at a peekheight. So as an example, if you have a sheet that will never been hidden you can try this:

just keep a property variable called oldOffSet and then do this:

behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
    var oldOffSet = 0f
    override fun onSlide(bottomSheet: View, slideOffset: Float) {
    
        val inRangeExpanding = oldOffSet < slideOffset
        val inRangeCollapsing = oldOffSet > slideOffset
        oldOffSet = slideOffset
    }
}
like image 70
j2emanue Avatar answered Sep 28 '22 00:09

j2emanue


You can add a BottomSheetCallback to know the status and the direction.

Something like:

    BottomSheetBehavior<...> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);

    bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {

      @Override public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_DRAGGING){
            // Dragging state
        }    
      }

      @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        // The new offset of this bottom sheet within [-1,1] range. 
        // Offset increasesas this bottom sheet is moving upward. 
        // From 0 to 1 the sheet is between collapsed and expanded states and 
        // From -1 to 0 it is between hidden and collapsed states.
      }
    });
like image 40
Gabriele Mariotti Avatar answered Sep 28 '22 00:09

Gabriele Mariotti