【问题标题】:use CoordinatorLayout to hide/show RelativeLayout when scrolling a RecyclerView滚动 RecyclerView 时使用 CoordinatorLayout 隐藏/显示 RelativeLayout
【发布时间】:2016-06-11 08:32:54
【问题描述】:

我有一个布局(由 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>

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

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

【问题讨论】:

  • 您找到解决方案了吗?我有完全相同的问题...谢谢
  • 很遗憾我还没有找到可接受的解决方案

标签: android coordinator-layout


【解决方案1】:

在您的工具栏代码中试试这个:

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

我发现此链接很有帮助:Scrolling Toolbar

【讨论】:

  • 我使用了这些值,但是当我按照您的建议进行操作时,工具栏和相关布局会滚动。但我希望工具栏保持固定,只是相对布局滚动:(
猜你喜欢
  • 2017-07-26
  • 2017-06-28
  • 2015-12-16
  • 2016-06-17
  • 2019-01-21
  • 2017-05-30
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
相关资源
最近更新 更多