CollapsingToolbarLayout and NestedScrollView not working

Eugene H picture Eugene H · Jul 4, 2015 · Viewed 12.3k times · Source

I am trying to implement CollapsingToolbarLayout with a NestedScrollView and it is displaying the TextView within the NestedScrollView at the bottom and not allowing, scrolling or collapsing the Toolbar. I have gotten this to work with a RecyclerView but not NestedScrollView. When I remove app:layout_behavior="@string/appbar_scrolling_view_behavior the Toolbar collapses but the NestedScrollView is not below the AppBarLayout. Any solutions or suggestions to fix this?

XML

<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:fitsSystemWindows="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Hello"
                android:textColor="#000"
                android:textSize="16sp"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <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"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="134dp"
                android:background="@color/primary"
                app:layout_collapseMode="parallax"/>

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

Result

enter image description here

Answer

Psypher picture Psypher · Jul 5, 2015

Change to some certain height in the AppbarLayout. Example:

android:layout_height="300dp". 

The main problem being, the nested scroll view does not have enough views to cause a scroll. Hence the parallax effect would not work.

Here is a working example that uses NestedScrollView and CollapsingToolbarLayout