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

时间:2015-12-18 10:48:42

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

如何在recyclelerView向下滚动时折叠后隐藏工具栏,在recyclelerView向上滚动时显示工具栏,并在列表末尾展开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>

4 个答案:

答案 0 :(得分:2)

这应该完美。在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>

答案 1 :(得分:1)

您可能想要更改工具栏的行为。您可以通过更改layout_scrollFlags来提供滚动行为。

pin

删除{{1}}并在工具栏中进行此更改,它会起作用!

答案 2 :(得分:0)

  • android:fitsSystemWindows="true"
  • 中添加CoordinatorLayout
  • app:layout_scrollFlags="scroll|enterAlwaysCollapsed"移除Toolbar
  • TabLayout内移动CollapsingToolbarLayout

    <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:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    
            <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.CollapsingToolbarLayout>
    
    </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"/>
    

答案 3 :(得分:0)

对于CollapsingToolbarLayout,请使用滚动标记:

app:layout_scrollFlags="scroll|enterAllwaysCollapsed"

此标志组合将在向下滚动时消失工具栏。当您滚动到列表顶部时,CollapsingToolbarLayout将完全展开。

并且无需从app:layout_collapseMode="pin"中移除Toolbar,因为collapseMode标记用于定义CollapsingToolbarLayout内的视图的行为和位置,并且不会影响实际的折叠和展开CollapsingToolbarLayout