How to disable CollapsingToolbar's collapse when scroll has not content?

Earwin delos Santos picture Earwin delos Santos · Sep 14, 2015 · Viewed 19.3k times · Source

I am using android support design 'com.android.support:design:22.2.1' my problem is that when the scroll has no content in it, the collapsing toolbar still enables the collapse action. I need to remove the collapsing of the view if the scroll has no content in it.

My 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.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:background="@android:color/white"
                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"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:expandedTitleMarginEnd="64dp"
                    app:expandedTitleMarginStart="48dp"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <fragment
                        android:id="@+id/pawfile_header"
                        android:name="com.lightbulb.pawesome.fragments.PawfileHeaderFragment"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="10dp"
                        android:fitsSystemWindows="true"
                        app:layout_collapseMode="parallax" />

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

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

            <fragment
                android:id="@+id/pawfile_timeline"
                android:name="com.lightbulb.pawesome.user_timeline.PawesomeUserTimelineFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

UDPATE QUESTION:

I'm done with disabling and enabling the collapsing of the view. I used this line of code in order to disable the collapsing:

AppBarLayout.LayoutParams appbarParams = (AppBarLayout.LayoutParams)     collapsingToolbar.getLayoutParams();
appbarParams.setScrollFlags(0);
collapsingToolbar.setLayoutParams(p);

The problem is that the appbar is force to have the elevation of the scrollflags is set to 0. I tried using appbar.setElevation(0) but it has no effect. how can I remove the elevation from my appbar?

Answer

rKrishna picture rKrishna · Sep 14, 2015

get your appbarlayout reference and set the setScrollFlags.

AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
p.setScrollFlags(0);
toolbar.setLayoutParams(p);