Merge Adapter sequentially with Merge adapters

 Merge Adapter is a new feature introduced in recyclerview:1.2.0-alpha02, that allows us to sequentially club multiple adapters. This offers more encapsulation and reusability, rather than combining several data sources into one adapter. In this post, you can learn how to use Merge Adapter and some other useful stuff, such as how to make effective use of the view pool through adapters.

Now the Android team has launched the latest recycle rview update. To use Merge Adapter, upgrade the version of the recyclerview library to 1.2.0-alpha02, or add the following line in the build.gradle file under the dependency tag:
Addictions
Using 'androidx.recyclerview: recyclerview:1.2.0-alpha02'
}
To support us android, a new concept for merging data adapters has been developed. They have recently launched a new Recyclerview package:1.2.0-alpha02. This kit has a new class called Merge Adapter as the name means that it enables you to combine multiple data adapters into a single recycler
view. And now we can conveniently keep our data source differently, rather than integrating it in a single adapter.

Let 's start with the example, then. Suppose you were given the following screen to design and manage progress along with views of the hide and display as below. Both of these can be achieved in one recycler view. Let's see how this is going to work. With the aid of this example we should understand that concept.more info visit:android course
What is Merge Adapter?
Merge Adapter enables us to merge multi adapter data in a sequence. We do have 3 different adapters in our case. One is for title & picture, the second is for food list, and the third is for advance along with quote.
Merge Adapter is a new feature introduced in recyclerview:1.2.0-alpha02, that allows us to sequentially club multiple adapters. This offers more encapsulation and reusability, rather than combining several data sources into one adapter. In this post, you can learn how to use Merge Adapter and some other useful stuff, such as how to make effective use of the view pool through adapters.
private val headerAdapter = HeaderAdapter()
private val foodListAdapter = FoodListAdapter()
private val footerAdapter = FooterAdapter()
val mergeAdapter = MergeAdapter(headerAdapter, foodListAdapter, footerAdapter)
rv.adapter = mergeAdapter

Data from all three adapters will be shown in a recyclerview sequentially. This way you can handle your logic separately and no logic mixup is required. For example, you can handle bottom view progress in your own adapter, rather than merging it with others.
Places to remember
Merge Adapter must call the notifyDataSetChanged() method any time we call adapter notifyDataSetChanged() that is part of mergeAdapter. So using ListAdapter better to avoid calling that process.
We 're usually using one view holder per adapter. But if you want to use one view holder with more than one adapter when using mergeAdapter, you have to transfer config like this in the constructor.
val configBuilder = MergeAdapter.Config.Builder()
configBuilder.setIsolateViewTypes(false)
val mergeAdapter = MergeAdapter(configBuilder.build(),headerAdapter, foodListAdapter, footerAdapter)
We also use the method ViewHolder.getAdapterPosition) (to find viewHolder position within the adapter. If you are reusing viewHolders and you want to get the last attached adapter then use the method ViewHolder.getBindingAdapter).
If you choose to use the same adapter in a recycler view several times then you can build several instances of the same adapter and transfer them in the mergeAdapter constructor. Defining the reasoning into various adapters is good practice.
Inclusion
Now the Android team has launched the latest recyclerview update. To use MergeAdapter, upgrade the version of the recyclerview library to 1.2.0-alpha02, or add the following line in the build.gradle file under the dependency tag:
Addictions
Use 'androidx.recyclerview: recyclerview:1.2.0-alpha02'
}
Using MergeAdapter
Let's take the case for a simple use. We have to show a layout of headers, then a list of topics, and a footer showing either loading or error. We have three different adapters-HeaderAdapter, TopicAdapter, and FooterAdapter-for each form.
We want to use the merge-adapter constructor to combine these three adapters and set the MergeAdapter instance result to recyclerview. The first is to fuse three adapters as seen here:

Val header: HeaderAdapter= …
Value ThemeAdapter: AdapterTopic= …
Adapter: FooterAdapter= …
Value MergeAdapter = AdapterMerge(headerAdapter, topicAdapter,
AdapterFooter)
The views in the recyclerview should make in the MergeAdapter constructor according to the order of the adapters. After that we need to invoke setAdapter as normal and pass the instance of themergeAdapter, as shown below:
MergeAdapter = RecyclerView.adapter
We achieve separation of concerns in this way:
Now we can do it in FooterAdapter to show the loading or the error case
If we have different view-types in topics then in TopicAdapter we can do it separately.
Browse MergeAdapter
Config. MergeAdapter
adapter uses its own viewHolders list, by default. If you have the same interface to use through multiple
adapters, you need to use MergeAdapter. Config. We can configure merge-adapters to three types:
 MergeAdapter. Config. StableIdMode.
 NO STABLE IDS
 The default mode for this. — adapter has its own viewfinder pool and disregards the secure IDs from subadapters.
MergeAdapter. Config. StableIdMode. ISOLATED STABLE IDS
In this mode, MergeAdapter collects all IDs from sub-adapters since two different viewHolders may return the same ID because they are not aware of each other. Then MergeAdapter isolates the ID pool of each RecyclerView. Adapter from one another, allowing it to overwrite the stable ID reported before reporting back to the Recycle reView.
Merge Adapter. Config. Stable Id Mode. SHARED STABLE IDS
In this mode, Merge Adapter does not isolate the pool of each adapter from each other, nor will it override ID's as it did in the previous instance.

Updating View Holders and Data

Instead of the general recycle review.adapter, I would suggest using ListAdapter which handles data changes for you. The next thing is the onlooker. To get the adapter location in recyclerview, we use View Holder.getAdapter Position, but you need to use ViewHolder.getBindingAdapter) (while you are running via the Merge adapter.
Merge Adapter properties
The properties of Merge Adapter are as follows.
View Holder
By default, each adapter has its own ViewHolder pool, with no reuse in between adapters. If multiple adapters present the same ViewHolder you can want instances between them to be reused.
You can do this with a MergeAdapter. Config file, where isolateViewType = null. And all of the combined adapters will use the same view set.
You should implement Adapter.getItemViewType to support different ViewHolder types.
Notifications shift with data
When one of the notify functions is called by an adapter component of a MergeAdapter, the MergeAdapter computes the new element positions before updating the RecyclerView.
If an adapter calls Adapter.notifyDataSetChanged, Adapter.notifyDataSetChanged will always calln MergeAdapter, rather than Adapter.notifyItemRangeChanged. As normal with RecyclerView, stop calling
Adapter.notifyDataSetChanged, choose more granular updates or using an automatic Adapter Implementation to do so.
Find Place of ViewHolder
In the past, you might have used ViewHolder.getAdapterPosition to get a ViewHolder position within the adapter. Now use View Holder.getBindingAdapterPosition because we're combining multiple adapters.
Using ViewHolder.getBindingAdapter to get the adapter that last attached a ViewHolder, in the case you are sharing ViewHolders.
Conclusion
That is it! If you want to display various types of data sequentially, which would benefit from being encapsulated in their own adapters, start using MergeAdapter. You can learn more through android online training.

Comments

Popular posts from this blog

Android App Project Package Structure (Android Studio)

ImageView and ImageButton in Android

Developing Android unit and instrumentation tests - Tutorial