首页 > 解决方案 > 如何合并两个回收器视图,使它们一个接一个地滚动?

问题描述

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

这是我的布局结构 在此处输入图像描述

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

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

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

我的 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>

标签: androidandroid-layoutandroid-recyclerviewnestedrecyclerview

解决方案


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

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


推荐阅读