简体   繁体   English

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

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

This is my layout file 这是我的布局文件

<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>

I set app:layout_scrollFlags for my CollapsingToolbarLayout to scroll|enterAlways and expected that toolbar will work like in gapps (eg Google Play or Google Magazine) with auto-hide/show like on this sample: 我为我的CollapsingToolbarLayout设置app:layout_scrollFlagsscroll|enterAlways并期望工具栏将像gapps(例如Google Play或Google Magazine)一样工作,并在此示例中使用自动隐藏/显示:

Google Magazine 谷歌杂志

But I only got scrolling toolbar that does not retract automatically, but only if user completely scrolled it himself. 但我只有滚动的工具栏不会自动缩回,但只有当用户自己完全滚动它时。 See sample: my app 请参阅示例: 我的应用

Here is my fragment with recyclerview, which I replace with fragment_placeholder 这是我的片段与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>

Thanks for advice! 谢谢你的建议!

You should measure if the bottom and top offset of the AppBarLayout is offset for more than the half the the height of the AppBarLayout and user has released the scroll. 您应该测量AppBarLayout的底部和顶部偏移是否偏移超过AppBarLayout高度的AppBarLayout并且用户已释放滚动。 When this condition is fulfilled just start animating the translationY of the Toolbar or the offsetting of the AppBarLayout . 满足此条件时,只需启动Toolbar的translationY动画或AppBarLayout的偏移AppBarLayout

This could be achieved with custom CoordinatorLayout.Behavor or with inheriting existing AppBarLayout.Behaviour and tweaking it to do so. 这可以通过自定义CoordinatorLayout.Behavor或继承现有的AppBarLayout.Behaviour并调整它来实现。

i think you want to do like this 我想你想这样做

在此输入图像描述

take a look of this library 看看这个图书馆

https://github.com/ksoichiro/Android-ObservableScrollView https://github.com/ksoichiro/Android-ObservableScrollView

here is the example about how to implement it 这是关于如何实现它的示例

https://snow.dog/blog/material-design-flexible-space-header-with-image/ https://snow.dog/blog/material-design-flexible-space-header-with-image/

You need to use NestedScrollView 您需要使用NestedScrollView

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

Check this Example 检查此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用协调器布局滚动recyclerview时无法隐藏工具栏 - Unable to hide toolbar when recyclerview is scrolled using Coordinator Layout 隐藏带有协调器布局和RecyclerView的工具栏 - Hide Toolbar with Coordinator layout and RecyclerView in a fragment 如何为我的Android应用程序实现类似“自动隐藏导航”的Chrome? - How can I implement a chrome like “auto-hide navigation” for my Android app? 工具栏在滚动不起作用时自动隐藏和显示 - toolbar auto hide and show when scroll not working ViewPager的列表向下滚动时,工具栏无法自动隐藏 - Toolbar can't auto-hide when viewpager's list scrolling down 如何在协调器布局中使用工具栏滚动和折叠LinearLayout - How to scroll and collapse a LinearLayout with Toolbar in Coordinator Layout 通过协调器布局以编程方式隐藏工具栏 - hide toolbar programmatically with coordinator layout 无法滚动 RecyclerView,尝试在滚动时隐藏工具栏 - Can't scroll RecyclerView, when try to hide toolbar on scrolling 当我在“layout.xml”文件中添加工具栏时,Android Studio RecyclerView 不会显示我的数据 - Android Studio RecyclerView wont show my data when i add toolbar at my `layout.xml` file 当我向上滚动 RecyclerView 时,工具栏和 TabLayout 不会隐藏 - Toolbar and TabLayout don't hide when I scroll the RecyclerView up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM