SectionedRecyclerView

这个一定要保存好,里面实现了第1段1个数据,第2段2个数据,第3段3个数据的效果


介绍:

一个让RecyclerView分段(section)显示,并且提供header和footer的adapter类。

运行效果:

使用说明:

要使用这个库,你需要继承SectionedRecyclerView,其中:

  • H是一个继承自RecyclerView.ViewHolder 用于hold section header的类。

  • VH是一个继承自RecyclerView.ViewHolder 用于hold 普通item的类。

  • F是一个继承自RecyclerView.ViewHolder 用于hold section footer的类。

根据本repository发布的sample:

  • 1. 创建一个继承自SectionedRecyclerView的类:


  1. public class CountSectionAdapter extends SectionedRecyclerViewAdapter<CountHeaderViewHolder,
  2.         CountItemViewHolder,
  3.         CountFooterViewHolder>
  • 2.实现相应的方法:


  1. @Override
  2. protected int getItemCountForSection(int section) {
  3.     return section + 1;
  4. }
  5.  
  6. @Override
  7. protected int getSectionCount() {
  8.     return 5;
  9. }
  10.  
  11. @Override
  12. protected boolean hasFooterInSection(int section) {
  13.     return true;
  14. }
  15.  
  16. protected LayoutInflater getLayoutInflater(){
  17.     return LayoutInflater.from(context);
  18. }
  19.  
  20. @Override
  21. protected CountHeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
  22.     View view = getLayoutInflater().inflate(R.layout.view_count_header, parent, false);
  23.     return new CountHeaderViewHolder(view);
  24. }
  25.  
  26. @Override
  27. protected CountFooterViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
  28.     View view = getLayoutInflater().inflate(R.layout.view_count_footer, parent, false);
  29.     return new CountFooterViewHolder(view);
  30. }
  31.  
  32. @Override
  33. protected CountItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
  34.     View view = getLayoutInflater().inflate(R.layout.view_count_item, parent, false);
  35.     return new CountItemViewHolder(view);
  36. }
  37.  
  38. @Override
  39. protected void onBindSectionHeaderViewHolder(CountHeaderViewHolder holder, int section) {
  40.     holder.render("Section " + (section + 1));
  41. }
  42.  
  43. @Override
  44. protected void onBindSectionFooterViewHolder(CountFooterViewHolder holder, int section) {
  45.     holder.render("Footer " + (section + 1));
  46. }
  47.  
  48. protected int[] colors = new int[]{0xfff44336, 0xff2196f3, 0xff009688, 0xff8bc34a, 0xffff9800};
  49. @Override
  50. protected void onBindItemViewHolder(CountItemViewHolder holder, int section, int position) {
  51.     holder.render(String.valueOf(position + 1), colors[section]);
  52. }
  • 3.如果你使用的是GridLayoutManager,你需要为它设置一个SectionedSpanSizeLookup确保header和footer的横跨整个RecyclerView的宽度(也就是列数):


  1. GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
  2. SectionedSpanSizeLookup lookup = new SectionedSpanSizeLookup(adapter, layoutManager);
  3. layoutManager.setSpanSizeLookup(lookup);
  4. recycler.setLayoutManager(layoutManager);
  • 4. 结果如下:

更简单的版本

大多数情况下,你只是需要这个adapter的简化版本,没有footer,而header也只是一个标题而已。对于这种情况,你可以使用SimpleSectionedAdapter,其中VH是一个继承自ViewHolder,用来hold普通item的类。

这种情况下,你需要实现以下方法:


  1. @Override
  2. protected String getSectionHeaderTitle(int section) {
  3.     return section == 0 ? "Today" : "Tomorrow";
  4. }
  5.  
  6. @Override
  7. protected int getSectionCount() {
  8.     return 2;
  9. }
  10.  
  11. @Override
  12. protected int getItemCountForSection(int section) {
  13.     return 3;
  14. }
  15.  
  16. @Override
  17. protected AgendaItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
  18.     LayoutInflater inflater = LayoutInflater.from(parent.getContext());
  19.     View view = inflater.inflate(R.layout.view_agenda_item, parent, false);
  20.     return new AgendaItemViewHolder(view);
  21. }
  22.  
  23. protected String[][] agenda = {{"Meeting", "Phone call", "Interview"},
  24.             {"Basket match", "Grocery shopping", "Taking a nap"}};
  25.  
  26. @Override
  27. protected void onBindItemViewHolder(AgendaItemViewHolder holder, int section, int position) {
  28.     holder.render(agenda[section][position]);
  29. }

结果如下:

获取

SectionedRecyclerView可以通过JCenter获得。,在 build.gradle文件中添加如下依赖就可以在项目中使用:


  1. dependencies{
  2.     compile 'com.truizlop.sectionedrecyclerview:library:1.0.0'
  3. }

你可能感兴趣的:(SectionedRecyclerView)