如何合并两个回收器视图,以便它们一个接一个地滚动? [英] How can i merge two recycler views such that they scroll one after another?

查看:76
本文介绍了如何合并两个回收器视图,以便它们一个接一个地滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现

黑框表示可以动态增加/减少的回收列表,其主要作用是向用户显示喜欢的字体.

红色框代表所有可用的字体,并且它们已经预先填充.

我希望这两个列表在滚动中相互流动,以使用户无法注意到它们实际上是两个不同的列表.

我的xml代码:

 <?xml version ="1.0"encoding ="utf-8"?< LinearLayoutxmlns:android ="http://schemas.android.com/apk/res/android"xmlns:app =" http://schemas.android.com/apk/res-autoxmlns: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"工具:context =.MainActivity".< EditTextandroid:layout_width =" match_parent"android:id ="@@ id/editText"android:textColorHint ="#C0C0C0"android:hint ="粘贴"android:textColor =" @ color/text_color"android:inputType ="text"android:layout_height =" wrap_content"/><按钮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:textAppearanceLargeandroid:background =" @ drawable/round_shape_for_button"android:onClick =" fire"android:id ="@@ id/button"android:layout_height =" wrap_content"/>< androidx.recyclerview.widget.RecyclerViewandroid:id ="@@ id/recyelerViewFav"android:layout_width =" match_parent"android:layout_height =" wrap_content"></androidx.recyclerview.widget.RecyclerView>< androidx.recyclerview.widget.RecyclerViewandroid:id ="@@ id/recyelerView"android:layout_width =" match_parent"android:layout_weight =" 1"android:layout_height =" 0dp"/></LinearLayout> 

解决方案

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

但是根据您要实现的目标,我建议使用 ConcatAdapter(adapter1,adapter2)来包装两个适配器并将新适配器设置在一个RecyclerView上.您仍然可以单独控制每个适配器添加,删除,并在更改后的适配器上调用 notifyDataSetChanged 将反映RecyclerView上的更改.

I tried to implement this answer and it worked but only if the both list are prepopulated already, but in my case one list is prepopulated and the other is incremented/decremented dynamically.

Here is my layout structure

Black box represents the recycle list which can be incremented/decremented dynamically, and its main action is to show the users favorite fonts.

Red box represents all the fonts which are available and they are prepopulated.

i want both these list to flow each-other in scroll, such that user can not notice that these are actually two different list.

my xml code :

<?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>

解决方案

There is 2 options, You can either use ConcatAdapter to combine both adapters in one adapter and use one RecyclerView (Recommended). Or use NestedScrollView to wrap both your RecyclerViews and make sure to set both RecyclerView heights to wrap_content.

But based on what you are trying to achieve, I would recommend to use ConcatAdapter(adapter1, adapter2) to wrap both your adapters and set that new adapter on one RecyclerView. You will still be able to control each adapter by itself Add, Remove and calling notifyDataSetChanged on the changed adapter will reflect changes on the RecyclerView.

这篇关于如何合并两个回收器视图,以便它们一个接一个地滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆