Tehleel Mir
Tehleel Mir

Reputation: 853

How can i merge two recycler views such that they scroll one after another?

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 enter image description here

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>

Upvotes: 0

Views: 1192

Answers (3)

kunal gupta
kunal gupta

Reputation: 11

I think you should use two lists in one RecyclerView adapter.And have two different view holders.Whenever you need to make a change just make the change in specific array list (1 or 2) and call notifyDatasetChanged() on the single adapter you have. Something like this answer

Upvotes: 1

Priyanka Nigam
Priyanka Nigam

Reputation: 14

Create 2 different xml file apart from activity_main.xml ,create 2 external java file associated with these 2 XML file for user handling code purposes. Give your desired layout for recycler view in those 2 XML file,map those XML file in MainActivity.java class using some java class which will extend RecyclerView.Adapter class and override some methods as onCreateViewHolder(),onBindViewHolder().To inflate the recycler view data ,create 1 more java file which will use some getter() & setter() inorder to populate the recycler view with data by the help of the class extending RecyclerView.Adapter class.

Upvotes: -1

Mo Adel
Mo Adel

Reputation: 1156

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.

Upvotes: 4

Related Questions