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

8

我有一个带有两个选项卡的活动。每个选项卡中都包含一个带有SwipeRefreshLayoutRecyclerView的片段。

在活动中,我有一个带有AppBarLayout(带有ToolbarTabLayout)和ViewPager用于Fragments的CoordinatorLayout

截图

现在,我想实现的是:当用户在片段中滚动时,工具栏而不是选项卡会隐藏,就像在Play Store中一样。

在我所阅读过的示例中,布局非常简单:它们在同一个xml中将RecyclerViewToolbar放置在CoordinatorLayout内。

然后,他们只需编写:

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

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

所以,我无法弄清楚如何做到这一点。

我的XML文件如下:

活动布局如下:

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

并且这些片段是:

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

提前致谢。


移除 LinearLayout - tachyonflux
еңЁViewPagerдёҠдҪҝз”Ёapp:layout_behavior="@string/appbar_scrolling_view_behavior"пјҢиҖҢдёҚжҳҜSwipeRefreshLayoutгҖӮ - tachyonflux
你能否更新你的代码并重新发布吗? - tachyonflux
抱歉,我没看到。让我检查一下。 - Lucas Daddiego
好的,我没有看到其他问题。抱歉。 - tachyonflux
显示剩余3条评论
2个回答

3

兄弟,我也遇到了同样的问题。问题不在代码上。我敢打赌你在使用旧版的构建工具和库文件。请将它们更新为最新版本:

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

在我的情况下,这个方法非常有效!祝你好运!


@LucasDaddiego,你试过我的解决方案了吗? 如果它有帮助到你的话,可以请你将我的答案标记为正确吗 :) - Kiril Aleksandrov
很高兴知道它有所帮助 :) - Kiril Aleksandrov
1
这也解决了我的需求! 给那些可能像我一样做错了的人一个提示,如果你(像我一样)使用的是ListView而不是RecyclerView,这个技巧就不起作用了。 当我切换到RecyclerView后,一切都很好。 - FrancescoC

2

如果你像我一样在Fragment中使用ScrollView,我建议你使用android.support.v4.widget.NestedScrollView。这样,Toolbar和/或TabLayout将与ScrollView一起滚动。


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