Android SectionedRecyclerViewAdapter Section Header

时间:2017-05-19 07:23:55

标签: android android-recyclerview recycler-adapter sectionedrecyclerviewadapter

我使用来自luizgrp/SectionedRecyclerViewAdapter的SectionedRecyclerViewAdapter作为我的RecyclerView的适配器。

我们可以Section添加SectionedRecyclerViewAdapter Header布局,如下所示:

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中定义的旋转进度条。但我的问题是当部分仍在header时,loading state已经显示。如何在状态更改为loaded之前隐藏标题?

我想在状态更改为header后才向部分添加loaded。但似乎无法设置Section的标头只是在Section的构造函数中。

任何人都有任何想法?谢谢!

2 个答案:

答案 0 :(得分:3)

尝试覆盖SectionedRecyclerViewAdapter类并在onBindViewHolder替换

if (section.hasHeader())

通过

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

答案 1 :(得分:2)

我设法让它现在起作用,上面有亚历山大的提示。解决方法是:

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

谢谢!