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

8
我有一个布局(由Android Studio生成),其中我在AppBarLayout中添加了RelativeLayout。以下是代码和其外观:

enter image description here

我卡住的地方: 我想要实现的是,当向下滚动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>

我认为在应该滚动的视图中使用app:layout_scrollFlags="scroll|enterAlways",再加上app:layout_behavior="@string/appbar_scrolling_view_behavior"会实现这一点,但是它并没有起作用。另外,当我将这些字段添加到工具栏本身时,两个布局都会滚动消失 - 这不是我想要的,我希望工具栏始终保持固定。

如果有人能指点我正确的方向就太好了?(我希望可以使用协调布局而不是通过onscroll监听器来进行某些布局操作)


1
你找到任何解决方案了吗?我也有完全相同的问题...谢谢 - Blunderer
很遗憾,我还没有找到一个可接受的解决方案。 - stamanuel
1个回答

0

尝试在您的工具栏代码中使用以下代码:

app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"

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


2
我尝试过调整这些值,但是当我按照你的建议操作时,工具栏和相对布局会一起滚动。 但是我希望工具栏保持固定,只有相对布局可以滚动 :( - stamanuel

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