shagi
shagi

Reputation: 663

Scrolling ToolBar Android

In TabLayout I have 2 tabs, where in first placed FrameLayout with recyclerView and in second tab there ScrollView with LinerLayout in it. And I need to hide ToolBar when i scroll in any tab. When i scroll RecyclerView in first tab toolbar scrolls as well, but when i scroll in second tab it is not. Can't uderstand why.

I've got this main_acrivity.xml

<FrameLayout 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:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <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.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:tabIndicatorColor="@android:color/background_light"
                app:tabSelectedTextColor="@android:color/background_light"
                app:tabTextColor="@android:color/background_light"/>

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



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

</FrameLayout>

Upvotes: 1

Views: 657

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

The new scrolling behavior will not work with a regular android.widget.ScrollView, as it doesn't support nested scrolling.

Scroll behaviors rely on Views that support nested scrolling, which is needed to propagate scroll events up the View tree so that the Toolbar knows when to slide up and hide.

The reason it works with the RecyclerView is that it supports nested scrolling by default.

What you need is a NestedScrollView:

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

So, in your layout that has the ScrollView, replace it with android.support.v4.widget.NestedScrollView, and the scrolling view behavior will work as expected:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- your content here...... -->

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

Upvotes: 2

Related Questions