4

Hi I'm working on an app, in it I have to populate a data recyclerview with Firebase, ready. Now I want to do is fill out a recyclerview with data firebase but has sections which would be the father that host data firebase, I'm a little lost with this, and I think it is in the adapter Recyclerview where not as do, I hope you can help me. Sorry for my English, it's not good.

Image with what I want to do

Thanks! and i am sorry for my english.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Latm
  • 183
  • 10
  • I'm also looking for sticky-headers feature for FirebaseUI, but unsuccessful. Any help is welcome ! :) – PAD Apr 14 '16 at 09:11

1 Answers1

1

Use this library SectionedRecyclerViewAdapter to group your data into sections.

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

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 for each year
MySection section1 = new MySection("Section 1", section1DataList);
MySection section2 = new MySection("Section 2", section2DataList);

// 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);
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71