[英]Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment
我有一個帶有兩個標簽的活動。 每個選項卡都包含一個片段,其中包含SwipeRefreshLayout
和RecyclerView
。
在活動,我有一個CoordinatorLayout
與AppBarLayout
(具有Toolbar
和TabLayout
)和ViewPager
的片段。
現在,我要實現的是:當用戶滾動片段時,工具欄(而不是選項卡)會隱藏,就像在Play商店中一樣。
在我通過Internet閱讀的示例中,布局非常簡單:它們在同一xml的CoordinatorLayout
具有RecyclerView
和Toolbar
。
然后,他們只寫:
<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>
提前致謝。
伙計,我遇到了同樣的問題。 問題不在代碼中。 我敢打賭您正在使用舊版本的構建工具和庫。 將它們更新為最新版本:
就我而言,這就像一種魅力! 祝好運!
對於像我一樣在Fragment中使用Scrollview的每個人,我建議您使用android.support.v4.widget.NestedScrollView。 這樣,工具欄和/或TabLayout將與Scrollview一起滾動
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.