android - 如何在 recyclerView 向下滚动时折叠后隐藏工具栏

标签 android android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout android-support-design

如何在recyclerView向下滚动时collapse后隐藏toolbar,recyclerView向上滚动时显示toolbar,在列表末尾展开CollapsingToolbarLayout?现在 CollapsingToolbarLayout 只是折叠,工具栏在滚动时一直显示。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".screens.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        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"
            android:minHeight="@dimen/actionBarHeight"
            app:contentScrim="@color/colorPrimary"
            app:expandedTitleTextAppearance="@style/TransparentText"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/toolbar_image"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:adjustViewBounds="true"
                android:background="#229944"
                android:contentDescription="@null"
                android:scaleType="fitCenter"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/actionBarHeight"
                android:minHeight="@dimen/actionBarHeight"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            app:layout_scrollFlags="scroll" />

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

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

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

最佳答案

这应该可以完美运行。在 API 17 中测试和工作

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/careers_coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".activity.CareersActivity"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        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="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

            <ImageView
                android:id="@+id/background"
                android:layout_width="match_parent"
                android:layout_height="256dp"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                android:src="@drawable/placeholder"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:contentScrim="?attr/colorPrimary"
                android:fitsSystemWindows="true"
                app:titleTextColor="@color/main_color_white"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"/>

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextColor="@android:color/white"
            app:tabIndicatorHeight="4dp"
            app:tabMode="fixed"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

关于android - 如何在 recyclerView 向下滚动时折叠后隐藏工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34353845/

相关文章:

android - 展开折叠工具栏的透明工具栏

android - AppBarLayout 具有滚动行为替代/滚动粘滞 View /大高度 AppBarLayout 导致奇怪的滚动

javascript - 在Android设备中,我们如何在通过浏览器上传之前压缩视频?

java - Recyclerview onCreateViewHolder 被调用尽管所有 View 都被回收?

android - 如何在 Android 中更改偏好类别的文本颜色?

java - 使用混淆器时选取框工具栏标题不起作用

java - 如何以编程方式更改工具栏文本的颜色

android - 修复了带有 CoordinatorLayout 的 header 在滚动时显示阴影

android - 即使嵌套 ScrollView 中没有滚动内容,折叠工具栏布局也会滚动

javascript - 在 Android WebView 上阻止 JS deviceorientation devicemotion