android - 当使用协调器布局滚动我的 recyclerview 时,如何提供自动隐藏/显示工具栏?

标签 android android-toolbar android-coordinatorlayout android-appbarlayout

这是我的布局文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.moskart.mosfake.activity.MainActivity">

   <android.support.design.widget.CoordinatorLayout
       android:id="@+id/coordinatorLayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <android.support.design.widget.AppBarLayout
           android:layout_width="match_parent"
           android:layout_height="?attr/actionBarSize"
           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:minHeight="?attr/actionBarSize"
               android:fitsSystemWindows="true"
               app:contentScrim="?attr/colorPrimary"
               app:layout_scrollFlags="scroll|enterAlways"
               >

               <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                   xmlns:local="http://schemas.android.com/apk/res-auto"
                   android:id="@+id/toolbar"
                   android:layout_width="match_parent"
                   android:layout_height="?attr/actionBarSize"
                   android:minHeight="?attr/actionBarSize"
                   android:background="?attr/colorPrimary"
                   local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                   local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                   app:layout_collapseMode="pin"
                   />
           </android.support.design.widget.CollapsingToolbarLayout>

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


       <FrameLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"
           >

           <FrameLayout
               android:id="@+id/fragment_placeholder"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
           </FrameLayout>

           <View
               android:layout_width="match_parent"
               android:layout_height="5dp"
               android:background="@drawable/toolbar_dropshadow" />

       </FrameLayout>


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

   <android.support.design.widget.NavigationView
       android:id="@+id/navigation"
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       app:headerLayout="@layout/navigation_drawer_header"
       app:itemIconTint="@color/navItemIconTintColor"
       app:itemTextColor="@color/navItemTextColor"
       app:menu="@menu/menu_navigation_drawer" />

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

我将 CollapsingToolbarLayoutapp:layout_scrollFlags 设置为 scroll|enterAlways 并希望工具栏像在 gapps 中一样工作(例如 Google Play 或Google Magazine),像这个示例一样自动隐藏/显示:

Google Magazine

但我只有滚动工具栏不会自动缩回,但前提是用户自己完全滚动它。参见示例:my app

这是我的 recyclerview fragment ,我将其替换为 fragment_placeholder

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.moskart.mosfake.fragment.CategorySelectionFragment"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/card_horizontal_margin"
        android:paddingRight="@dimen/card_horizontal_margin"
        />

</LinearLayout>

多谢指教!

最佳答案

您应该测量 AppBarLayout 的底部和顶部偏移是否偏移超过 AppBarLayout 高度的一半,并且用户已释放滚动。当满足此条件时,只需开始为 Toolbar 的 translationY 或 AppBarLayout 的偏移设置动画。

这可以通过自定义 CoordinatorLayout.Behavor 或继承现有的 AppBarLayout.Behaviour 并对其进行调整来实现。

关于android - 当使用协调器布局滚动我的 recyclerview 时,如何提供自动隐藏/显示工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32377878/

相关文章:

android - 抽屉导航 Activity 中菜单项的 OnClickListener

android - 抽屉导航、工具栏和回收器在同一个 xml 布局文件中查看所有内容?

android - 在 CoordinatorLayout 中使用时,页脚会滚出屏幕

android - AppBarLayout 与 FrameLayout 容器作为滚动内容不起作用

java - Android 解析 Json 对 Java ArrayList 的响应

android - 导航 View :setCheckedItem not working for child item

java - Firebase Auth 在单击按钮时未启动 Activity 但在关闭并重新打开应用程序时有效

java - 如何将 XML 多次包含到填充不同数据的主 xml 中?

android - Appcompat 工具栏 "android:title"属性不起作用

android - 使用 fragment 内的recyclerview在滚动时隐藏工具栏