Search code examples
androidandroid-layoutandroid-appbarlayoutandroid-collapsingtoolbarlayout

Android parallax effect CollapsingToolbarLayout with ViewGroup Layout is not working


I am developing an Android app. In my app, I am using CollapsingToolbarLayout for parallax effect. Before I used AppbarLayout, CollapsingToolbarLayout together with ViewPager, I was working fine. But this time, I replaced view holder for content with RelativeLayout. So when I scroll up from the content, toolbar is not collapsed. I mean when I scroll up from the position I circled in the screenshot below.

enter image description here

But if I scroll up from the toolbar area, it is working. I mean the area in the screenshot.

enter image description here

This is my XML layout

<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:background="@android:color/background_light"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/ai_app_bar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            >

           <RelativeLayout
               android:background="@drawable/item"
               app:layout_collapseMode="parallax"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
              <View
                  android:background="@color/lightGray"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"></View>
           </RelativeLayout>

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


    <RelativeLayout
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:text="This is testing"
            android:textColor="@color/textColorPrimary"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/ai_review_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@android:drawable/btn_star"
        android:layout_margin="10dp"
        android:visibility="gone"
        app:layout_anchor="@id/ai_app_bar"
        app:layout_anchorGravity="bottom|right|end"
        />
</android.support.design.widget.CoordinatorLayout>

Why toolbar is not collapsed when I scroll up from the relative layout area? How can I fix that to be the toolbar collapsed if I scroll up from the RelativeLayout area?


Solution

  • You should use RecyclerView instead of RelativeLayout outside of the AppBarLayout and add this view tag to it app:layout_behavior="@string/appbar_scrolling_view_behavior"

    string/appbar_scrolling_view_behavior maps to AppBarLayout.ScrollingViewBehavior, which is used to notify the AppBarLayout when scroll events occur on this particular view.

    From here Handling Scrolls with CoordinatorLayout

    Edit

    Also, a CollapsingToolbar needs RecyclerView or NestedListView to work.