avatarSiva Ganesh Kantamani

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2799

Abstract

ter<span class="hljs-operator">:</span> TopicAdapter<span class="hljs-operator">=</span><span class="hljs-keyword">val</span> footerAdapter<span class="hljs-operator">:</span> FooterAdapter<span class="hljs-operator">=</span></pre></div><div id="34f9"><pre><span class="hljs-attribute">val mergeAdapter</span> = MergeAdapter(headerAdapter, topicAdapter, footerAdapter)</pre></div><p id="c424">The views in the <code>recyclerview</code> will render according to the order of adapters in the <a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter"><code>MergeAdap</code>ter</a> constructor. After that, as usual, we need to invoke <code>setAdapter </code>and pass the<code>mergeAdapter`</code>instance, as shown below:</p><div id="6249"><pre><span class="hljs-attr">recyclerView.adapter</span> = mergeAdapter</pre></div><figure id="83fc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nbYNmKcRMl5yaajjI19NEg.png"><figcaption></figcaption></figure><p id="ab4f">In this way, we achieve separation of concerns:</p><ul><li>To show loading or error case, now we can do it in <code>FooterAdapter</code></li><li>If we have different view-types in topics, then we can do it separately in <code>TopicAdapter</code>.</li></ul><h1 id="9bac">Exploring MergeAdapter</h1><h2 id="42ba">MergeAdapter.Config</h2><p id="c5df">By default, every adapter uses its own pool of <code>viewHolder</code>s. If you have the same layout that needs to be used across different adapters, you have to use <a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter.Config"><code>MergeAdapter.Con</code>fig</a>. We can configure merge-adapter to three different types:</p><ul><li><a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter.Config.StableIdMode#NO_STABLE_IDS"><code>MergeAdapter.Config.StableIdMode.NO_STABLE_</code>IDS</a> — This is the default mode. Each adapter has their own <code>viewholder</code> pool and ignores the stable IDs from sub adapters.</li><li><a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter.Config.StableIdMode#ISOLATED_STABLE_IDS"><code>MergeAdapter.Config.StableIdMode.ISOLATED_STABLE_</code>IDS</a> — In this mode, <a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter"><code>MergeAdapter</code></a><code> </code>collects all the IDs from sub-adapters as two different <code>viewHolder</code>s may return the same ID because they’re unaware of each other. Then <a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter"><code>MergeAdap</code>ter</a> will isolate each <a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerV

Options

iew.Adapter"><code>RecyclerView.Adap</code>ter</a>'s ID pool from each other, causing it to overwrite the reported stable ID before reporting back to the <a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView"><code>RecyclerV</code>iew</a>.</li><li><a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter.Config.StableIdMode#SHARED_STABLE_IDS"><code>MergeAdapter.Config.StableIdMode.SHARED_STABLE_</code>IDS</a> — In this mode, <a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter"><code>MergeAdap</code>ter</a> won’t isolate each adapter’s pool from each other and also won’t override ID’s as it did in the previous case.</li></ul><h2 id="d9f2">Updating Data and ViewHolders</h2><p id="9d59">I would recommend using <code>ListAdapter</code>, which handles the data changes for you instead of the general <code>recyclerview.adapter</code>. The next thing is <code>viewholder</code>. To obtain the adapter position in <code>recyclerview</code>, we use <a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAdapterPosition%28%29"><code>ViewHolder.getAdapterPosit</code>ion</a>, but when you’re operating through the <code>Merge</code> adapter, you need to use <a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapter%28%29"><code>ViewHolder.getBindingAdapte</code>r()</a>.</p><h1 id="3d04">Conclusion</h1><p id="f5cf">As it’s still in the early stages of development, I would not recommend you use this feature in production code just yet. There may be changes in the future, or some features may be removed in forthcoming versions.</p><p id="a2ea">I also recommend using the view-types feature from <code>Recyclerview.Adapter</code> to show different views in <code>recyclerview</code>. Use the <a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter"><code>MergeAdap</code>ter</a> only when it benefits from encapsulation. <a href="https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter"><code>MergeAdap</code>ter</a> isn’t a solution to show different view-types; it’s a way of providing encapsulation while inflating different view-types.</p><h1 id="8093">Bonus</h1><p id="95cf">To learn more about adapters in Android, read the following articles:</p><ul><li><a href="https://readmedium.com/building-a-reactive-heterogeneous-adapter-in-kotlin-eed9487df29b">“Building a Reactive & Heterogeneous Adapter in Kotlin”</a></li><li><a href="https://readmedium.com/evolution-of-adapters-in-android-2e2ff58c0f98">“Evolution of Adapters in Android”</a></li></ul><p id="cbbc">Thank you for reading.</p></article></body>

How to Merge Adapters Sequentially in Android

Introducing a brand new MergeAdapter for Android developers

Photo by Azharul Islam on Unsplash

Introduction

MergeAdapter is a new feature introduced in recyclerview:1.2.0-alpha02, enabling us to club multiple adapters sequentially. This provides more encapsulation and re-usability, instead of merging several data sources into the same adapter. In this article, you’ll learn how to use MergeAdapter and some other useful things, like how to use the view pool efficiently across adapters.

Integration

The Android team has now released the new version of recyclerview. To use MergeAdapter, update the recyclerview library version to 1.2.0-alpha02 or include the following line under the dependency tag in-app level build.gradle file:

dependencies {
   implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha02'
}

Working with MergeAdapter

Let’s take a basic use case. We need to show a header layout, then a list of topics, and a footer showing either loading or error. We have three different adapters, one for each type — HeaderAdapter, TopicAdapter, and FooterAdapter.

We want to merge these three adapters using the merge-adapter constructor and set the result MergeAdapter instance to recyclerview. The first here is to merge three adapters as shown:

val headerAdapter: HeaderAdapter=val topicAdapter: TopicAdapter=val footerAdapter: FooterAdapter=
val mergeAdapter = MergeAdapter(headerAdapter, topicAdapter, 
     footerAdapter)

The views in the recyclerview will render according to the order of adapters in the MergeAdapter constructor. After that, as usual, we need to invoke setAdapter and pass themergeAdapter`instance, as shown below:

recyclerView.adapter = mergeAdapter

In this way, we achieve separation of concerns:

  • To show loading or error case, now we can do it in FooterAdapter
  • If we have different view-types in topics, then we can do it separately in TopicAdapter.

Exploring MergeAdapter

MergeAdapter.Config

By default, every adapter uses its own pool of viewHolders. If you have the same layout that needs to be used across different adapters, you have to use MergeAdapter.Config. We can configure merge-adapter to three different types:

Updating Data and ViewHolders

I would recommend using ListAdapter, which handles the data changes for you instead of the general recyclerview.adapter. The next thing is viewholder. To obtain the adapter position in recyclerview, we use ViewHolder.getAdapterPosition, but when you’re operating through the Merge adapter, you need to use ViewHolder.getBindingAdapter().

Conclusion

As it’s still in the early stages of development, I would not recommend you use this feature in production code just yet. There may be changes in the future, or some features may be removed in forthcoming versions.

I also recommend using the view-types feature from Recyclerview.Adapter to show different views in recyclerview. Use the MergeAdapter only when it benefits from encapsulation. MergeAdapter isn’t a solution to show different view-types; it’s a way of providing encapsulation while inflating different view-types.

Bonus

To learn more about adapters in Android, read the following articles:

Thank you for reading.

Programming
Recyclerview
Android
Mobile
AndroidDev
Recommended from ReadMedium