问题
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: git@github.com: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.
回答1:
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 anAppBarLayout
and one other child layout.AppBarLayout
has to have aToolbar
and/or aTabLayout
.The
app:layout_scrollFlags="scroll|enterAlways"
attribute goes on theToolbar
and/or theTabLayout
.Whichever layout has
scrollflags
(or both) will scroll out of view. So I don't know if you wanted theToolbar
to scroll and not theTabLayout
, but either way you can control the behavior.You can't use a
ListView
as your scrolling view. Even inside yourViewPager
fragment. Adding theapp:layout_behavior="@string/appbar_scrolling_view_behavior"
attribute made no difference.You have to use a
RecyclerView
or aNestedScrollView
. Oddly, I didn't need theapp:layout_behavior="@string/appbar_scrolling_view_behavior"
attribute on theRecyclerView
. And this was inside theFragment
subclass for theViewPager
.
来源:https://stackoverflow.com/questions/32804037/coordinatorlayout-doesnt-hide-the-actionbar