Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 457
Vamsi Challa Avatar asked Dec 19 '22 03:12

Vamsi Challa


1 Answers

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>
like image 186
Joaquin Iurchuk Avatar answered Jan 23 '23 05:01

Joaquin Iurchuk