Home | History | Annotate | Download | only in binding
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.layoutlib.bridge.impl.binding;
     18 
     19 import com.android.ide.common.rendering.api.AdapterBinding;
     20 import com.android.ide.common.rendering.api.DataBindingItem;
     21 import com.android.ide.common.rendering.api.IProjectCallback;
     22 import com.android.ide.common.rendering.api.ResourceReference;
     23 
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.AdapterView;
     27 import android.widget.ListAdapter;
     28 import android.widget.SpinnerAdapter;
     29 
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 
     33 /**
     34  * Fake adapter to do fake data binding in {@link AdapterView} objects for {@link ListAdapter}
     35  * and {@link SpinnerAdapter}.
     36  *
     37  */
     38 public class FakeAdapter extends BaseAdapter implements ListAdapter, SpinnerAdapter {
     39 
     40     // don't use a set because the order is important.
     41     private final List<ResourceReference> mTypes = new ArrayList<ResourceReference>();
     42 
     43     public FakeAdapter(ResourceReference adapterRef, AdapterBinding binding,
     44             IProjectCallback callback) {
     45         super(adapterRef, binding, callback);
     46 
     47         final int repeatCount = getBinding().getRepeatCount();
     48         final int itemCount = getBinding().getItemCount();
     49 
     50         // Need an array to count for each type.
     51         // This is likely too big, but is the max it can be.
     52         int[] typeCount = new int[itemCount];
     53 
     54         // We put several repeating sets.
     55         for (int r = 0 ; r < repeatCount ; r++) {
     56             // loop on the type of list items, and add however many for each type.
     57             for (DataBindingItem dataBindingItem : getBinding()) {
     58                 ResourceReference viewRef = dataBindingItem.getViewReference();
     59                 int typeIndex = mTypes.indexOf(viewRef);
     60                 if (typeIndex == -1) {
     61                     typeIndex = mTypes.size();
     62                     mTypes.add(viewRef);
     63                 }
     64 
     65                 int count = dataBindingItem.getCount();
     66 
     67                 int index = typeCount[typeIndex];
     68                 typeCount[typeIndex] += count;
     69 
     70                 for (int k = 0 ; k < count ; k++) {
     71                     mItems.add(new AdapterItem(dataBindingItem, typeIndex, mItems.size(), index++));
     72                 }
     73             }
     74         }
     75     }
     76 
     77     public boolean isEnabled(int position) {
     78         return true;
     79     }
     80 
     81     public int getCount() {
     82         return mItems.size();
     83     }
     84 
     85     public Object getItem(int position) {
     86         return mItems.get(position);
     87     }
     88 
     89     public long getItemId(int position) {
     90         return position;
     91     }
     92 
     93     public int getItemViewType(int position) {
     94         return mItems.get(position).getType();
     95     }
     96 
     97     public View getView(int position, View convertView, ViewGroup parent) {
     98         // we don't care about recycling here because we never scroll.
     99         AdapterItem item = mItems.get(position);
    100         return getView(item, null /*parentGroup*/, convertView, parent);
    101     }
    102 
    103     public int getViewTypeCount() {
    104         return mTypes.size();
    105     }
    106 
    107     // ---- SpinnerAdapter
    108 
    109     public View getDropDownView(int position, View convertView, ViewGroup parent) {
    110         // pass
    111         return null;
    112     }
    113 }
    114