android - 如何合并两个回收器视图,使它们一个接一个地滚动?
问题描述
我试图实现这个答案并且它有效,但前提是这两个列表都已经预先填充,但在我的情况下,一个列表是预先填充的,另一个是动态递增/递减的。
黑框代表循环列表,可以动态增加/减少,主要作用是显示用户喜欢的字体。
红色框代表所有可用的字体,它们是预填充的。
我希望这两个列表在滚动中相互流动,这样用户就不会注意到它们实际上是两个不同的列表。
我的 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>
解决方案
有 2 个选项,您可以ConcatAdapter
将两个适配器组合在一个适配器中,也可以使用一个 RecyclerView(推荐)。或者使用NestedScrollView
包装两个 RecyclerView 并确保将两个 RecyclerView 高度设置为 wrap_content。
但是根据您要实现的目标,我建议您使用ConcatAdapter(adapter1, adapter2)
包装两个适配器并将新适配器设置在一个 RecyclerView 上。您仍然可以自行控制每个适配器,Add, Remove
并且调用notifyDataSetChanged
已更改的适配器将反映 RecyclerView 上的更改。
推荐阅读
- couchbase - FindAndModify 使用 Couchbase
- python - 如何使用 c++ 中的引用实现类似 python 的行为?
- ios - makeTouchBar() 不被称为 Mac Catalyst touchbar
- android - 最新的 studio 3.6 中未生成数据绑定类
- python - AttributeError:模块“shapely”没有属性“affinity”
- javascript - 为什么终端颜色与浏览器颜色不匹配?
- terminal - 在终端中使用转义序列
- c++ - Eigen 如何在容器循环中干净地附加矩阵?
- javascript - 我想根据客户端系统时区更改我的日期蚂蚁时间
- typescript - 函数剩余参数和泛型