在CoordinatorLayout中隐藏工具栏下方的视图

5

我正在创建一个如下所示的布局:

顶部工具栏

在顶部工具栏下方的工具栏

可滚动列表

当我滚动时,我想隐藏位于顶部工具栏和可滚动列表之间的工具栏。我的布局如下:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="SomeActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways"/>

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:minHeight="0dp"
        android:id="@+id/info_bar"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:layout_scrollFlags="scroll|enterAlways" />

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/content_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

通过这些更改,当滚动时两个工具栏都会隐藏。我只需要隐藏第二个工具栏。

编辑:如果我无法使其起作用,我将在我的recyclerView上附加一个滚动监听器,并以这种方式解决此问题。但我想知道是否有更清晰的滚动行为解决方案。


从第一个工具栏中删除 app:layout_scrollFlags="scroll|enterAlways" - Daniel Nugent
那是我的第一个实现。如果我这样做,两个工具栏都会留在那里。 - Prathamesh Shetye
没错,那个可以完美地工作。 - Prathamesh Shetye
有趣。也许尝试将其分成两个AppBarLayout?只是瞎猜.... - Daniel Nugent
那个没有起作用... 底部应用栏与顶部的重叠了。 - Prathamesh Shetye
显示剩余2条评论
2个回答

12

将顶部工具栏移出协调布局(coordinatorlayout)并解决了此问题。(感谢同事)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_done" />

</android.support.design.widget.CoordinatorLayout>

:表示段落的HTML标签。

0
如果我理解正确的话,您可能想在向上滚动时折叠可折叠工具栏(collapseableToolbar),而不是在工具栏和列表项之间显示一些(20dp)空间。对于我来说,这花费了半天的时间,问题只是在coordinatorLayout中的一个布尔值。只需将"fitsSystemWindows"的值从true更改为false即可。
通过这种方式,您可以在滚动时隐藏collapseToolBar的图像。
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
  tools:context="com.thedeveloperworldisyours.imagecollapsingtoolbarlayout.ScrollingActivity">

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接