I have a bottom sheet implemented using BottomSheetBehavior within a CoordinatorLayout. My BottomSheet has a top appbar sort of title bar, and then some scrollable content. Something like this.
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<LinearLayout
android:id="@+id/bottom_sheet"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:orientation="vertical">
...
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I want the user to be able to drag the bottom sheet to the expanded, half-expanded, and hidden positions, but only when dragging the bottom sheet's top appbar. So if the bottom sheet is at the half-expanded state, dragging/scrolling/flinging the scrollable content within the bottom sheet should not adjust it's position - it should remain half-expanded. But dragging the top appbar up/down should cause the bottom sheet to perform its normal draggable behavior.
Is this possible with BottomSheetBehavior?
Old question, but answering so it may help someone.
Try following:
BottomSheetBehaviour
to create your own CustomDraggableBottomSheetBehaviour
CustomDraggableBottomSheetBehaviour
override onInterceptTouchEvent()
and use isDraggable
property to enable/disable dragging if touch points are inside your "specific" view.CustomDraggableBottomSheetBehaviour
app:layout_behaviour="com.myapp.CustomDraggableBottomSheetBehaviour"
Here is an example of that class in kotlin:
class CustomDraggableBottomSheetBehaviour<V: View>: BottomSheetBehaviour<V> {
var draggableView: View? = null
constructor(): super()
constructor(ctx: Context, attrs: AttributeSet): super(ctx, attrs)
override fun onInterceptTouchEvent(parent: CoordinatorLayout, child: V, event: MotionEvent): Boolean {
draggableView?.let {
isDraggable = parent.isPointInChildBounds(it, event.x.toInt(), event.y.toInt())
}
return super.onInterceptTouchEvent(parent, child, event)
}
}
Just assign the draggableView
property to your 'Top AppBar' on object received by BottomSheetBehaviour.from(someViewGoup)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With