簡體   English   中英

當使用協調器布局滾動我的recyclerview時,如何提供自動隱藏/顯示工具欄?

[英]How can I provide auto-hide/show toolbar, when scroll my recyclerview with coordinator layout?

這是我的布局文件

<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_scrollFlagsscroll|enterAlways並期望工具欄將像gapps(例如Google Play或Google Magazine)一樣工作,並在此示例中使用自動隱藏/顯示:

谷歌雜志

但我只有滾動的工具欄不會自動縮回,但只有當用戶自己完全滾動它時。 請參閱示例: 我的應用

這是我的片段與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>

謝謝你的建議!

您應該測量AppBarLayout的底部和頂部偏移是否偏移超過AppBarLayout高度的AppBarLayout並且用戶已釋放滾動。 滿足此條件時,只需啟動Toolbar的translationY動畫或AppBarLayout的偏移AppBarLayout

這可以通過自定義CoordinatorLayout.Behavor或繼承現有的AppBarLayout.Behaviour並調整它來實現。

您需要使用NestedScrollView

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

檢查此示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM