Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 376
Matteo Avatar asked Nov 08 '22 14:11

Matteo


1 Answers

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

like image 136
sud007 Avatar answered Nov 14 '22 21:11

sud007