Question:
Is there a way to turn off when scrolling RecyclerView
another damper, which is higher RecyclerView
? I know how to do this with the Toolbar
, but something is not clear how to do the same with other views?
Answer:
Here's some sample code. You need to put your View
in AppBarLayout
. Here the layoutHeader
will be scrolled when the RecyclerView
scrolled.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/layoutHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/mkbPrimaryColor"
android:orientation="vertical"
android:visibility="visible"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Notice the additional attributes app:layout_behavior="@string/appbar_scrolling_view_behavior"
and app:layout_scrollFlags="scroll|enterAlways"
AppBarLayout
recommended to place AppBarLayout
below other elements. However, it will still be at the top of the page.