Reputation: 213
I have a json string which I break up into 4 datasets, each dataset has it's own RecyclerView.Adapter.
I want to output all adapters in the same RecyclerView ideally with StickyHeaders for each adapter.
I believe this can be done using ListViews via CommonsWare's CWAC-merge (https://github.com/commonsguy/cwac-merge)
Any ideas as to how this can, or should be done with RecyclerView? I am an amature developer so the less abstract the response the better ;-D
Thanks.
Upvotes: 11
Views: 8156
Reputation: 5585
in new update of Recyclerview widget you can use MergeAdapter class
import androidx.recyclerview.widget.MergeAdapter
instantiate adapters:
private val mfirstAdapter = FirstAdapter()
private val mSecondAdapter = SecondAdapter()
// here is MergeAdapter class
private val adapter = MergeAdapter(mfirstAdapter , mSecondAdapter )
and then usage:
binding.recycler.adapter = adapter
how to update adapter: (using .submitList if use diffUtils, otherwise use notifyDataSetChaned())
mfirstAdapter.submitList(myDataArrayList.subList(0, 10))
mSecondAdapter.submitList(myDataArrayList.subList(10, list.size - 1))
Upvotes: 6
Reputation: 71
I made a class that do exactly what u want https://gist.github.com/agustindev/e2faa7ef6a7b61cc0eafeeaf81e4e434
...
val multipleAdapter = MultipleAdapter()
val adapter1 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
val adapter2 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
val adapter3 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
multipleAdapter.addAdapter(adapter1)
multipleAdapter.addAdapter(adapter2)
multipleAdapter.addAdapter(adapter3)
recyclerView.adapter = multipleAdapter
Upvotes: 0
Reputation: 6988
You achieve that with only one adapter: SectionedRecyclerViewAdapter. With this library you can group your items into Sections and you can optionally add headers and/or footers to them.
First create a Section class to group your items:
class MySection extends StatelessSection {
String title;
List<String> list;
public MySection(String title, List<String> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);
this.title = title;
this.list = list;
}
@Override
public int getContentItemsTotal() {
return list.size(); // number of items of this section
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
// bind your view here
itemHolder.tvItem.setText(list.get(position));
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvItem.setText(title);
}
public void addRow(String item) {
this.list.add(item);
}
}
Then you set up the RecyclerView with your Sections:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with the list of data
MySection section1 = new MySection("Header of section 1", section1List);
MySection section2 = new MySection("Header of section 2", section2List);
// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
Upvotes: 1
Reputation: 11
You can look at GroupAdapter
GroupAdapter.Builder builder = new GroupAdapter.Builder();
builder.add(firstAdapter);
builder.add(secondAdapter);
GroupAdapter groupAdapter = builder.build();
recyclerView.setAdapter(groupAdapter);
Upvotes: 0
Reputation: 10980
I would go for this library: https://github.com/martijnvdwoude/recycler-view-merge-adapter
Check the documentation on the Github page for examples on how to use this in your code.
Extract from the readme:
RecyclerView myRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
// Create new merge adapter
RecyclerViewMergeAdapter mergeAdapter = new RecyclerViewMergeAdapter();
// Add any number of subadapters to merge adapter
MyRecyclerViewSubAdapter subAdapter1 = new MyRecyclerViewSubAdapter();
MyRecyclerViewSubAdapter subAdapter2 = new MyRecyclerViewSubAdapter();
mergeAdapter.addAdapter(subAdapter1);
mergeAdapter.addAdapter(subAdapter2);
// Set merge adapter to RecyclerView
myRecyclerView.setAdapter(mergeAdapter);
Upvotes: 0