简体   繁体   English

使用CoordinatorLayout隐藏工具栏,但在片段上使用RecyclerView

[英]Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment

I have an Activity with two tabs. 我有一个带有两个标签的活动。 Each tab contains a fragment with a SwipeRefreshLayout and a RecyclerView inside them. 每个选项卡都包含一个片段,其中包含SwipeRefreshLayoutRecyclerView

In the Activity, I have a CoordinatorLayout with an AppBarLayout (with a Toolbar and a TabLayout ) and a ViewPager for the Fragments. 在活动,我有一个CoordinatorLayoutAppBarLayout (具有ToolbarTabLayout )和ViewPager的片段。

Screenshot 屏幕截图

Now, what I want to achieve is: when the user scrolls in the fragments, the Toolbar, and not the tabs, hides, like in the Play Store. 现在,我要实现的是:当用户滚动片段时,工具栏(而不是选项卡)会隐藏,就像在Play商店中一样。

In the examples that I have read over the Internet, the layout is very simple: they have the RecyclerView and the Toolbar inside the CoordinatorLayout on the same xml. 在我通过Internet阅读的示例中,布局非常简单:它们在同一xml的CoordinatorLayout具有RecyclerViewToolbar

Then, they just write: 然后,他们只写:

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvToDoList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

And

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>

So, I can't figure out how to do it. 因此,我不知道该怎么做。

My xml's are the following: 我的xml如下:

The Activity Layout is: 活动布局为:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/activity_main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/color_primary"
                app:layout_scrollFlags="scroll|enterAlways"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/activity_main_tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabIndicatorColor="@color/color_text_primary"
                app:tabMode="fixed" />

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

        <android.support.v4.view.ViewPager
            android:id="@+id/activity_main_tabs_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


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

<ListView
    android:id="@+id/activity_main_nav_drawer_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/color_primary"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="5dp"
    android:overScrollMode="never"
    android:smoothScrollbar="true" />
</android.support.v4.widget.DrawerLayout>

And the fragments are: 片段是:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_coupons_swipe_refresh_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.github.yasevich.endlessrecyclerview.EndlessRecyclerView
    android:id="@+id/fragment_coupons_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>

Thanks in advance. 提前致谢。

Man, I faced the same problem. 伙计,我遇到了同样的问题。 The problem is not in the code. 问题不在代码中。 I bet you are using old versions of the build tools and the libraries. 我敢打赌您正在使用旧版本的构建工具和库。 Update them to the latest: 将它们更新为最新版本:

  • buildToolsVersion "22.0.1" buildToolsVersion“ 22.0.1”
  • com.android.support:appcompat-v7:22.1.1 com.android.support:appcompat-v7:22.1.1
  • com.android.support:recyclerview-v7:22.2.0 com.android.support:recyclerview-v7:22.2.0

In my case this worked like a charm! 就我而言,这就像一种魅力! Good luck! 祝好运!

For everyone using a Scrollview in a Fragment, like me, I suggest you to use android.support.v4.widget.NestedScrollView. 对于像我一样在Fragment中使用Scrollview的每个人,我建议您使用android.support.v4.widget.NestedScrollView。 In that way, Toolbar and/or TabLayout will scroll together with Scrollview 这样,工具栏和/或TabLayout将与Scrollview一起滚动

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM