Add Many Sections In An Android ListView
Posted by blogmeister on
October 15, 2011
I have seen custom code made by fellow Android developers on creating a ListView widget much like the one you see in the Contacts app. However, I wanted my ListView to display more than 1 section just like in the image above (click to enlarge).
The simplest adapter I found is Commonware’s called SectionedAdapter. However, it caters to only 1 header per set so I modified his existing code to cater adding a header without any items within it.
Just do it like this
|
1 2 3 4 |
adapter.addSection("Month: February", null); adapter.addSection("Day: 27 February", null); adapater.addSection("NNNNNNNNNNNNNN", new MyArrayAdapterInstance(this.context, myarraylist)); listview.setAdapter(adapter); |
I also added an extra parameter to the addSection() method in case you want to have a different background color. Just do it like this
|
1 |
adapter.addSection("Month: February", null, R.color.my_custom_color); |
And here is the full source of my modified class of Commonware’s SectionedAdapter class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
abstract public class SectionedAdapter extends BaseAdapter { abstract protected View getHeaderView(String caption, int index, View convertView, ViewGroup parent); private List<Section> sections = new ArrayList<Section>(); private static int TYPE_SECTION_HEADER = 0; public SectionedAdapter() { super(); } public void addSection(String caption, Adapter adapter) { sections.add(new Section(caption, adapter, -1)); } public void addSection(String caption, Adapter adapter, int color) { sections.add(new Section(caption, adapter, color)); } public Object getItem(int position) { for (Section section : this.sections) { if (position == 0) { return(section); } int size = 1; if (section.adapter != null) size = section.adapter.getCount() + 1; if (position < size) { if (section.adapter != null) return(section.adapter.getItem(position - 1)); } position -= size; } return(null); } public int getCount() { int total = 0; for (Section section : this.sections) { if (section.adapter == null) total ++; else total += section.adapter.getCount() + 1; // add one for header } return(total); } public int getViewTypeCount() { int total = 1; // one for the header, plus those from sections for (Section section : this.sections) { if (section.adapter != null) total += section.adapter.getViewTypeCount(); } return(total); } public int getItemViewType(int position) { int typeOffset = TYPE_SECTION_HEADER+1; // start counting from here for (Section section : this.sections) { if (position == 0) { return(TYPE_SECTION_HEADER); } int size = 1; if (section.adapter != null) size = section.adapter.getCount() + 1; if (position < size) { if (section.adapter == null) return(typeOffset); else return(typeOffset + section.adapter.getItemViewType(position - 1)); } position -= size; if (section.adapter != null) typeOffset += section.adapter.getViewTypeCount(); } return(-1); } public boolean areAllItemsSelectable() { return(false); } public boolean isEnabled(int position) { return (getItemViewType(position) != TYPE_SECTION_HEADER); } @Override public View getView(int position, View convertView, ViewGroup parent) { int sectionIndex = 0; for (Section section : this.sections) { if (position == 0) { View view = getHeaderView(section.caption, sectionIndex, convertView, parent); if (section.color > -1) view.setBackgroundResource(section.color); return view; } int size = 1; if (section.adapter != null) size = section.adapter.getCount() + 1; if (position < size) if (section.adapter != null) return section.adapter.getView(position - 1, convertView, parent); position -= size; sectionIndex++; } return(null); } @Override public long getItemId(int position) { return(position); } class Section { String caption; Adapter adapter; int color; Section(String caption, Adapter adapter, int color) { this.caption = caption; this.adapter = adapter; this.color = color; } } } |










