blob: 700d9e0fb028eeb0bb42eba9d872d53b1c8bc9fa [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 Object getItem(int position) {
25 for (Object section : this.sections.keySet()) {
26 Adapter adapter = sections.get(section);
27 int size = adapter.getCount() + 1;
28
29 // check if position inside this section
30 if (position == 0)
31 return section;
32 if (position < size)
33 return adapter.getItem(position - 1);
34
35 // otherwise jump into next section
36 position -= size;
alision9f7a6ec2013-05-24 16:26:26 -040037 }
alision84813a12013-05-27 17:40:39 -040038 return null;
alision9f7a6ec2013-05-24 16:26:26 -040039 }
40
alision9f7a6ec2013-05-24 16:26:26 -040041 public int getCount() {
alision84813a12013-05-27 17:40:39 -040042 // total together all sections, plus one for each section header
43 int total = 0;
44 for (Adapter adapter : this.sections.values())
45 total += adapter.getCount() + 1;
46 return total;
alision9f7a6ec2013-05-24 16:26:26 -040047 }
48
49 @Override
alision84813a12013-05-27 17:40:39 -040050 public int getViewTypeCount() {
51 // assume that headers count as one, then total all sections
52 int total = 1;
53 for (Adapter adapter : this.sections.values())
54 total += adapter.getViewTypeCount();
55 return total;
alision9f7a6ec2013-05-24 16:26:26 -040056 }
57
58 @Override
alision84813a12013-05-27 17:40:39 -040059 public int getItemViewType(int position) {
60 int type = 1;
61 for (Object section : this.sections.keySet()) {
62 Adapter adapter = sections.get(section);
63 int size = adapter.getCount() + 1;
alision9f7a6ec2013-05-24 16:26:26 -040064
alision84813a12013-05-27 17:40:39 -040065 // check if position inside this section
66 if (position == 0)
67 return TYPE_SECTION_HEADER;
68 if (position < size)
69 return type + adapter.getItemViewType(position - 1);
70
71 // otherwise jump into next section
72 position -= size;
73 type += adapter.getViewTypeCount();
74 }
75 return -1;
76 }
77
78 public boolean areAllItemsSelectable() {
79 return false;
80 }
81
82 @Override
83 public boolean isEnabled(int position) {
84 return (getItemViewType(position) != TYPE_SECTION_HEADER);
85 }
86
87 @Override
88 public View getView(int position, View convertView, ViewGroup parent) {
89 int sectionnum = 0;
90 for (Object section : this.sections.keySet()) {
91 Adapter adapter = sections.get(section);
92 int size = adapter.getCount() + 1;
93
94 // check if position inside this section
95 if (position == 0)
96 return headers.getView(sectionnum, convertView, parent);
97 if (position < size)
98 return adapter.getView(position - 1, convertView, parent);
99
100 // otherwise jump into next section
101 position -= size;
102 sectionnum++;
103 }
104 return null;
105 }
106
107 @Override
108 public long getItemId(int position) {
109 return position;
110 }
111
112}