2 package com.cyanogenmod.eleven.widgets;
4 import android.content.Context;
5 import android.view.View;
6 import android.view.ViewGroup;
7 import android.widget.Adapter;
8 import android.widget.ArrayAdapter;
9 import android.widget.BaseAdapter;
11 import com.cyanogenmod.eleven.R;
13 import java.util.LinkedHashMap;
16 public class SeparatedListAdapter extends BaseAdapter {
18 public final Map<String, Adapter> mSections = new LinkedHashMap<String, Adapter>();
20 public final ArrayAdapter<String> mHeaders;
22 public final static int TYPE_SECTION_HEADER = 0;
25 * Constructor of <code>SeparatedListAdapter</code>
27 * @param context The {@link Context} to use.
29 public SeparatedListAdapter(final Context context) {
30 mHeaders = new ArrayAdapter<String>(context, R.layout.list_header);
37 public Object getItem(int position) {
38 for (final Object section : mSections.keySet()) {
39 final Adapter adapter = mSections.get(section);
40 final int size = adapter.getCount() + 1;
42 // check if position inside this section
46 if (position < size) {
47 return adapter.getItem(position - 1);
50 // otherwise jump into next section
60 public int getCount() {
61 // total together all mSections, plus one for each section header
63 for (final Adapter adapter : mSections.values()) {
64 total += adapter.getCount() + 1;
73 public int getViewTypeCount() {
74 // assume that mHeaders count as one, then total all mSections
76 for (final Adapter adapter : mSections.values()) {
77 total += adapter.getViewTypeCount();
86 public int getItemViewType(int position) {
88 for (final Object section : mSections.keySet()) {
89 final Adapter adapter = mSections.get(section);
90 final int size = adapter.getCount() + 1;
92 // check if position inside this section
94 return TYPE_SECTION_HEADER;
96 if (position < size) {
97 return type + adapter.getItemViewType(position - 1);
100 // otherwise jump into next section
102 type += adapter.getViewTypeCount();
107 public boolean areAllItemsSelectable() {
115 public boolean isEnabled(final int position) {
116 return getItemViewType(position) != TYPE_SECTION_HEADER;
123 public View getView(int position, final View convertView, final ViewGroup parent) {
125 for (final Object section : mSections.keySet()) {
126 final Adapter adapter = mSections.get(section);
127 final int size = adapter.getCount() + 1;
129 // check if position inside this section
131 return mHeaders.getView(sectionnum, convertView, parent);
133 if (position < size) {
134 return adapter.getView(position - 1, convertView, parent);
137 // otherwise jump into next section
148 public long getItemId(final int position) {
152 public void addSection(final String section, final Adapter adapter) {
153 mHeaders.add(section);
154 mSections.put(section, adapter);