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

时间:2015-09-03 14:04:32

标签: 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>

我为我的app:layout_scrollFlags设置了CollapsingToolbarLayoutscroll|enterAlways,并期望该工具栏可以像gapps(例如Google Play或Google Magazine)一样使用此示例中的自动隐藏/显示:

Google Magazine

但是我只有滚动的工具栏不能自动缩回,但只有当用户自己完全滚动它时。请参阅示例:my app

这是我的使用recyclerview的片段,我将其替换为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>

感谢您的建议!

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

答案 2 :(得分:0)

您需要使用NestedScrollView

<android.support.v4.widget.NestedScrollView ...>
<LinearLayout ...>
    //Your Code Here
</LinearLayout>

选中此Example

相关问题