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_SECTION_HEADER = 0;
     32     public final static int TYPE_ITEM = 1;
     33 
     34     public void addSection(FolderSelectorAdapter adapter) {
     35         mSections.add(adapter);
     36     }
     37 
     38     @Override
     39     public Object getItem(int position) {
     40         for (FolderSelectorAdapter adapter : mSections) {
     41             int size = adapter.getCount();
     42 
     43             // check if position inside this section
     44             if (position == 0 || position < size)
     45                 return adapter.getItem(position);
     46 
     47             // otherwise jump into next section
     48             position -= size;
     49         }
     50         return null;
     51     }
     52 
     53     @Override
     54     public int getCount() {
     55         // total together all sections, plus one for each section header
     56         int total = 0;
     57         for (FolderSelectorAdapter adapter : mSections) {
     58             total += adapter.getCount();
     59         }
     60         return total;
     61     }
     62 
     63     @Override
     64     public int getViewTypeCount() {
     65         // assume that headers count as one, then total all sections
     66         int total = 0;
     67         for (Adapter adapter : mSections)
     68             total += adapter.getViewTypeCount();
     69         return total;
     70     }
     71 
     72     @Override
     73     public int getItemViewType(int position) {
     74         int type = 0;
     75         for (FolderSelectorAdapter adapter : mSections) {
     76             int size = adapter.getCount();
     77             // check if position inside this section
     78             if (position == 0 || position < size) {
     79                 return type + adapter.getItemViewType(position);
     80             }
     81 
     82             // otherwise jump into next section
     83             position -= size;
     84             type += adapter.getViewTypeCount();
     85         }
     86         return -1;
     87     }
     88 
     89     @Override
     90     public boolean areAllItemsEnabled() {
     91         return false;
     92     }
     93 
     94     @Override
     95     public boolean isEnabled(int position) {
     96         return (getItemViewType(position) != TYPE_SECTION_HEADER);
     97     }
     98 
     99     @Override
    100     public View getView(int position, View convertView, ViewGroup parent) {
    101         for (FolderSelectorAdapter adapter : mSections) {
    102             int size = adapter.getCount();
    103             if (position == 0 || position < size) {
    104                 return adapter.getView(position, convertView, parent);
    105             }
    106             // otherwise jump into next section
    107             position -= size;
    108         }
    109         return null;
    110     }
    111 
    112     @Override
    113     public long getItemId(int position) {
    114         return position;
    115     }
    116 
    117 }
    118