SHARE
TWEET

Untitled

a guest Jul 8th, 2013 31 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class NestedAdapter extends BaseAdapter {
  2.     private ArrayList<Pair<Adapter, String>> adapters;
  3.     private Context context;
  4.  
  5.     public NestedAdapter(Context context, Pair<Adapter, String>... adapters) {
  6.         this.context = context;
  7.         this.adapters = new ArrayList<Pair<Adapter, String>>();
  8.     }
  9.  
  10.     public void addAdapter(Adapter adapter, String title) {
  11.         adapters.add(new Pair<Adapter, String>(adapter, title));
  12.     }
  13.  
  14.     private int getAdapterSize(Pair<Adapter, String> pair) {
  15.         if(pair.second==null) {
  16.             return pair.first.getCount();
  17.         } else {
  18.             return pair.first.getCount()+1;
  19.         }
  20.     }
  21.  
  22.     @Override
  23.     public int getCount() {
  24.         int count = 0;
  25.         for(Pair<Adapter, String> pair: adapters) {
  26.             count += getAdapterSize(pair);
  27.         }
  28.         return count;
  29.     }
  30.  
  31.     @Override
  32.     public Object getItem(int position) {
  33.         Pair<Adapter, Object> pair = getItemWithAdapter(position);
  34.         if(pair.second instanceof String) {
  35.             return pair.second;
  36.         } else {
  37.             return pair.first.getItem((Integer) pair.second);
  38.         }
  39.     }
  40.  
  41.     private Pair<Adapter, Object> getItemWithAdapter(int position) {
  42.         int sum = 0;
  43.         int adapterId = 0;
  44.         while(sum<=position) {
  45.             sum += getAdapterSize(adapters.get(adapterId));
  46.             adapterId++;
  47.         }
  48.         adapterId--;
  49.         sum -= getAdapterSize(adapters.get(adapterId));
  50.  
  51.         int index = position - sum;
  52.         Pair<Adapter, String> pair = adapters.get(adapterId);
  53.         if(pair.second==null) {
  54.             return new Pair<Adapter, Object>(pair.first, index);
  55.         } if(index==0) {
  56.             return new Pair<Adapter, Object>(pair.first, pair.second);
  57.         } else {
  58.             return new Pair<Adapter, Object>(pair.first, index - 1);
  59.         }
  60.     }
  61.  
  62.     @Override
  63.     public long getItemId(int position) {
  64.         return position;
  65.     }
  66.  
  67.     @Override
  68.     public View getView(int position, View convertView, ViewGroup parent) {
  69.         Pair<Adapter, Object> itemPair = getItemWithAdapter(position);
  70.         Object second = itemPair.second;
  71.         if(second instanceof String) {
  72.             View view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
  73.             ((TextView)view.findViewById(android.R.id.text1)).setText((String) second);
  74.             return view;
  75.         } else {
  76.             int index = (Integer) second;
  77.             return itemPair.first.getView(index, convertView, parent);
  78.         }
  79.     }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top