Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable scrolling of AppBarLayout in CoordinatorLayout?

I have MapFragment with parallax effect inside AppBarLayout:

enter image description here

I want to disable scrolling on AppBarLayout, because it is not possible to move across map, since touch evenys on the map are always handled as scroll events. I would like to handle collapsing of AppBarLayout by scrolling RecyclerView only, which is on the bottom of the screen.

This is my xml:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.AppBarLayout     android:id="@+id/appbar"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:fitsSystemWindows="true"     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">      <android.support.design.widget.CollapsingToolbarLayout         android:id="@+id/collapsing_toolbar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:fitsSystemWindows="true"         app:contentScrim="@color/white"         app:expandedTitleMarginEnd="64dp"         app:expandedTitleMarginStart="48dp"         app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"         app:titleEnabled="false">          <FrameLayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             app:layout_collapseMode="parallax"             android:fitsSystemWindows="true">              <fragment                 android:id="@+id/map"                 android:name="com.androidmapsextensions.SupportMapFragment"                 android:layout_width="match_parent"                 android:layout_height="match_parent" />         </FrameLayout>          <android.support.v7.widget.Toolbar             android:id="@+id/toolbar"             android:layout_width="match_parent"             android:layout_height="73dp"             app:contentInsetLeft="0dp"             app:contentInsetStart="0dp"             app:layout_collapseMode="pin"             app:popupTheme="@style/ThemeOverlay.AppCompat.Light">              <include                 android:id="@+id/search_bar"                 layout="@layout/layout_searchbar" />         </android.support.v7.widget.Toolbar>          <View             android:id="@+id/toolbar_shadow"             android:layout_width="match_parent"             android:layout_height="3dp"             android:layout_below="@id/search_bar"             android:layout_gravity="bottom"             android:background="@drawable/toolbar_dropshadow"             android:visibility="gone" />     </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout>   <RecyclerView     android:id="@+id/farm_list"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@android:color/transparent"     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

Thank you for the response.

like image 626
janox1x2x3 Avatar asked Dec 05 '15 17:12

janox1x2x3


People also ask

How do I stop my ToolBar from collapsing?

Solution: The solution is simple, we just need to set the app:scrimAnimationDuration=”0" in our collapsing toolbar layout like the below code snippet.

What is the difference between AppBarLayout and ToolBar?

AppBarLayout is a parent layout of ToolBar and ToolBar is custom ActionBar. if you want scroll action on the ToolBar so you should write ToolBar into the AppBarLayout,before you will write code for scroll the ToolBar, you must know the NestedScrollBar,it is used to scroll the ToolBar.

What is the use of CoordinatorLayout in Android?

CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.

What is the use of AppBarLayout in Android?

AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures. Children should provide their desired scrolling behavior through AppBarLayout.


1 Answers

I'm not sure I got it, but I think you are looking for a DragCallback.

The DragCallback interface allows to choose whether the sibling scrolling view should be controlled by scrolls onto the AppBarLayout.

You can define one by calling:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams(); AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior(); behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {     @Override     public boolean canDrag(@NonNull AppBarLayout appBarLayout) {         return false;     } }); 

By always returning false, your scrolling view will not be controlled by the ABL any longer.

Note: before calling this you should check that ViewCompat.isLaidOut(appBarLayout), otherwise params.getBehavior() will return null.

like image 145
natario Avatar answered Sep 23 '22 18:09

natario