1. Introduction

This time I want to introduce MergeAdapter by Google. As the name suggests, it’s a RecyclerView component.

This component is in androidx. Recyclerview: recyclerview: 1.2.0 – alpha02, its main role is to concentrate more Adapter in a Adapter, and then display in the recyclerview.

So if you want to use RecyclerView to display different viewtypes, you need to use RecyclerView getItemViewType, or use external libraries like Groupie.

But with MergeAdapter, we can use it directly to achieve the above requirements, very convenient and elegant.

Without further ado, let’s introduce its usage and then discuss its advantages and disadvantages.

2. Usage

2.1 import library

Add RecyclerView libraries to build.gradle.

androidx.recyclerview:recyclerview:1.2.0-alpha02
Copy the code

2.2 Creating an Adapter Layout

Create layout according to your needs, as many viewTypes as you need to create the corresponding layout.

<? xml version="1.0" encoding="utf-8"? > <layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <androidx.constraintlayout.widget.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:layout_marginStart="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:text="hello" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Copy the code

2.3 create Adapter

Create an Adapter based on your requirements. Same as normal usage.

class FirstAdapter(private val data: List<String>) :
    ListAdapter<String, FirstAdapter.ViewHolder>(DiffCallback()) {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from((parent.context))
        val binding: ItemFirstBinding =
            DataBindingUtil.inflate(inflater, R.layout.item_first, parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.textView1.text = data[position]
    }

    override fun getItemCount(a): Int {
        return data.size
    }

   class DiffCallback : DiffUtil.ItemCallback<String>() {
        override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }
        override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem === newItem
        }
    }

    class ViewHolder(var binding: ItemFirstBinding) : RecyclerView.ViewHolder(binding.root)
Copy the code

2.4 create MergeAdapter

We create MergeAdapter in MainActivity and then RecyclerView.

val firstAdapter = FirstAdapter(data)
val secondAdapter = SecondAdapter(data)
val thirdAdapter = ThirdAdapter(data)

/ / create the MergeAdapter
// You need to pass multiple adapters to MergeAdapter via listOf
mergeAdapter = MergeAdapter(listOf(firstAdapter,secondAdapter,thirdAdapter))

binding.recyclerView.adapter = mergeAdapter
Copy the code

3. About the MergeAdapter

3.1 remove the adapter

You can remove an existing Adapter.

mergeAdapter.removeAdapter(firstAdapter)
Copy the code

3.2 add adapter

You can add adapter after MergeAdapter to RecyclerView.

mergeAdapter.addAdapter(firstAdapter)
Copy the code

3.3 Obtaining the current Adapter array

Get the current Adapter array that has been passed to RecyclerView.

val adapters = mergeAdapter.adapters
Copy the code

3.4 Adapter reuse

By default, each Adapter maintains its own ViewHolder pool and cannot reuse one another. If we want to reuse it we need to set mergeAdapter.config.

val configBuilder = MergeAdapter.Config.Builder()
configBuilder.setIsolateViewTypes(false)
Copy the code

The Config is then passed in when the MergeAdapter is created.

val mergeAdapter = MergeAdapter(configBuilder.build(),listOf(firstAdapter,secondAdapter,thirdAdapter))
Copy the code

3.5 Notification of Data updates

When a new data update is required, the corresponding Adapter is called, and notifyDatasetChanged is used with the corresponding Adapter. The notifyDatasetChanged of the Adapter is called, and finally the notifyDatasetChanged of the MergeAdapter is also called.

thirdAdapter.submitList(addData())
thirdAdapter.notifyDataSetChanged()
Copy the code

4. Deficiencies

It’s a little natural, but one of MergeAdapter’s obvious weaknesses is that viewTypes can’t be mixed, limiting its range of use. But if there is no such requirement, use MergeAdapter first!

5. GitHub

Making: github.com/HyejeanMOON…

Android ConstraintLayout: juejin.cn/post/684490…

Paging tutorial for Jetpack: juejin.cn/post/684490…

Groupie tutorials: juejin.cn/post/684490…