Search code examples
androidandroid-recyclerviewsectionedrecyclerviewadapter

Android SectionedRecyclerViewAdapter Section Header


I am using SectionedRecyclerViewAdapter from luizgrp/SectionedRecyclerViewAdapter as adapter for my RecyclerView.

We can add Section to SectionedRecyclerViewAdapter with Header layout, as below:

public class Section1 extends Section {
    public Section1 () {
        super(
                R.layout.section_1_header,
                R.layout.section_1_item,
                R.layout.section_1_loading,
                R.layout.section_1_failed
        );
    }

    .....
}


.....


Section1 section1 = new Section1();
section1.setState(Section.State.LOADING);

SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
sectionAdapter.addSection(section1);

recyclerView.setAdapter(sectionAdapter);

During loading state, I am showing a spinning progress bar as defined in section_1_loading.xml. But my problem is the header is already shown when section is still in loading state. How do I hide header before state changes to loaded?

I thought about only add header to Section after state changes to loaded. But seems can't as only way to set Section's header is in Section's constructor.

Anyone has any idea? Thanks!


Solution

  • Try to override the SectionedRecyclerViewAdapter class and in onBindViewHolder replace

    if (section.hasHeader())

    by

    if (section.hasHeader() && section.getState() != Section.State.LOADING)