Hide toolbar with CoordinatorLayout but RecyclerView on fragment

I have an Activity with two tabs. Each tab contains a snippet with SwipeRefreshLayout and a RecyclerView inside them.

In Activity, I have a CoordinatorLayout with an AppBarLayout (with Toolbar and a TabLayout ) and a ViewPager for fragments.

Screenshot

Now I want to achieve: when the user scrolls through the fragments, the toolbar, not the tabs, is hidden, as in the Play Store.

In the examples I read over the Internet, the layout is very simple: they have a RecyclerView and Toolbar inside CoordinatorLayout in the same xml.

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

My xml:

Action Layout:

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

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

+8
android android-fragments android-tablayout android-design-library android-toolbar
source share
2 answers

Man, I faced the same problem. The problem is not in the code. I bet you use old versions of build tools and libraries. Update them to the latest version:

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

In my case, it worked like a charm! Good luck

+3
source share

For everyone who uses Scrollview in the Fragment, like me, I suggest you use android.support.v4.widget.NestedScrollView. Thus, the toolbar and / or TabLayout will scroll along with Scrollview

+2
source share

All Articles