OSDN Git Service

70f01b3c75e70cf8b2ff3b07acf72adde8a5edcb
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / widgets / SeparatedListAdapter.java
1
2 package com.cyanogenmod.eleven.widgets;
3
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;
10
11 import com.cyanogenmod.eleven.R;
12
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15
16 public class SeparatedListAdapter extends BaseAdapter {
17
18     public final Map<String, Adapter> mSections = new LinkedHashMap<String, Adapter>();
19
20     public final ArrayAdapter<String> mHeaders;
21
22     public final static int TYPE_SECTION_HEADER = 0;
23
24     /**
25      * Constructor of <code>SeparatedListAdapter</code>
26      *
27      * @param context The {@link Context} to use.
28      */
29     public SeparatedListAdapter(final Context context) {
30         mHeaders = new ArrayAdapter<String>(context, R.layout.list_header);
31     }
32
33     /**
34      * {@inheritDoc}
35      */
36     @Override
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;
41
42             // check if position inside this section
43             if (position == 0) {
44                 return section;
45             }
46             if (position < size) {
47                 return adapter.getItem(position - 1);
48             }
49
50             // otherwise jump into next section
51             position -= size;
52         }
53         return null;
54     }
55
56     /**
57      * {@inheritDoc}
58      */
59     @Override
60     public int getCount() {
61         // total together all mSections, plus one for each section header
62         int total = 0;
63         for (final Adapter adapter : mSections.values()) {
64             total += adapter.getCount() + 1;
65         }
66         return total;
67     }
68
69     /**
70      * {@inheritDoc}
71      */
72     @Override
73     public int getViewTypeCount() {
74         // assume that mHeaders count as one, then total all mSections
75         int total = 1;
76         for (final Adapter adapter : mSections.values()) {
77             total += adapter.getViewTypeCount();
78         }
79         return total;
80     }
81
82     /**
83      * {@inheritDoc}
84      */
85     @Override
86     public int getItemViewType(int position) {
87         int type = 1;
88         for (final Object section : mSections.keySet()) {
89             final Adapter adapter = mSections.get(section);
90             final int size = adapter.getCount() + 1;
91
92             // check if position inside this section
93             if (position == 0) {
94                 return TYPE_SECTION_HEADER;
95             }
96             if (position < size) {
97                 return type + adapter.getItemViewType(position - 1);
98             }
99
100             // otherwise jump into next section
101             position -= size;
102             type += adapter.getViewTypeCount();
103         }
104         return -1;
105     }
106
107     public boolean areAllItemsSelectable() {
108         return false;
109     }
110
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public boolean isEnabled(final int position) {
116         return getItemViewType(position) != TYPE_SECTION_HEADER;
117     }
118
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public View getView(int position, final View convertView, final ViewGroup parent) {
124         int sectionnum = 0;
125         for (final Object section : mSections.keySet()) {
126             final Adapter adapter = mSections.get(section);
127             final int size = adapter.getCount() + 1;
128
129             // check if position inside this section
130             if (position == 0) {
131                 return mHeaders.getView(sectionnum, convertView, parent);
132             }
133             if (position < size) {
134                 return adapter.getView(position - 1, convertView, parent);
135             }
136
137             // otherwise jump into next section
138             position -= size;
139             sectionnum++;
140         }
141         return null;
142     }
143
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public long getItemId(final int position) {
149         return position;
150     }
151
152     public void addSection(final String section, final Adapter adapter) {
153         mHeaders.add(section);
154         mSections.put(section, adapter);
155     }
156
157 }