当我在使用coordinator layout和recyclerview的时候,如何提供自动隐藏/显示工具栏?

4
这是我的布局文件。
<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>

我为我的CollapsingToolbarLayout设置了app:layout_scrollFlags属性为scroll|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>

感谢您的建议!

搜索 ObservableScrollView,也许这就是你想要的... :) - Kirankumar Zinzuvadia
3个回答

1
你需要测量AppBarLayout的底部和顶部偏移是否超过了AppBarLayout高度的一半,并且用户已经释放了滚动。当满足此条件时,只需开始动画Toolbar的translationY或AppBarLayout的偏移即可。这可以通过自定义CoordinatorLayout.Behavor或继承现有的AppBarLayout.Behaviour并调整其实现来实现。

我在这里看到了一个类似的方法https://dev59.com/018d5IYBdhLWcg3wsT4b,但是我认为现在CoordinatorLayout已经提供了这个功能。 - Augusent
我应该为我的AppBarLayout或Toolbar创建自定义的CoordinatorLayout.Behavior,以及在layoutDependsOn中应该定义什么依赖关系?是CollapsingToolbarLayout吗?感谢您的回答! - Augusent

1

0
你需要使用NestedScrollView。
<android.support.v4.widget.NestedScrollView ...>
<LinearLayout ...>
    //Your Code Here
</LinearLayout>

请检查此示例


不幸的是,这个例子和我的情况一样。 - Augusent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接