Advertisement
Guest User

SectionAdapter.java

a guest
Oct 5th, 2010
1,325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package com.test.customlistview;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5.  
  6. import android.content.Context;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Adapter;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.BaseAdapter;
  12.  
  13. public class SectionAdapter extends BaseAdapter {
  14.    protected final static int TYPE_SECTION_HEADER = 0;
  15.    protected final ArrayAdapter<String> headers;
  16.    protected final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
  17.    protected boolean showHeaders = true;
  18.  
  19.    public SectionAdapter(Context context) {
  20.      headers = new ArrayAdapter<String>(context, R.layout.header_row);
  21.    }
  22.  
  23.    public void addSection(String header, Adapter adapter) {
  24.      headers.add(header);
  25.      sections.put(header, adapter);
  26.    }
  27.  
  28.    public void clear () {
  29.      headers.clear();
  30.      sections.clear();
  31.    }
  32.    
  33.    public boolean areAllItemsSelectable() {
  34.      return false;
  35.    }
  36.  
  37.    public int getCount() {
  38.      int total = 0;
  39.  
  40.      for (Adapter adapter : this.sections.values()) {
  41.        int size = adapter.getCount();
  42.  
  43.        if (size == 0)
  44.          continue;
  45.  
  46.        total += size + (showHeaders ? 1 : 0);
  47.      }
  48.  
  49.      return total;
  50.    }
  51.  
  52.    public Object getItem(int position) {
  53.      for (Object section : this.sections.keySet()) {
  54.        Adapter adapter = sections.get(section);
  55.  
  56.        int size = adapter.getCount();
  57.  
  58.        if (size == 0)
  59.          continue;
  60.  
  61.        size += (showHeaders ? 1 : 0);
  62.  
  63.        if (showHeaders) {
  64.          if (position == 0)
  65.            return section;
  66.          if (position < size)
  67.            return adapter.getItem(position - 1);
  68.        } else if (position < size) {
  69.          return adapter.getItem(position);
  70.        }
  71.  
  72.        position -= size;
  73.      }
  74.  
  75.      return null;
  76.    }
  77.  
  78.    public long getItemId(int position) {
  79.      return position;
  80.    }
  81.  
  82.    public int getItemViewType(int position) {
  83.      int type = 1;
  84.  
  85.      for (Object section : this.sections.keySet()) {
  86.        Adapter adapter = sections.get(section);
  87.  
  88.        int size = adapter.getCount();
  89.  
  90.        if (size == 0)
  91.          continue;
  92.  
  93.        size += (showHeaders ? 1 : 0);
  94.  
  95.        if (showHeaders) {
  96.          if (position == 0)
  97.            return TYPE_SECTION_HEADER;
  98.          if (position < size)
  99.            return type + adapter.getItemViewType(position - 1);
  100.        } else if (position < size) {
  101.          return type + adapter.getItemViewType(position);
  102.        }
  103.  
  104.        position -= size;
  105.        type += adapter.getViewTypeCount();
  106.      }
  107.  
  108.      return -1;
  109.    }
  110.  
  111.    public View getView(int position, View convertView, ViewGroup parent) {
  112.      int sectionnum = 0;
  113.      for (Object section : this.sections.keySet()) {
  114.        Adapter adapter = sections.get(section);
  115.  
  116.        int size = adapter.getCount();
  117.  
  118.        if (size == 0) {
  119.          sectionnum++;
  120.          continue;
  121.        }
  122.  
  123.        size += (showHeaders ? 1 : 0);
  124.  
  125.        if (showHeaders) {
  126.          if (position == 0)
  127.            return headers.getView(sectionnum, convertView, parent);
  128.          if (position < size)
  129.            return adapter.getView(position - 1, convertView, parent);
  130.        } else if (position < size) {
  131.          return adapter.getView(position, convertView, parent);
  132.        }
  133.  
  134.        position -= size;
  135.        sectionnum++;
  136.      }
  137.  
  138.      return null;
  139.    }
  140.  
  141.    public int getViewTypeCount() {
  142.      int total = 1;
  143.  
  144.      for (Adapter adapter : this.sections.values())
  145.        total += adapter.getViewTypeCount();
  146.  
  147.      return total;
  148.    }
  149.  
  150.    public boolean isEnabled(int position) {
  151.      return (getItemViewType(position) != TYPE_SECTION_HEADER);
  152.    }
  153.  
  154.    public void setShowHeaders(boolean showHeaders) {
  155.      this.showHeaders = showHeaders;
  156.    }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement