Search code examples
androidandroid-coordinatorlayoutandroid-appbarlayout

Strange behavior of AppBarLayout child


I have the following layout:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="24dp"
            android:background="@color/someColor"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" >
        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <View
                android:layout_width="wrap_content"
                android:layout_height="56dp"/>
        </android.support.constraint.ConstraintLayout>

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

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

        <View
            android:layout_width="match_parent"
            android:layout_height="10000dp"
            android:minHeight="1000dp" />

    </android.support.v4.widget.NestedScrollView>

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

The first ConstraintLayout collapsing on scrolling just fine as expected. BUT if I change its height to 23dp, then it does not collapse at all. What causes it and why this limitation was applied?


Solution

  • You won't need two ConstraintLayout in there actually. However, the problem seems to be the second flag exitUntilCollapsed. If you're looking for collapsing on scroll, you can try adding:

    app:layout_scrollFlags="scroll|snap"
    

    Or:

    app:layout_scrollFlags="scroll|enterAlways"
    

    To your views.

    exitUntilCollapsed seems to be a best choice for using in CollapsingToolbarLayout.