marco marco - 27 days ago 9
Android Question

Scrolling RecyclerView behind a AppBarLayout

I'm trying to achieve an effect like the videos in the "behavior section" at this URL ( Google Design official site ). It's the same effect of Google Photo, I guess, with a search bar on the top of the RecyclerView that disappear/appear on scrolling up/down

I don't know how to get it, I started with CoordinatorLayout and AppBarLayout, but I can't get the RecyclerView visible behind the AppBarLayout, it always remains white, covering the RecyclerView as show below ( result and code [edit: added image with layout layers] ).

Or it's a custom CoordinatorLayout.Behavior ?

Any help or suggestion would be appreciated, thank you

result

<?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: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.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways"
android:background="@android:color/transparent">

<SearchView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/holo_green_light"
android:layout_marginTop="40dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"/>

</android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>


Sample layout

enter image description here

Answer

It is definitely a custom CoordinatorLayout.Behavior. After some searches and readings ( most important, some inspirations, and the foundamentals ), I got what I wanted with the following layout

<?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: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"
        android:clipToPadding="false"
        android:paddingEnd="@dimen/activity_horizontal_margin"
        android:paddingStart="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/grid_spacing_header"/>

    <SearchView
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/searchbar_top"
        app:layout_behavior=".SearchBarBehavior"/>

</android.support.design.widget.CoordinatorLayout>

with my own SearchBarBehavior that extends CoordinatorLayout.Behavior [code still incomplete]