Android 折叠工具栏布局并在滚动时隐藏工具栏
我正在尝试使用CoordinatorLayout和CollazingToolbarLayout创建一些组合布局 在第一种状态下,当我们在最顶端的页面上,并且还没有滚动时,我希望工具栏如下图所示扩展(是的,我这样做了): 在第二种状态下,当开始向下滚动时,图像和工具栏应消失,如下所示(仅显示选项卡): 在最后一种状态下,一旦我在列表中的某个位置(但不是列表的顶部),我想开始向上滚动,一旦我开始向上滚动,我希望工具栏(而不是带有图像的扩展工具栏)开始旋转,如下所示(如果没有到达列表的顶部,图像将不会显示,只有工具栏): 我能达到第一个状态,但另外两个状态有问题, 一旦在CollasingToolbarLayout内部实现了工具栏,那么在CollasingToolbarLayout组件外部如何使用工具栏的灵活性就不清楚了。 我不能使工具栏隐藏,如果我这样做,那么它只会在我到达顶部时显示 无论如何,我当前的XML(如下所示)处于实现第一张图片的状态,但一旦我开始向下滚动,工具栏就停留在顶部,不会隐藏。注意:我必须告诉工具栏保持“锁定”,因为如果我不这样做,工具栏中的信息就会消失,只有一个空工具栏会显示出来(这是另一篇文章,但知道为什么会发生这种情况仍然很有趣?) 以下是我当前的xml:Android 折叠工具栏布局并在滚动时隐藏工具栏,android,android-coordinatorlayout,android-collapsingtoolbarlayout,coordinator-layout,Android,Android Coordinatorlayout,Android Collapsingtoolbarlayout,Coordinator Layout,我正在尝试使用CoordinatorLayout和CollazingToolbarLayout创建一些组合布局 在第一种状态下,当我们在最顶端的页面上,并且还没有滚动时,我希望工具栏如下图所示扩展(是的,我这样做了): 在第二种状态下,当开始向下滚动时,图像和工具栏应消失,如下所示(仅显示选项卡): 在最后一种状态下,一旦我在列表中的某个位置(但不是列表的顶部),我想开始向上滚动,一旦我开始向上滚动,我希望工具栏(而不是带有图像的扩展工具栏)开始旋转,如下所示(如果没有到达列表的顶部,图像将
<android.support.design.widget.CoordinatorLayout
android:id="@+id/benefit_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_material_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<include
android:id="@+id/toolbar_search_container"
layout="@layout/search_box"
android:layout_height="192dp"
android:layout_width="match_parent"
app:layout_collapseMode="parallax"
/>
<include
android:id="@+id/toolbar_benefit"
layout="@layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentScrim="?attr/colorPrimary"
/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/benefit_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
app:tabIndicatorColor="@color/accentColor"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black"
app:tabIndicatorHeight="4dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/benefit_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<include
layout="@layout/floating_btn_benefits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
/>
</android.support.design.widget.CoordinatorLayou
我已经解决了这个问题,只是为了说明一下,我希望我的工具栏能够在到达顶部时使用paralex图像进行扩展,但我也希望工具栏在向下滚动时消失,并在向上滚动时再次显示(没有paralex图像)。只有当我到达顶部时,paralex图像效果才会显示
因此,基本上解决方案是,使用以下属性更改组件collasingToolbarLayout
:
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
并使用以下属性更改工具栏组件
android:minHeight="?attr/actionBarSize"
关于我的paralex effect图像(这是我的工具栏\u搜索\u容器
),我不应该向其添加任何布局\u滚动标记
那么,它为什么会起作用呢?
要理解它,您需要知道什么是enterallways collapsed
,
enteralways被压缩
effects视图添加了minHeight
属性。这意味着,具有minHeight
的collainingtoolbarlayout
的每个子级都将受到此属性的影响。
因此,我的工具栏将生效
enteralways集合
简单的属性定义:
假设声明了enterAlways并且指定了最小高度,则还可以指定enterAlwaysCollapsed
。使用此设置时,视图将仅显示在此最小高度。只有当滚动到顶部时,视图才会扩展到其全部高度…”
嗯,这不正是我们想要的吗?(不要回答这个反驳性的问题;)
还有一点需要补充,视差组件(toolbar\u search\u container
)取决于要展开的工具栏,因为工具栏只有在到达顶部时才会展开,所以这一切都非常有效
新守则是:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/benefit_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_material_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
>
<include
android:id="@+id/toolbar_search_container"
layout="@layout/search_box"
android:layout_height="192dp"
android:layout_width="match_parent"
app:layout_collapseMode="parallax"
/>
<include
android:id="@+id/toolbar_benefit"
layout="@layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
app:contentScrim="?attr/colorPrimary"
app:layout_collapseMode="pin"
android:fitsSystemWindows="true"
/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/benefit_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
app:tabIndicatorColor="@color/accentColor"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black"
app:tabIndicatorHeight="4dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/benefit_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<include
layout="@layout/floating_btn_benefits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
/>
</android.support.design.widget.CoordinatorLayout>
将这行代码添加到折叠工具栏布局中
app:layout_scrollFlags="scroll|snap|enterAlways|enterAlwaysCollapsed"
非常感谢!!!我发现这个答案太难了,因为很难向谷歌解释这个问题。我已经尝试了几乎所有的可能性,但没有使用所有3个属性。应该让更多的人知道答案!!我按照你说的做了,但是当我向上滚动时,我的折叠工具栏布局在我向上滚动到顶部之前不会出现。你有什么建议吗ing?嘿,温特!你能在这里发布完整的xml吗?不带-标记吗?谢谢!!!我已经搜索了很多年了。真的谢谢你。为了让它更平滑,请添加快照滚动标记,并添加app:contentScrim以在未完全展开时更改工具栏颜色。