CoordinatorLayout layout_anchor does not work
Asked Answered
M

1

16

I just tried out the new Support Design Library with CoordinatorLayout, but I'm having problems anchoring child views. My layout currently 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:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="enterAlways" />

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_anchor="@id/toolbar"
    app:layout_anchorGravity="bottom"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:src="@drawable/ic_fab_add"
    android:onClick="onAddPlayerClick"
    android:layout_gravity="bottom|end"
    app:elevation="4dp"
    app:borderWidth="0dp"/>

When the RecyclerView list is loaded, the first item is being overlapped by the action bar. I assume the app:layout_anchor is not working properly. I already tried anchoring it to appbar to no success.

Does anyone know what I am doing wrong here?

Marchioness answered 5/6, 2015 at 20:44 Comment(0)
R
22

Try this code. You need to use app:layout_behavior in your RecyclerView

<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.FloatingActionButton
    android:id="@+id/fab"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:src="@drawable/ic_fab_add"
    android:onClick="onAddPlayerClick"
    app:elevation="4dp"
    app:borderWidth="0dp"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    android:clickable="true"/>
Risible answered 6/6, 2015 at 5:21 Comment(8)
Ah did I really miss that?... Thank you very much.Problem solved.Marchioness
What is the value of @string/appbar_scrolling_view_behavior and why does adding this behavior work?Grasmere
I want to know this as wellVivi
@McAdam331 android.support.design.widget.AppBarLayout$ScrollingViewBehavior. Check out that inner class!Parricide
@Eenvincible: see comment aboveParricide
AppBarLayout$ScrollingViewBehavior is a behavior that will automatically offset a view to place it below the AppBarLayout. It only works with an AppBarLayout.Afterworld
if i have a text view above the recyclerview. how would i put the recyclerview below the textview ???Turban
I think the original code wouldn't work for another reason since anchoring works only for the views that stays at the same level in the view hierarchy (but it's also fixed in the solution).Fumigator

© 2022 - 2024 — McMap. All rights reserved.