CollapsingToolbarLayout not working(collapsing)when scrolled

You should use RecyclerView instead of ListView

Note: don't forget to update RecyclerView in Gradle file.

  compile 'com.android.support:recyclerview-v7:22.2.0'

I did an example by using RecyclerView instead. The source code could be found here: https://github.com/jiahaoliuliu/MaterialDesignSample/tree/collapsingToolbars

There are a couple of things that you should take into account and the post does not says.

  1. Use CoordinatorLayout as the main layout

  2. Use a theme without ActionBar and set the toolbar as actionBar instead. You can do it by creating a special theme for the activity like this:

    <!-- Indigo without actionbar when toolbar is used -->
    <style name="IndigoWithoutActionBar" parent="Indigo">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    And in the AndroidManifest.xml file, do the follow:

    <activity
        android:name=".CollapsingToolbarActivity"
        android:label="@string/app_name"
        android:theme="@style/IndigoWithoutActionBar"
        >
    </activity>
    

    Once this is done, set it on the Java code:

    // Actionbar
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

Here is a functional xml code that I use.

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

        <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"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/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>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/simpleRecyclerView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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

If this is not enough for you, you can follow the code of Chris Banes here: https://github.com/chrisbanes/cheesesquare


For my case : I dint give height to my Toolbar .

It was wrap_content. If thats the case then try to fix the height of the Toolbar using :-

android:layout_height="?attr/actionBarSize"