SectionedRecyclerView
介绍:
一个让RecyclerView分段(section)显示,并且提供header和footer的adapter类。
运行效果:
使用说明:
要使用这个库,你需要继承SectionedRecyclerView<H, VH, F>,其中:
-
H是一个继承自RecyclerView.ViewHolder 用于hold section header的类。
-
VH是一个继承自RecyclerView.ViewHolder 用于hold 普通item的类。
-
F是一个继承自RecyclerView.ViewHolder 用于hold section footer的类。
根据本repository发布的sample:
- 1. 创建一个继承自SectionedRecyclerView的类:
public class CountSectionAdapter extends SectionedRecyclerViewAdapter<CountHeaderViewHolder,
CountItemViewHolder,
CountFooterViewHolder>
- 2.实现相应的方法:
@Override
protected int getItemCountForSection(int section) {
return section + 1;
}
@Override
protected int getSectionCount() {
return 5;
}
@Override
protected boolean hasFooterInSection(int section) {
return true;
}
protected LayoutInflater getLayoutInflater(){
return LayoutInflater.from(context);
}
@Override
protected CountHeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.view_count_header, parent, false);
return new CountHeaderViewHolder(view);
}
@Override
protected CountFooterViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.view_count_footer, parent, false);
return new CountFooterViewHolder(view);
}
@Override
protected CountItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.view_count_item, parent, false);
return new CountItemViewHolder(view);
}
@Override
protected void onBindSectionHeaderViewHolder(CountHeaderViewHolder holder, int section) {
holder.render("Section " + (section + 1));
}
@Override
protected void onBindSectionFooterViewHolder(CountFooterViewHolder holder, int section) {
holder.render("Footer " + (section + 1));
}
protected int\[\] colors = new int\[\]{0xfff44336, 0xff2196f3, 0xff009688, 0xff8bc34a, 0xffff9800};
@Override
protected void onBindItemViewHolder(CountItemViewHolder holder, int section, int position) {
holder.render(String.valueOf(position + 1), colors\[section\]);
}
- 3.如果你使用的是GridLayoutManager,你需要为它设置一个SectionedSpanSizeLookup确保header和footer的横跨整个RecyclerView的宽度(也就是列数):
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
SectionedSpanSizeLookup lookup = new SectionedSpanSizeLookup(adapter, layoutManager);
layoutManager.setSpanSizeLookup(lookup);
recycler.setLayoutManager(layoutManager);
- 4. 结果如下:
更简单的版本
大多数情况下,你只是需要这个adapter的简化版本,没有footer,而header也只是一个标题而已。对于这种情况,你可以使用SimpleSectionedAdapter,其中VH是一个继承自ViewHolder,用来hold普通item的类。
这种情况下,你需要实现以下方法:
@Override
protected String getSectionHeaderTitle(int section) {
return section == 0 ? "Today" : "Tomorrow";
}
@Override
protected int getSectionCount() {
return 2;
}
@Override
protected int getItemCountForSection(int section) {
return 3;
}
@Override
protected AgendaItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.view_agenda_item, parent, false);
return new AgendaItemViewHolder(view);
}
protected String\[\]\[\] agenda = {{"Meeting", "Phone call", "Interview"},
{"Basket match", "Grocery shopping", "Taking a nap"}};
@Override
protected void onBindItemViewHolder(AgendaItemViewHolder holder, int section, int position) {
holder.render(agenda\[section\]\[position\]);
}
结果如下:
获取
SectionedRecyclerView可以通过JCenter获得。,在 build.gradle文件中添加如下依赖就可以在项目中使用:
dependencies{
compile 'com.truizlop.sectionedrecyclerview:library:1.0.0'
}
已下载
0