简体   繁体   中英

Toolbar not collapsing on scroll in CoordinatorLayout

I am using the following layout file to implement a coordinator layout with collapsing toolbar (when the user scrolls down, it should collapse, and reappear when they scroll up) However, this scroll off screen/collapse functionality is not working, and I don't know why. Instead of reacting, the toolbar, and tab bar stay statically at the top of the screen.

<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">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <!--                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Alpha" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Beta" />
            <!-- a bunch of content just to make the view long enough to scroll -->
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Upsilon" />

        </LinearLayout>

    </ScrollView>


    <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="?attr/actionBarSize"
            android:background="?attr/colorAccent"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />
            <!--                    ^^^^^^^^^^^^^^^^^^ -->

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways" />
            <!--                    ^^^^^^^^^^^^^^^^^^ -->

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_padding"
        android:src="@drawable/ic_add"/>

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

Any help? Thanks!

As far as I know, ScrollView does not work. You can use NestedScrollView or RecyclerView instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM