Recylerview, CursorAdapter with Header views in between

805 Views Asked by At

I have been using cursor along with recyclerview.

I have a queried cursor object (passed from loader) and an array of header Strings[].

String headers[] = {"apples", "bananas"...};

Now I want to show items as

Apples
cursor row 1
cursor row 2
cursor row 3
Bananas
cursor row 4
cursor row 5

I don't want to tweak with getItemCount() method. So, planning to pass a single cursor with proper length.

One possible way is to use MatrixCursor and MergeCursor to add dummy rows as mentioned here: Adding rows into Cursor manually. This is fine but MergeCursor aligns headers and cursor data one after the other.

Wanted to explore ways in which a final cursor can be achieved with the correct header and item positions.

1

There are 1 best solutions below

2
On

You can use the library SectionedRecyclerViewAdapter to group your data into sections and add a header to each section.

First create a Section class:

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.title.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);
    }
}

Then you set up the RecyclerView with your Sections:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Add your Sections to the adapter
sectionAdapter.addSection(new MySection(headers[0], applesList));
sectionAdapter.addSection(new MySection(headers[1], bananasList));

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);

With the example above you will have to work on how to convert your Cursor into List<String> but you can change the MySection class to receive a Cursor instead of a List<String>.