使用CoordinatorLayout在滚动RecyclerView时隐藏/显示RelativeLayout

sta*_*uel 5 android coordinator-layout

我有一个布局(由android studio生成),我在AppBarLayout中添加了一个RelativeLayout.代码如下,它看起来像这样:

在此输入图像描述

我被卡住的地方:我想要实现的是当滚动Recyclerview时我希望绿色相对布局(具有id'controlContainer')与它一起滚动,当我向上滚动它应该滚动(不仅仅是在顶部但在任何地方我在列表中向上滚动)

顶部的工具栏应保持原样.

<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=".MainActivity">

<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="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        />

    <RelativeLayout
        android:id="@+id/controlContainer"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@android:color/holo_green_dark"
        app:layout_scrollFlags="scroll|enterAlways"></RelativeLayout>

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


<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/venue_list" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我认为app:layout_scrollFlags="scroll|enterAlways"在应该滚动的视图中使用app:layout_behavior="@string/appbar_scrolling_view_behavior"应该实现,但它没有做任何事情.或者,当我将这些字段添加到工具栏本身时,两个布局都会滚动 - 这不是我想要的,我希望工具栏始终保持固定.

如果有人能指出我在这里正确的方向会很好吗?(我希望有可能使用协调器布局而不是使用onscroll侦听器进行一些布局操作?)

小智 0

在你的工具栏代码中尝试一下:

app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
Run Code Online (Sandbox Code Playgroud)

我发现这个链接很有帮助:滚动工具栏

  • 我使用了这些值,但是当我按照您的建议操作时,工具栏和相对布局就会滚动。但我希望工具栏保持固定,而只是相对布局滚动:( (2认同)