Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment
C

2

8

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

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

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.

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.

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:

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.

Cloudscape answered 10/7, 2015 at 16:52 Comment(8)
Remove the LinearLayoutsBrittne
And app:layout_behavior="@string/appbar_scrolling_view_behavior" on the ViewPager, not the SwipeRefreshLayoutBrittne
Nope, didn't work :(. Thanks for the comment.Cloudscape
Can you update your code and repost it.Brittne
LinearLayouts is plural because I was referring to the ones in your fragment layout and activity layout.Brittne
Sorry, didn't saw that. Let me check.Cloudscape
I just updated the code. Still not working. Thansk for your timeCloudscape
Well, I don't see any other problems. Sorry.Brittne
B
3

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"
  • com.android.support:appcompat-v7:22.1.1
  • com.android.support:recyclerview-v7:22.2.0

In my case this worked like a charm! Good luck!

Burnish answered 11/7, 2015 at 10:23 Comment(3)
@LucasDaddiego, did you managed to try my solution? If it helped you, may I ask you to accept my answer as correct :)Burnish
Glad to know it helped :)Burnish
This solved my needs aswell! Just a tip for everybody who might be doing it wrong like me, this wiz-trick won't work if you (like me) are using a ListView instead of a RecyclerView. As soon as I switched, everything went fine.Onondaga
M
2

For everyone using a Scrollview in a Fragment, like me, I suggest you to use android.support.v4.widget.NestedScrollView. In that way, Toolbar and/or TabLayout will scroll together with Scrollview

Massimo answered 16/1, 2016 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.