【问题标题】:How can i merge two recycler views such that they scroll one after another?如何合并两个回收器视图,使它们一个接一个地滚动?
【发布时间】:2021-03-11 06:13:31
【问题描述】:

我尝试实现this 答案并且它有效,但前提是这两个列表都已预填充,但在我的情况下,一个列表是预填充的,另一个是动态递增/递减的。

这是我的布局结构

黑框代表循环列表,可以动态增加/减少,主要作用是显示用户喜欢的字体。

红色框代表所有可用的字体,它们是预填充的。

我希望这两个列表在滚动中相互流动,这样用户就不会注意到它们实际上是两个不同的列表。

我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:padding="@dimen/home_section_padding"
    android:background="@color/background"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:id="@+id/editText"
        android:textColorHint=" #C0C0C0"
        android:hint="Paste"
        android:textColor="@color/text_color"
        android:inputType="text"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_width="match_parent"
        android:text="Style"
        android:textStyle="bold"
        android:textColor="@color/text_color"
        android:layout_marginBottom="10dp"
        android:fontFamily="serif-monospace"
        android:textAppearance="?android:textAppearanceLarge"
        android:background="@drawable/round_shape_for_button"
        android:onClick="fire"
        android:id="@+id/button"
        android:layout_height="wrap_content"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyelerViewFav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    </androidx.recyclerview.widget.RecyclerView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyelerView"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />





</LinearLayout>

【问题讨论】:

    标签: android android-layout android-recyclerview nestedrecyclerview


    【解决方案1】:

    有 2 个选项,您可以使用 ConcatAdapter 将两个适配器组合在一个适配器中,并使用一个 RecyclerView(推荐)。或者使用NestedScrollView 包装两个 RecyclerView,并确保将两个 RecyclerView 的高度都设置为 wrap_content。

    但根据您要实现的目标,我建议使用ConcatAdapter(adapter1, adapter2) 包装您的两个适配器并将新适配器设置在一个 RecyclerView 上。您仍然可以自行控制每个适配器 Add, Remove 并在更改后的适配器上调用 notifyDataSetChanged 将反映 RecyclerView 上的更改。

    【讨论】:

    【解决方案2】:

    我认为您应该在一个 RecyclerView 适配器中使用两个列表。并且有两个不同的视图持有者。每当您需要进行更改时,只需在特定的数组列表(1 或 2)中进行更改并在单个上调用 notifyDatasetChanged()你有的适配器。 像这样answer

    【讨论】:

      【解决方案3】:

      除了 activity_main.xml 之外创建 2 个不同的 xml 文件,创建 2 个与这 2 个 XML 文件关联的外部 java 文件,用于用户处理代码的目的。在这 2 个 XML 文件中为回收器视图提供所需的布局,使用一些 java 类将这些 XML 文件映射到 MainActivity.java 类中,该类将扩展 RecyclerView.Adapter 类并覆盖一些方法,如 onCreateViewHolder()、onBindViewHolder()。查看数据,创建另外 1 个 java 文件,该文件将使用一些 getter() 和 setter() 以便在扩展 RecyclerView.Adapter 类的帮助下用数据填充回收器视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-27
        • 1970-01-01
        • 1970-01-01
        • 2013-12-14
        • 1970-01-01
        • 1970-01-01
        • 2018-11-09
        • 2023-03-14
        相关资源
        最近更新 更多