Search code examples
androidandroid-coordinatorlayout

CoordinatorLayout scroll doesn't work unless Toolbar has layout_scrollflags:scroll


I'm trying to have an AppBarLayout scroll only one of its children, and not scroll the ToolBar within it.

However, when I remove layout_scrollflags=scroll from the Toolbar, nothing scrolls.

Is it possible to have only one view in an AppBarLayout scroll but not the Toolbar?

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


            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?actionBarSize"
                    android:theme="@style/BarTheme"
                    app:title="@string/my">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center|start"
                        android:src="@{ViewModel.showingDropdown ? @drawable/ic_arrow_drop_up_white_24dp : @drawable/ic_arrow_drop_down_white_24dp}"
                        android:tint="@color/ring_blue"
                        android:visibility="@{ViewModel != null ? View.VISIBLE : View.GONE}" />
                </android.support.v7.widget.Toolbar>

                <FrameLayout
                    android:id="@+id/widget_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_scrollFlags="scroll|enterAlways" />
            </android.support.design.widget.AppBarLayout>

The result of that code is that nothing scrolls, but when I add layout_scrollflags=scroll to the toolbar, both the Toolbar and the FrameLayout scroll, which I don't want


Solution

  • use collapsing toolbar layout...for this.. and make the toolbar collapse_mode = "pin" and others collapse_mode = parallax the layouts that you want to scroll for reference check https://antonioleiva.com/collapsing-toolbar-layout/

    • Keep the parent layout of collapsing toolbar layout as appBarlayout..

    • Assign scroll flags to it..

    • Define the child layouts of the collapsing toolbar layout with their desired collapse_modes (parallax,pin..etc etc )