RGS
RGS

Reputation: 4253

fix toolbar top and header below

I have a header and a toolbar after it. When scroll up the header the toolbar will be fix on top.

The problem is, I want the toolbar before header, on top of my activity, and when I scroll the header up the toolbar will only change its color.

toolbar
header (when scroll, change toolbar color)

any ideas how can I add the toolbar before header, on top of my activity and change its color when scroll up header?

<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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <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="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout android:id="@+id/header"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll">


        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="0dp"
            android:id="@+id/imageViewProfile"
            android:adjustViewBounds="true"
            android:cropToPadding="false" />

        </RelativeLayout>


            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="?attr/colorPrimaryDark" />


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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:scrollbars="none"
        android:layout_alignParentLeft="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

Upvotes: 0

Views: 540

Answers (1)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Try 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/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="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="220dp"
                android:background="@color/colorAccent"
                app:layout_collapseMode="parallax">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:padding="0dp"
                    android:id="@+id/imageViewProfile"
                    android:adjustViewBounds="true"
                    android:cropToPadding="false"
                    android:layout_centerHorizontal="true"
                    android:src="@mipmap/ic_launcher"/>

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="?attr/colorPrimaryDark" />
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <!--
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:scrollbars="none"
        android:layout_alignParentLeft="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
        -->
    <include layout="@layout/content_scrolling"></include>
</android.support.design.widget.CoordinatorLayout>

OUTPUT

enter image description here

Hope this will help~

Upvotes: 1

Related Questions