0

Just Like my title says, I was trying to hide my toolbar dynamically when I scroll down the RecycledView inside of a fragment by using the coordinator layout. But it is not working. I think it is because something wrong with my XML file. I will be really grateful if someone helps me.

Here is my fragment XML file, this fragment is contained inside a tab layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jinyu.jiaodian.MainActivity"
>

<android.support.v7.widget.RecyclerView
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    />
<android.support.design.widget.FloatingActionButton
    android:id="@+id/upload_a_word"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_input_add"
    android:layout_alignParentRight="true" />

Here is my app_bar layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout       xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.jinyu.jiaodian.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include
    layout="@layout/tab_layout"/>

Ilovejavaing
  • 3
  • 1
  • 3
  • Possible duplicate of [android lollipop toolbar: how to hide/show the toolbar while scrolling?](http://stackoverflow.com/questions/26539623/android-lollipop-toolbar-how-to-hide-show-the-toolbar-while-scrolling) – Rishabh Maurya Feb 12 '17 at 03:33

1 Answers1

2

Put app:layout_behavior="@string/appbar_scrolling_view_behavior" on your include, not on the AppBarLayout. That behavior is what propagates nested scrolling events to your AppBarLayout.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Hi, thanks for answering me, I took your suggestion. The toolbar hides automatically when I scroll down my recycledView, but If I scroll up, my toolbar covers the two tabs of the tab layout. I update the XML code for the app bar layout base on your suggestion, could you please take a took thanks. – Ilovejavaing Feb 13 '17 at 05:06
  • `app:layout_behavior="@string/appbar_scrolling_view_behavior"` needs to be on a direct child view of the `CoordinatorLayout`, not on the `RecyclerView` contained within a `RelativeLayout`. – ianhanniballake Feb 13 '17 at 05:11
  • Thanks man, you solved my problem. I really really really appreciate it!!! – Ilovejavaing Feb 13 '17 at 05:52