2

As the title suggests, I have an activity that has a FrameLayout that houses my Fragments [I am NOT using a Tabs, just Transactions]. I also have a NavigationDrawer and Toolbar. All of these things work fine.

The issue is that I'm trying to add a CollapsingToolbarLayout to the Activity layout and have RecyclerView in the Fragment scroll and control the collapsing of the CollapsingToolbarLayout.

With the setup shown below, I'm able to use the NavigationDrawer, see the Toolbar and the expanded CollapsingToolbarLayout with NO image inside (odd issue there). I load the Fragment into the FrameLayout which contains a RecyclerView with 10 test views. This scrolls fine below the CollapsingToolbar but it does not collapse.

Activity Layout XML

<android.support.v4.widget.DrawerLayout
    android:id="@+id/dl_drawer_layout"
    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:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cl_dashboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/ctb_dashboard"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/the_color"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/iv_dashboard_header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/the_image"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax"/>

                <include
                    android:id="@+id/toolbar"
                    layout="@layout/overlay_toolbar"
                    app:layout_collapseMode="pin"/>

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

        <FrameLayout
            android:id="@+id/fl_dashboard_fragmentframe"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        </FrameLayout>

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nv_navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/overlay_drawer_header"
        app:menu="@menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>

Fragment Layout XML

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_fragment_dashboardhome"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</android.support.v7.widget.RecyclerView>

I don't understand why this isn't working and I've run through a lot of documentation and examples with no luck. Any help or suggestion would be greatly appreciated.

AnxGotta
  • 1,006
  • 7
  • 28

2 Answers2

2

It turned out that the layout I was using for the Toolbar did not have the correct height value.

overlay_toolbar.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    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="wrap_content"
    android:background="@color/get_blue"
    android:elevation="5dp"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/actionbar_getblue">

</android.support.v7.widget.Toolbar>

Adjustment to overlay_toolbar include call in Activity XML

<include
    android:id="@+id/toolbar"
    layout="@layout/overlay_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_collapseMode="pin"/>

After that single adjustment to the height the CollapsingToolbarLayout began working perfectly.

AnxGotta
  • 1,006
  • 7
  • 28
1

app:layout_behavior should be applied to RecyclerView or any other View capable of nested scrolling such as NestedScrollView.

See this tutorial from codepath: https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

You can use app:layout_behavior="@string/appbar_scrolling_view_behavior" on RecyclerView you have in fragment.

Prakash
  • 4,479
  • 30
  • 42
  • I removed app:="@string/appbar_scrolling_view_behavior" from the FrameLayout in the Activity and added it to the RecyclerLayout in the Fragment with no luck. I then tried leaving app:="@string/appbar_scrolling_view_behavior" in the FrameLayout in the Activity and added it to the Fragment RecyclerView. Unfortunately, both of these resulted in the same, non-scrolling behavior. – AnxGotta Oct 14 '15 at 19:04
  • http://android-developers.blogspot.com/2015/05/android-design-support-library.html says: app:layout_collapseMode="pin" to ensure that the Toolbar itself remains pinned to the top of the screen while the view collapses. Not sure if that is a problem. You can check "Creating Parallax Animations" section from https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout#creating-parallax-animations – Prakash Oct 15 '15 at 00:20
  • 1
    I discovered the issue, I appreciate your input but I have used all these resources and the fix was due to the Toolbar height value. – AnxGotta Oct 15 '15 at 12:25