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.
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))
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With