3

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.

enter image description here


<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?

Matt Robertson
  • 2,928
  • 5
  • 34
  • 62

1 Answers1

4

Old question, but answering so it may help someone.

Try following:

  • Extend BottomSheetBehaviour to create your own CustomDraggableBottomSheetBehaviour
  • In your CustomDraggableBottomSheetBehaviour override onInterceptTouchEvent() and use isDraggable property to enable/disable dragging if touch points are inside your "specific" view.
  • In your xml layout file provide reference to your 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)

prodev
  • 575
  • 5
  • 15