我有一个有两个标签的活动。每个选项卡都包含一个片段,其中包含一个SwipeRefreshLayout
和一个RecyclerView
。
在活动中,我有一个CoordinatorLayout
和一个AppBarLayout
(有一个Toolbar
和一个TabLayout
)片段。
Screenshot
现在,我想要实现的是:当用户在片段中滚动时,工具栏而不是选项卡会像在play store中那样隐藏。
在我通过Internet阅读的示例中,布局非常简单:它们在同一XML上的ViewPager
和RecyclerView
中都有Toolbar
和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>
提前谢谢。
最佳答案
伙计,我也面临同样的问题。问题不在代码中。我敢打赌您使用的是构建工具和库的旧版本。更新到最新版本:
buildtoolsversion“22.0.1”版本
支持:appcompat-v7:22.1.1
支持:recyclerview-v7:22.2.0
在我看来,这是一种魅力!祝你好运!