12 package com.freerdp.freerdpcore.utils;
14 import android.content.Context;
15 import android.view.View;
16 import android.view.ViewGroup;
17 import android.widget.Adapter;
18 import android.widget.ArrayAdapter;
19 import android.widget.BaseAdapter;
21 import com.freerdp.freerdpcore.R;
23 import java.util.LinkedHashMap;
29 public final static int TYPE_SECTION_HEADER = 0;
30 public final Map<String, Adapter> sections =
new LinkedHashMap<>();
31 public final ArrayAdapter<String> headers;
35 headers =
new ArrayAdapter<>(context, R.layout.list_header);
38 public void addSection(String section, Adapter adapter)
40 this.headers.add(section);
41 this.sections.put(section, adapter);
44 public void setSectionTitle(
int section, String title)
46 String oldTitle = this.headers.getItem(section);
49 this.headers.remove(oldTitle);
50 this.headers.insert(title, section);
53 Adapter adapter = this.sections.get(oldTitle);
54 this.sections.remove(oldTitle);
55 this.sections.put(title, adapter);
58 public Object getItem(
int position)
60 for (
int i = 0; i < headers.getCount(); i++)
62 String section = headers.getItem(i);
63 Adapter adapter = sections.get(section);
66 if (adapter.getCount() > 0)
68 int size = adapter.getCount() + 1;
74 return adapter.getItem(position - 1);
88 for (Adapter adapter : this.sections.values())
89 total += ((adapter.getCount() > 0) ? adapter.getCount() + 1 : 0);
93 public int getViewTypeCount()
97 for (Adapter adapter : this.sections.values())
98 total += adapter.getViewTypeCount();
102 public int getItemViewType(
int position)
105 for (
int i = 0; i < headers.getCount(); i++)
107 String section = headers.getItem(i);
108 Adapter adapter = sections.get(section);
111 if (adapter.getCount() > 0)
113 int size = adapter.getCount() + 1;
117 return TYPE_SECTION_HEADER;
119 return type + adapter.getItemViewType(position - 1);
123 type += adapter.getViewTypeCount();
129 public boolean areAllItemsSelectable()
134 public boolean isEnabled(
int position)
136 return (getItemViewType(position) != TYPE_SECTION_HEADER);
139 @Override
public View getView(
int position, View convertView, ViewGroup parent)
142 for (
int i = 0; i < headers.getCount(); i++)
144 String section = headers.getItem(i);
145 Adapter adapter = sections.get(section);
148 if (adapter.getCount() > 0)
150 int size = adapter.getCount() + 1;
154 return headers.getView(sectionnum, convertView, parent);
156 return adapter.getView(position - 1,
null, parent);
166 @Override
public long getItemId(
int position)
168 for (
int i = 0; i < headers.getCount(); i++)
170 String section = headers.getItem(i);
171 Adapter adapter = sections.get(section);
172 if (adapter.getCount() > 0)
174 int size = adapter.getCount() + 1;
178 return adapter.getItemId(position - 1);
187 public String getSectionForPosition(
int position)
190 for (
int i = 0; i < headers.getCount(); i++)
192 String section = headers.getItem(i);
193 Adapter adapter = sections.get(section);
194 if (adapter.getCount() > 0)
196 int size = adapter.getCount() + 1;
199 if (position >= curPos && position < (curPos + size))