Reputation: 23
This is driving me nuts. I have been trying to make this work for two days, and no progress has been made. I want to have an app bar that hides when I scroll the ViewPager
down, and shows when I scroll ViewPager
up, and this appears to be the standard way of doing it.
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.design.widget.TabLayout
android:id="@+id/posts_pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:layout_collapseMode="pin">
</android.support.design.widget.TabLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/posts_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
The viewpager contains a bunch of fragments which contain a listview in each. I'm using appcompat and design of version 22.2.0.
I have compared my code with the code here: [email protected]:slidenerd/Android-Design-Support-Library-Demo.git, and found no major difference. Could someone please help? Thanks. I wouldn't post this if I haven't exhausted all my options.
Upvotes: 2
Views: 5273
Reputation: 31015
I did some testing and here are my findings:
The layout you had originally (without the CollapsingToolbarLayout
) was closer. I tweaked your layout and tested with it:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/posts_pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/posts_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
So here are the major points:
CoordinatorLayout
has to have an AppBarLayout
and one other child layout.AppBarLayout
has to have a Toolbar
and/or a TabLayout
.The app:layout_scrollFlags="scroll|enterAlways"
attribute goes on the Toolbar
and/or the TabLayout
.
Whichever layout has scrollflags
(or both) will scroll out of view. So I don't know if you wanted the Toolbar
to scroll and not the TabLayout
, but either way you can control the behavior.
You can't use a ListView
as your scrolling view. Even inside your ViewPager
fragment. Adding the app:layout_behavior="@string/appbar_scrolling_view_behavior"
attribute made no difference.
You have to use a RecyclerView
or a NestedScrollView
. Oddly, I didn't need the app:layout_behavior="@string/appbar_scrolling_view_behavior"
attribute on the RecyclerView
. And this was inside the Fragment
subclass for the ViewPager
.
Upvotes: 5