首页 > 解决方案 > 具有 2 个回收站视图的协调器布局

问题描述

我想用这个布局构建一个 android 片段:

  1. 屏幕的上半部分是一个水平滚动的 RecyclerView。
  2. 屏幕的下半部分是一个垂直滚动的 RecyclerView。

当底部 RecyclerView 向底部滚动时,我希望顶部的 RecyclerView 折叠和隐藏(并在底部视图滚动到顶部时打开)。

协调器布局似乎是答案,但我遇到的每个示例都使用 AppBarLayout 作为顶部。包含该片段的活动已经显示了一个应用栏;我不想修改它。

我将如何在 CoordinatorLayout 中实现这两个 RecyclerView 设置而不解决应用栏?

标签: androidandroid-recyclerviewandroid-coordinatorlayout

解决方案


您可以使用具有垂直方向的 a NestedScrollView, a作为 of 的子级,并将两个s 添加为 of 的子级LinearLayoutNestedScrollViewRecyclerViewLinearLayout

样本

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

推荐阅读