Lampione
Lampione

Reputation: 1608

ViewPager scroll layout behavior not working with Toolbar

I'm dealing with something strange related to ViewPager, AppBarLayout and Toolbar.

In my layout, I've got a standard Material Design setup, such as:

<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context="...">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/grey_900"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabBackground="@color/grey_900"
            app:tabIndicatorColor="@color/white_1000"
            app:tabIndicatorHeight="4dp" />

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

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        ...
        app:layout_behavior="com.inkstinctapp.inkstinct.FabBehavior" />

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

My ViewPager contains 4 fragments with this layout:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <me.zhanghai.android.materialprogressbar.MaterialProgressBar
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:indeterminate="true"
        android:tint="@color/white_1000"
        android:layout_centerInParent="true" />

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

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

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

</RelativeLayout>

I can't get the Toolbar to scroll, I've tried every combination of:

app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|enterAlways"

None of them seems to work except if I remove the appbar_scrolling_view_behavior from my viewpager, which is not ok because the viewpager goes under the AppBarLayout.

What am I doing wrong? Any help would be really appreciated!

Upvotes: 5

Views: 9566

Answers (4)

hamid
hamid

Reputation: 79

you should use Coordinate layout as parent layout to have access to (app:layout_behavior ) in your view pager .

Upvotes: 0

sud007
sud007

Reputation: 6141

Here is a broad layout implementation of the behaviour you want to achieve.

<CoordinatorLayout>
 <AppbarLayout/>
 <Viewpager 
 .
 .
 app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
 <FloatingActionButton/>
</CoordinatorLayout>

add behaviour to the underlying ViewPager and it does work. Now let's say your AppbarLayout is a custom implementation with a ToolBar a TabLayout and a custom view say a LinearLayout this is how that code should implement the scroll attribute.

<AppBarLayout>
    <CollapsingToolbarLayout
        app:layout_scrollFlags="scroll|snap"
        />

    <Toolbar
        app:layout_scrollFlags="scroll|snap"
        />

    <LinearLayout
        android:id="+id/title_container"
        app:layout_scrollFlags="scroll|enterAlways"
        />

    <TabLayout /> <!-- no flags -->
</AppBarLayout>

Further you can experiment with the flags and their values. But remember, AppbarLayout is a vertical LinearLayout with superpowers. So, place your views accordingly. Try this blogpost for details

Upvotes: 2

alican akyol
alican akyol

Reputation: 288

You can try it;

app:layout_behavior="@string/appbar_scrolling_view_behavior"

For Example code;

<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:id="@+id/mainScreen">

<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"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    -->

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        style="@style/AppTabLayout"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<!--<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragment_container">-->

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

<!--</FrameLayout>-->

Upvotes: -1

Ivan Milisavljevic
Ivan Milisavljevic

Reputation: 2068

Try wraping your toolbar inside CollapsingToolbarLayout like this

<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:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            />

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

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@color/ezy_green"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/gray_background"
        app:tabIndicatorHeight="4dp"
        app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

And dont forget to add scrollview behavior to your Recycler View and to set rest of the attributes to fit your needs

Upvotes: 0

Related Questions