CoordinatorLayout, AppBarLayout and ToolBar - Toolbar doesn't scroll off the screen

10.4k views Asked by At

I am trying to make my ToolBar scroll off the screen, when I scroll up my ViewPager(ViewPager is inside a fragment that is placed in FrameLayout) and show the ToolBar when I scroll down. I am trying to achieve this using the Support Design Library.

Here is my XML:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            android:background="?attr/colorPrimaryDark"
            android:elevation="4dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="@android:color/white"
                android:textSize="20sp" />

            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_gravity="right"
                android:indeterminate="true"
                android:visibility="gone" />
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/detail_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.xx.xxx.DetailActivity"
        tools:ignore="MergeRootFrame"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >
    </FrameLayout>

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

But, the toolbar doesn't respond to scroll. What am I doing wrong?

4

There are 4 answers

2
Joaquin Iurchuk On

You should have something like this:

<?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/main_content"
    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="@dimen/detail_backdrop_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/primary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/header"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/your_scrollable_view" />

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"
        android:onClick="registrarNuevoUsuario"
        android:src="@drawable/ic_action_check"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end" />

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

dimens.xml

<resources>
    <dimen name="fab_margin">16dp</dimen>
    <dimen name="detail_backdrop_height">256dp</dimen>
</resources>
4
Theo Kallioras On

If you just want the toolbar to enter/exit AppBarLayout is sufficient. You do not need a CollapsingToolbarLayout. Try changing your FrameLayout into a android.support.v4.widget.NestedScrollView

2
Amit Vaghela On

Similar issue was reported on google https://code.google.com/p/android/issues/detail?id=201822

But after release of Android Support Library, revision 23.2.1 (March 2016) This bug is solved.

Fixed : AppBarLayout not scrolling out completely when used with fitsSystemWindow

update Support Library to Android Support Library to 23.2.1 OR to further.

0
dekaru On

What do you have inside your ViewPager? A ListView or a RecyclerView? Either way you might want to consider adding the behaviour to that scrolling element.

app:layout_behavior="@string/appbar_scrolling_view_behavior"

and most importantly and as stated by this other SO answer, don't forget to update your build tools to 22 and your gradle references to at least v 22.20 of both the component and the Design Support Library:

compile 'com.android.support:design:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'

Check out this for a complete tutorial.