I am developing an android app in which I'm storing two different types of information on 'FirebaseDatabase`.
Then in the MainActivity
, I'm retrieving them and showing to users in a RecyclerView
. Both information are meant to be shown in different layouts, i.e., the layouts for both of them are different and that's why I have two make two different Model class and now have 2 different adapters. I'm using FastAdapter by @MikePenz
So, what I did is I set the adapter on recyclerview in the same sequence as the information is fetched from database:
1.
public void prepareDataOfSRequests() {
gModelClass = new GModelClass(postedBy, ***, ***, ***, "error", ***, formattedTime, currentLatitude, currentLongitude, utcFormatDateTime, userEmail, userID, null, ***, itemID);
fastItemAdapter.add(0, gModelClass);
recyclerView.setAdapter(fastItemAdapter);
recyclerView.smoothScrollToPosition(0);
emptyRVtext.setVisibility(View.INVISIBLE);
}
2.
public void prepareDataOfERequests() {
eModelClass = new EModelClass(***, ***, ***, ***, "error", ***, formattedTimeE, currentLatitudeE, currentLongitudeE, utcFormatDateTimeE, userEmailE, userIDE, null, ***, ***, ***, ***, itemID);
fastItemAdapterER.add(eventRequestsModelClass);
recyclerView.setAdapter(fastItemAdapterER);
recyclerView.smoothScrollToPosition(0);
emptyRVtext.setVisibility(View.INVISIBLE);
}
as the recyclerview is only one and I'm setting 2 different adapters one by one, the recyclerview is getting updated with the 2nd adapter and showing only it's content.
So, how can I show or set both the adapter to the same 'RecyclerView' and can show the content stored in both the adapters.
You cannot set two adapters on one RecyclerView. However you can make a custom Adapter that handles two types of items. Here is how:
Let's assume you need to display objects of two types Animals and Beverages. Let's handle the hardest scenario, that is, your objects require completely different layouts to display them and completely different
ViewHolders
. Here are theViewHolders
:First you add constants to your adapter representing your view types:
For the sake of simplicity lets assume you store your objects in a list:
First you need to implement
getItemViewType
:Next you use the item type inside
onCreateViewHolder
:Finally you bind proper view holder:
If you like, you definitely can write a middle layer to accept multiple adapter.
You can take a look at what I do if you are interested in. https://github.com/Jintin/MixAdapter