Sunday, May 8, 2016

Scrolling is not working with CoordinatorLayout + parallax image + BottomSheetLayout

Leave a Comment

Introduction

I have an activity, which implements a common pattern with parallax header image and scrolling content using CoordinatorLayout, AppBarLayout and CollapsingToolbarLayout. My xml layout looks like this:

<android.support.design.widget.CoordinatorLayout     android:fitsSystemWindows="true"     android:layout_height="match_parent"     android:layout_width="match_parent">      <android.support.design.widget.AppBarLayout         android:fitsSystemWindows="true"         android:id="@+id/appbar"         android:layout_height="wrap_content"         android:layout_width="match_parent"         android:theme="@style/AppTheme.AppBarOverlay">          <android.support.design.widget.CollapsingToolbarLayout             android:fitsSystemWindows="true"             android:layout_height="wrap_content"             android:layout_width="match_parent"             app:contentScrim="?attr/colorPrimary"             app:layout_scrollFlags="scroll|exitUntilCollapsed">              <TextView                 android:background="@color/colorAccent"                 android:gravity="center"                 android:layout_height="250dp"                 android:layout_width="match_parent"                 android:text="ParallaxImage"                 app:layout_collapseMode="parallax"/>              <android.support.v7.widget.Toolbar                 android:id="@+id/toolbar"                 android:layout_height="?attr/actionBarSize"                 android:layout_width="match_parent"                 app:layout_collapseMode="pin"                 app:popupTheme="@style/AppTheme.PopupOverlay"/>          </android.support.design.widget.CollapsingToolbarLayout>     </android.support.design.widget.AppBarLayout>      <android.support.v4.widget.NestedScrollView         android:id="@+id/content"         android:layout_height="match_parent"         android:layout_width="match_parent"         app:layout_behavior="@string/appbar_scrolling_view_behavior">          <TextView             android:layout_height="wrap_content"             android:layout_margin="@dimen/text_margin"             android:layout_width="wrap_content"             android:text="@string/large_text"/>     </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> 

As you can see in the gif animation below, everything works correctly. You can scroll the whole screen from the content NestedScrollView as well as from the Toolbar or the parallax View.

AppBarLayout + NestedScrollView

Problem

Google introduced a BottomSheetBehavior class (Android design support library 23.2) to help developers to implement Bottom sheets. My xml layout with Bottom sheet looks like this:

<android.support.design.widget.CoordinatorLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:fitsSystemWindows="true">      <android.support.design.widget.AppBarLayout         android:id="@+id/appbar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:fitsSystemWindows="true"         android:theme="@style/AppTheme.AppBarOverlay">         <!-- ommited -->     </android.support.design.widget.AppBarLayout>      <android.support.v4.widget.NestedScrollView         android:id="@+id/content"         android:layout_width="match_parent"         android:layout_height="match_parent"         app:layout_behavior="@string/appbar_scrolling_view_behavior">         <!-- ommited -->     </android.support.v4.widget.NestedScrollView>      <LinearLayout         android:id="@+id/bottomSheet"         android:layout_width="match_parent"         android:layout_height="400dp"         android:background="@android:color/holo_blue_bright"         android:orientation="vertical"         app:behavior_peekHeight="?attr/actionBarSize"         app:layout_behavior="android.support.design.widget.BottomSheetBehavior">          <TextView             android:layout_width="match_parent"             android:layout_height="?attr/actionBarSize"             android:gravity="center_vertical"             android:paddingLeft="16dp"             android:paddingRight="16dp"             android:text="BottomSheetLayout"             android:textColor="@android:color/white"/>          <android.support.v4.widget.NestedScrollView             android:id="@+id/bottomSheetContent"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:scrollbars="vertical">              <TextView                 android:layout_width="match_parent"                 android:layout_height="400dp"                 android:background="@android:color/holo_green_dark"                 android:padding="16dp"                 android:text="@string/large_text"                 android:textColor="@android:color/white"/>          </android.support.v4.widget.NestedScrollView>     </LinearLayout>  </android.support.design.widget.CoordinatorLayout> 

And the result looks like this:

enter image description here

As you can see, Now I am not able to scroll, if I start to scroll from the parallax View. Scrolling from content NestedScrollView and from the Toolbar works as expected.

Question

How can I manage the scrolling to work from parallax View as well (the same way as in the first gif animation)? It seems that the BottomSheetBehavior intercepts touch events and prevents the AppBarLayout (AppBarLayoutBehavior) to handle the scroll. But the weird thing is that scrolling from Toolbar works and both parallax View and Toolbar are children of the AppBarLayout.

1 Answers

Answers 1

I think you should use NestedScrollView with the BottomSheetBehavior,replace blow as the bootemSheet!

<android.support.v4.widget.NestedScrollView     android:id="@+id/bottomSheet"     android:layout_width="match_parent"     android:layout_height="400dp"     android:background="@android:color/holo_blue_bright"     android:orientation="vertical"     app:behavior_peekHeight="?attr/actionBarSize"     app:layout_behavior="android.support.design.widget.BottomSheetBehavior">      <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical">          <TextView             android:layout_width="match_parent"             android:layout_height="?attr/actionBarSize"             android:gravity="center_vertical"             android:paddingLeft="16dp"             android:paddingRight="16dp"             android:text="BottomSheetLayout"             android:textColor="@android:color/white"/>          <ScrollView             android:layout_width="match_parent"             android:layout_height="match_parent">              <TextView                 android:layout_width="match_parent"                 android:layout_height="400dp"                 android:background="@android:color/holo_green_dark"                 android:padding="16dp"                 android:text="@string/large_text"                 android:textColor="@android:color/white"/>         </ScrollView>     </LinearLayout>  </android.support.v4.widget.NestedScrollView> 

NestedScrollView can know how to nested with the toolbar,not the LinearLayout!

wish to help!!

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment