Home | History | Annotate | Download | only in ui
      1 /*******************************************************************************
      2  *      Copyright (C) 2012 Google Inc.
      3  *      Licensed to The Android Open Source Project.
      4  *
      5  *      Licensed under the Apache License, Version 2.0 (the "License");
      6  *      you may not use this file except in compliance with the License.
      7  *      You may obtain a copy of the License at
      8  *
      9  *           http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *      Unless required by applicable law or agreed to in writing, software
     12  *      distributed under the License is distributed on an "AS IS" BASIS,
     13  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *      See the License for the specific language governing permissions and
     15  *      limitations under the License.
     16  *******************************************************************************/
     17 
     18 package com.android.mail.ui;
     19 
     20 import android.view.View;
     21 import android.view.ViewGroup;
     22 import android.widget.Adapter;
     23 import android.widget.BaseAdapter;
     24 
     25 import java.util.ArrayList;
     26 
     27 public class SeparatedFolderListAdapter extends BaseAdapter {
     28 
     29     private final ArrayList<FolderSelectorAdapter> mSections =
     30             new ArrayList<FolderSelectorAdapter>();
     31     public final static int TYPE_ITEM = 0;
     32 
     33     public void addSection(FolderSelectorAdapter adapter) {
     34         mSections.add(adapter);
     35     }
     36 
     37     public void clearSections() {
     38         mSections.clear();
     39     }
     40 
     41     @Override
     42     public Object getItem(int position) {
     43         for (FolderSelectorAdapter adapter : mSections) {
     44             int size = adapter.getCount();
     45 
     46             // check if position inside this section
     47             if (position < size)
     48                 return adapter.getItem(position);
     49 
     50             // otherwise jump into next section
     51             position -= size;
     52         }
     53         return null;
     54     }
     55 
     56     @Override
     57     public int getCount() {
     58         // total together all sections, plus one for each section header
     59         int total = 0;
     60         for (FolderSelectorAdapter adapter : mSections) {
     61             total += adapter.getCount();
     62         }
     63         return total;
     64     }
     65 
     66     @Override
     67     public int getViewTypeCount() {
     68         // assume that headers count as one, then total all sections
     69         int total = 0;
     70         for (Adapter adapter : mSections)
     71             total += adapter.getViewTypeCount();
     72         return total == 0 ? 1 : total;
     73     }
     74 
     75     @Override
     76     public int getItemViewType(int position) {
     77         int type = 0;
     78         for (FolderSelectorAdapter adapter : mSections) {
     79             int size = adapter.getCount();
     80             // check if position inside this section
     81             if (position < size) {
     82                 return type + adapter.getItemViewType(position);
     83             }
     84 
     85             // otherwise jump into next section
     86             position -= size;
     87             type += adapter.getViewTypeCount();
     88         }
     89         return -1;
     90     }
     91 
     92 
     93     @Override
     94     public View getView(int position, View convertView, ViewGroup parent) {
     95         for (FolderSelectorAdapter adapter : mSections) {
     96             int size = adapter.getCount();
     97             if (position < size) {
     98                 return adapter.getView(position, convertView, parent);
     99             }
    100             // otherwise jump into next section
    101             position -= size;
    102         }
    103         return null;
    104     }
    105 
    106     @Override
    107     public long getItemId(int position) {
    108         return position;
    109     }
    110 
    111 }
    112