Android SectionedRecyclerViewAdapter Section Header

2.4k Views Asked by At

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!

2

There are 2 best solutions below

1
On BEST ANSWER

Try to override the SectionedRecyclerViewAdapter class and in onBindViewHolder replace

if (section.hasHeader())

by

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

0
On

I managed to make it work now, with hints above from Alexandre. The workaround is:

// loading state - set no header so header section is hidden
section1.setHasHeader(false);
section1.setState(Section.State.LOADING);

....
....

// loaded state - set has header so header section is shown
section1.setHasHeader(true);
section1.setState(Section.State.LOADED);

Thanks!