4

I have inside my BottomSheet view a NestedScrollView and I want to drag down the bottomSheet when scrolling down from the nestedScrollview but it is not working.

My XML code is like below :

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:behavior_hideable="true"
        app:behavior_peekHeight="230dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

     <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/window_background"
            app:elevation="0dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
     
           ...

      </com.google.android.material.appbar.AppBarLayout>

      <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">  

           ...

      </androidx.core.widget.NestedScrollView>    
 
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Zain
  • 37,492
  • 7
  • 60
  • 84
Mohamed Jihed Jaouadi
  • 1,427
  • 4
  • 25
  • 44

1 Answers1

0

The reason it's not dragging down the BottomSheet that the nested scrolling of the NestedScrollView is enabled which catches the scrolling event until scrolling all the way up.

Disabling the NestedScrollView is not an option because it's needed in bottom scrolling. And intermittent enabling/disabling of it can cause unexpected scrolling behavior.

Workaround: An option to solve this; is to scroll all the way up whenever the NestedScrollView detects an up scroll, so that the bottom sheet can take the chance of collapsing:

// Kotlin 
nestedScrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
    if (scrollY < oldScrollY) // Scroll up
        nestedScrollView.scrollTo(0, 0)
})

// Java
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView nestedScrollView, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if (scrollY < oldScrollY) // Scroll up
            nestedScrollView.scrollTo(0, 0);
    }
});

The down side that the user would see the top content of the NestedScrollView before it goes off; or even if they changed their mind of not collapsing the bottom sheet. Probably not the best option; but it is simple and works.

Zain
  • 37,492
  • 7
  • 60
  • 84