Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.customlistview;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Adapter;
- import android.widget.ArrayAdapter;
- import android.widget.BaseAdapter;
- public class SectionAdapter extends BaseAdapter {
- protected final static int TYPE_SECTION_HEADER = 0;
- protected final ArrayAdapter<String> headers;
- protected final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
- protected boolean showHeaders = true;
- public SectionAdapter(Context context) {
- headers = new ArrayAdapter<String>(context, R.layout.header_row);
- }
- public void addSection(String header, Adapter adapter) {
- headers.add(header);
- sections.put(header, adapter);
- }
- public void clear () {
- headers.clear();
- sections.clear();
- }
- public boolean areAllItemsSelectable() {
- return false;
- }
- public int getCount() {
- int total = 0;
- for (Adapter adapter : this.sections.values()) {
- int size = adapter.getCount();
- if (size == 0)
- continue;
- total += size + (showHeaders ? 1 : 0);
- }
- return total;
- }
- public Object getItem(int position) {
- for (Object section : this.sections.keySet()) {
- Adapter adapter = sections.get(section);
- int size = adapter.getCount();
- if (size == 0)
- continue;
- size += (showHeaders ? 1 : 0);
- if (showHeaders) {
- if (position == 0)
- return section;
- if (position < size)
- return adapter.getItem(position - 1);
- } else if (position < size) {
- return adapter.getItem(position);
- }
- position -= size;
- }
- return null;
- }
- public long getItemId(int position) {
- return position;
- }
- public int getItemViewType(int position) {
- int type = 1;
- for (Object section : this.sections.keySet()) {
- Adapter adapter = sections.get(section);
- int size = adapter.getCount();
- if (size == 0)
- continue;
- size += (showHeaders ? 1 : 0);
- if (showHeaders) {
- if (position == 0)
- return TYPE_SECTION_HEADER;
- if (position < size)
- return type + adapter.getItemViewType(position - 1);
- } else if (position < size) {
- return type + adapter.getItemViewType(position);
- }
- position -= size;
- type += adapter.getViewTypeCount();
- }
- return -1;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- int sectionnum = 0;
- for (Object section : this.sections.keySet()) {
- Adapter adapter = sections.get(section);
- int size = adapter.getCount();
- if (size == 0) {
- sectionnum++;
- continue;
- }
- size += (showHeaders ? 1 : 0);
- if (showHeaders) {
- if (position == 0)
- return headers.getView(sectionnum, convertView, parent);
- if (position < size)
- return adapter.getView(position - 1, convertView, parent);
- } else if (position < size) {
- return adapter.getView(position, convertView, parent);
- }
- position -= size;
- sectionnum++;
- }
- return null;
- }
- public int getViewTypeCount() {
- int total = 1;
- for (Adapter adapter : this.sections.values())
- total += adapter.getViewTypeCount();
- return total;
- }
- public boolean isEnabled(int position) {
- return (getItemViewType(position) != TYPE_SECTION_HEADER);
- }
- public void setShowHeaders(boolean showHeaders) {
- this.showHeaders = showHeaders;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement