CoordinatorLayout不会隐藏操作栏 [英] CoordinatorLayout doesn't hide the actionbar
问题描述
这真让我发疯.我一直在努力进行这项工作两天,但仍未取得任何进展.我想有一个应用程序栏,当我向下滚动 ViewPager
时隐藏,而当我向上滚动 ViewPager
时显示,这似乎是一种标准的方法.
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>
viewpager包含一堆片段,每个片段中都包含一个listview.我正在使用appcompat和版本22.2.0的设计.
The viewpager contains a bunch of fragments which contain a listview in each. I'm using appcompat and design of version 22.2.0.
我已将我的代码与此处的代码进行了比较:git@github.com:slidenerd/Android-Design-Support-Library-Demo.git,发现没有重大区别.有人可以帮忙吗?谢谢.如果我还没有穷尽所有选择,我就不会发布.
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.
推荐答案
我做了一些测试,这是我的发现:
I did some testing and here are my findings:
您原来的布局(没有 CollapsingToolbarLayout
)更近了.我调整了布局并对其进行了测试:
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>
所以这是要点:
-
CoordinatorLayout
必须具有一个AppBarLayout
和另一个子布局. -
AppBarLayout
必须具有Toolbar
和/或TabLayout
. -
app:layout_scrollFlags ="scroll | enterAlways"
属性位于Toolbar
和/或TabLayout
上.
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
.
其中的布局具有 scrollflags
(或两者都有)将滚动显示.因此,我不知道您是否要让工具栏
滚动而不是 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.
您不能使用 ListView
作为滚动视图.即使在您的 ViewPager
片段中.添加 app:layout_behavior ="@ string/appbar_scrolling_view_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.
您必须使用 RecyclerView
或 NestedScrollView
.奇怪的是,我不需要 RecyclerView
上的 app:layout_behavior ="@ string/appbar_scrolling_view_behavior"
属性.这是在 ViewPager
的 Fragment
子类中.
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
.
这篇关于CoordinatorLayout不会隐藏操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!