Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.adapters; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 2 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 3 | import java.util.LinkedHashMap; |
| 4 | import java.util.Map; |
| 5 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 6 | import org.sflphone.R; |
| 7 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 8 | import android.content.Context; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 9 | import android.view.View; |
| 10 | import android.view.ViewGroup; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 11 | import android.widget.Adapter; |
| 12 | import android.widget.ArrayAdapter; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 13 | import android.widget.BaseAdapter; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 14 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 15 | public 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; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 19 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 20 | public MenuAdapter(Context context) { |
| 21 | headers = new ArrayAdapter<String>(context, R.layout.item_menu_header); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 22 | } |
| 23 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 24 | public void addSection(String section, Adapter adapter) { |
| 25 | this.headers.add(section); |
| 26 | this.sections.put(section, adapter); |
| 27 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 28 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 29 | 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; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 42 | } |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 43 | return null; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 44 | } |
| 45 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 46 | public int getCount() { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 47 | // 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; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 55 | 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; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 64 | 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; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 69 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 70 | // 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 | } |