1

So, I have this main activity layout using CoordinatorLayout to display a toolbar and tabs below it:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="0dp"
            local:layout_scrollFlags="scroll|enterAlways"
            local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            local:tabMode="fixed"
            local:tabGravity="fill"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foreground="@drawable/header_shadow"
        local:layout_behavior="@string/appbar_scrolling_view_behavior"  />

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

Then I have 2 fragment layouts, each for each of the 2 tabs I'll have. One fragment displays a ListView on its content:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@drawable/header_shadow"
    tools:context=".StationsFragment">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <ListView
            android:id="@+id/stations_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>

</FrameLayout>

I want my toolbar to hide when I scroll the listview above, and it should be ok with the code I have, at least from what I read in this answer, but it's not working. At the moment I've tried to remove the FrameLayout from the fragment activity but that didn't solve the issue. I've considered hard coding the scroll event from the listview but if possible I really want this to be working with no code behind..

Thanks in advance.

Community
  • 1
  • 1
yat0
  • 967
  • 1
  • 13
  • 29

2 Answers2

2

This is because you are using CoordinatorLayout with ListView. You can change your implementation to RecyclerView to achieve correct scroll.

check my answer here. This may help you.

Community
  • 1
  • 1
sha
  • 1,410
  • 2
  • 18
  • 37
  • so it is impossible to achieve this with `ListView` unless hard coding the scroll event, right? I'm using ListView because I've never worked with the `RecyclerView`, but it seems I'll have to use it.. – yat0 Dec 21 '15 at 23:35
  • Thats true. we have to move on to `RecyclerView` to take advantage of the features provided along with `toolbar`. This scroll effect only works with children of `NestedScrollingChild` – sha Dec 21 '15 at 23:45
  • Cool :). Happy coding – sha Dec 22 '15 at 00:00
0

try change from

local:layout_scrollFlags="scroll|enterAlways"

to

local:layout_scrollFlags="scroll|exitUntilCollapsed"
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • try adding nested scroll to your fragment. Check my edit – Deepak Goyal Dec 21 '15 at 23:38
  • 1
    collapse toolbar layout only works with nested scroll. So you must have to use recycler view instead of list view. Recycler view has inbuilt nested scrolling.... – Deepak Goyal Dec 21 '15 at 23:49
  • Alright, I understand.. Since @sha stated this before you I'll accept his answer, but thanks for your help, I appreciate it. – yat0 Dec 21 '15 at 23:57