【问题标题】:Two RecyclerViews on one screen一个屏幕上有两个 RecyclerViews
【发布时间】:2018-01-03 18:21:06
【问题描述】:

我的任务是在移动应用程序上实现一项功能。以前我没有使用过 Android 或 Java。

我需要的是一个屏幕(一个片段)来显示两个成员数量奇数的事件列表,我们称它们为ListX和ListY。

我制作了包含两个 RecyclerView 和两个标签的布局(一个标签用作每个回收器视图的标题)。 它像现在一样工作,并且我显示了事件,但 ListX 是可滚动的,即使它的 layout_height 设置为 wrap_content(当时仅显示 6 个项目),而 ListY 不可滚动并且正在显示集合中的所有事件。

我需要的是 ListX 中的所有项目都显示在第一个 RecyclerView 中(没有滚动),然后 ListY 显示在第二个 RecyclerView 中也没有滚动。

布局:

<FrameLayout
    android:id="@+id/resultsView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/label_daily" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list_daily"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/label_daily"
                    tools:listitem="@layout/fragment_timetracking_event_item" />

                <TextView
                    android:id="@+id/label_unfinished" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list_unfinished"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/label_unfinished"
                    tools:listitem="@layout/fragment_timetracking_event_item" />

            </LinearLayout>
        </ScrollView>

    </android.support.v4.widget.SwipeRefreshLayout>

</FrameLayout>

从 XML 中可以看出,两个 RecyclerView 都将 layout_height 设置为 wrap_content 但第一个 RecyclerView 不服从。

Here is sketch of how it should look like

有没有办法将两个列表放在一个 RecyclerView 中并制作自定义分隔符?或者任何其他方式将一个列表的开头锚定到另一个列表的末尾并让它们显示所有项目而不滚动?

我希望在这里得到帮助。

【问题讨论】:

  • 如何保证每个recyclerview中的所有项目都是可见的?也就是说,如果您希望两个列表都滚动,那么当列表大于可用屏幕尺寸时用户会怎么做?
  • 我知道这里有一个误解。我的问题是 RecyclerView 有它的嵌套滚动,当我从一个列表向下滚动到另一个 Recycler 时,它是独立滚动的。
  • 我需要整个 ScrollView 作为一个组滚动,而不需要 RecyclerViews 的嵌套滚动
  • 你应该检查一下。 stackoverflow.com/questions/30531091/…
  • 列表上的项目是动态的,这意味着有时 ListX 上可能有 5 个项目,ListY 上可能有 50 个项目,或者 ListX 上可能有 10 个项目,ListY 上可能有 0 个项目

标签: android android-layout


【解决方案1】:

你不需要 2 个 recyclerView,你可以使用单个 recyclerView 来实现。 您应该阅读getItemViewType 和在bindViewHoldercreateViewHolder 中使用相同的内容

你可以指定这三种类型

  • 标题
  • 日常用品
  • 完成项目

像这样创建你的列表

  1. 添加每日标题
  2. 添加每日物品
  3. 添加未完成的标题
  4. 添加未完成的项目

现在您可以在单个回收器视图中呈现两个列表

【讨论】:

  • 能否提供一些示例代码或工作教程?我真的是 Java n00b,我自己无法意识到你上面所说的,我今天尝试过类似的事情。
  • 所有这些类型都应该扩展 RecyclerView.ViewHolder 吗?
  • 它已经奏效了,当我有时间我会发布这个问题的答案。非常感谢您的帮助
【解决方案2】:

你的问题

...另一种方式将一个列表的开头锚定到另一个列表的末尾并让它们显示所有项目而不滚动?

有一个简单的解决方案,无需将代码重写为 RecycleView - 只需在 xml 布局中放置 android.support.v4.widget.NestedScrollView 而不是 ScrollView。这似乎是ScrollView 小部件中的一个错误,已在android.support.v4.widget.NestedScrollView 中修复。

P.S. 解决这个问题后你会遇到其他问题 - Android RecycleView 滚动没有动力,在"RecyclerView within NestedScrollView Scrolling Issue" 中解决。仅适用于 RecycleView 中的短项目列表,因为正如上面文章的 cmets 中提到的那样

这不是一个好的解决方案,因为禁用嵌套滚动也会禁用单元格重用,因此一次加载所有单元格。

在项目列表较长的情况下,这将影响您的应用程序的内存消耗。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 2018-12-25
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多