Android SectionedRecyclerViewAdapter 部分 Header

Android SectionedRecyclerViewAdapter Section Header

我正在使用 luizgrp/SectionedRecyclerViewAdapter 的 SectionedRecyclerViewAdapter 作为我的 RecyclerView 的适配器。

我们可以用Header布局将Section添加到SectionedRecyclerViewAdapter,如下:

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

loading 状态期间,我显示了 section_1_loading.xml 中定义的旋转进度条。但我的问题是当部分仍在 loading state 中时 header 已经显示。如何在状态更改为 loaded 之前隐藏 header?

我考虑过在状态更改为 loaded 后仅将 header 添加到部分。但是似乎不能因为设置 Section 的 header 的唯一方法是在 Section 的构造函数中。

有人知道吗?谢谢!

尝试覆盖 SectionedRecyclerViewAdapter class 并在 onBindViewHolder 中替换

if (section.hasHeader())

来自

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

根据 Alexandre 的提示,我现在设法让它工作了。解决方法是:

// 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);

谢谢!