Standard android BottomSheetBehavior has tree state: hidden, collapsed and expanded.
I want to allow user to "leave" bottom sheet between collapsed and expanded. Now, with the default behavior, it will snap to collapsed or expanded based which is closest. How should I disable this snap functionality?
I will present a way to achievie such functionality for a
View
extendingBottomSheetDialogFragment
.Expanding:
First of all overrive
onResume
:What the code above is supposed to do is just setting the
BottomSheet
peekHeight
to the heigth of the view. The key here is the functiongetBottomSheetBehaviour()
. The implementation is below:This just check if the parent of
View
has 'CoordinatorLayout.LayoutParams' set. If yes, sets appropriateBottomSheetBehavior.BottomSheetCallback
(which is needed in the next part), and more importantly returns theCoordinatorLayout.Behavior
, which is supposed to beBottomSheetBehavior
.Collapsing:
Here a [`BottomSheetBehavior.BottomSheetCallback.onSlide (View bottomSheet, float slideOffset)``](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.BottomSheetCallback.html#onSlide(android.view.View, float)) is just exactly what is needed. From the [documentation](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.BottomSheetCallback.html#onSlide(android.view.View, float)):
This means, that just checking the second parameter is needed for collapse detection:
define
BottomSheetBehavior.BottomSheetCallback
in the same class: