Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple RecyclerView.Adapter for use with single RecyclerView (Android)

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.

like image 268
wrik Avatar asked Feb 11 '15 03:02

wrik


2 Answers

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))
like image 112
Erfan Eghterafi Avatar answered Oct 19 '22 23:10

Erfan Eghterafi


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);
like image 22
Gustavo Avatar answered Oct 19 '22 22:10

Gustavo