blob: 2823b0176f3f2c64d01eb40194c28f07a98f6f19 [file] [log] [blame]
Alexandre Lision064e1e02013-10-01 16:18:42 -04001package org.sflphone.adapters;
alision9f7a6ec2013-05-24 16:26:26 -04002
alision84813a12013-05-27 17:40:39 -04003import java.util.LinkedHashMap;
4import java.util.Map;
5
Alexandre Lision064e1e02013-10-01 16:18:42 -04006import org.sflphone.R;
7
alision9f7a6ec2013-05-24 16:26:26 -04008import android.content.Context;
alision9f7a6ec2013-05-24 16:26:26 -04009import android.view.View;
10import android.view.ViewGroup;
alision84813a12013-05-27 17:40:39 -040011import android.widget.Adapter;
12import android.widget.ArrayAdapter;
alision9f7a6ec2013-05-24 16:26:26 -040013import android.widget.BaseAdapter;
alision9f7a6ec2013-05-24 16:26:26 -040014
alision84813a12013-05-27 17:40:39 -040015public class MenuAdapter extends BaseAdapter {
16 public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
17 public final ArrayAdapter<String> headers;
18 public final static int TYPE_SECTION_HEADER = 0;
alision9f7a6ec2013-05-24 16:26:26 -040019
alision84813a12013-05-27 17:40:39 -040020 public MenuAdapter(Context context) {
21 headers = new ArrayAdapter<String>(context, R.layout.item_menu_header);
alision9f7a6ec2013-05-24 16:26:26 -040022 }
23
alision84813a12013-05-27 17:40:39 -040024 public void addSection(String section, Adapter adapter) {
25 this.headers.add(section);
26 this.sections.put(section, adapter);
27 }
alision9f7a6ec2013-05-24 16:26:26 -040028
alision84813a12013-05-27 17:40:39 -040029 public Object getItem(int position) {
30 for (Object section : this.sections.keySet()) {
31 Adapter adapter = sections.get(section);
32 int size = adapter.getCount() + 1;
33
34 // check if position inside this section
35 if (position == 0)
36 return section;
37 if (position < size)
38 return adapter.getItem(position - 1);
39
40 // otherwise jump into next section
41 position -= size;
alision9f7a6ec2013-05-24 16:26:26 -040042 }
alision84813a12013-05-27 17:40:39 -040043 return null;
alision9f7a6ec2013-05-24 16:26:26 -040044 }
45
alision9f7a6ec2013-05-24 16:26:26 -040046 public int getCount() {
alision84813a12013-05-27 17:40:39 -040047 // total together all sections, plus one for each section header
48 int total = 0;
49 for (Adapter adapter : this.sections.values())
50 total += adapter.getCount() + 1;
51 return total;
alision9f7a6ec2013-05-24 16:26:26 -040052 }
53
54 @Override
alision84813a12013-05-27 17:40:39 -040055 public int getViewTypeCount() {
56 // assume that headers count as one, then total all sections
57 int total = 1;
58 for (Adapter adapter : this.sections.values())
59 total += adapter.getViewTypeCount();
60 return total;
alision9f7a6ec2013-05-24 16:26:26 -040061 }
62
63 @Override
alision84813a12013-05-27 17:40:39 -040064 public int getItemViewType(int position) {
65 int type = 1;
66 for (Object section : this.sections.keySet()) {
67 Adapter adapter = sections.get(section);
68 int size = adapter.getCount() + 1;
alision9f7a6ec2013-05-24 16:26:26 -040069
alision84813a12013-05-27 17:40:39 -040070 // check if position inside this section
71 if (position == 0)
72 return TYPE_SECTION_HEADER;
73 if (position < size)
74 return type + adapter.getItemViewType(position - 1);
75
76 // otherwise jump into next section
77 position -= size;
78 type += adapter.getViewTypeCount();
79 }
80 return -1;
81 }
82
83 public boolean areAllItemsSelectable() {
84 return false;
85 }
86
87 @Override
88 public boolean isEnabled(int position) {
89 return (getItemViewType(position) != TYPE_SECTION_HEADER);
90 }
91
92 @Override
93 public View getView(int position, View convertView, ViewGroup parent) {
94 int sectionnum = 0;
95 for (Object section : this.sections.keySet()) {
96 Adapter adapter = sections.get(section);
97 int size = adapter.getCount() + 1;
98
99 // check if position inside this section
100 if (position == 0)
101 return headers.getView(sectionnum, convertView, parent);
102 if (position < size)
103 return adapter.getView(position - 1, convertView, parent);
104
105 // otherwise jump into next section
106 position -= size;
107 sectionnum++;
108 }
109 return null;
110 }
111
112 @Override
113 public long getItemId(int position) {
114 return position;
115 }
116
117}