FakeAdapter.java revision ab775ecdd189b32e35b0d3f4a821502f88b03a4b
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
17package com.android.layoutlib.bridge.impl.binding;
18
19import com.android.ide.common.rendering.api.AdapterBinding;
20import com.android.ide.common.rendering.api.DataBindingItem;
21import com.android.ide.common.rendering.api.IProjectCallback;
22import com.android.ide.common.rendering.api.ResourceReference;
23
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.AdapterView;
27import android.widget.ListAdapter;
28import android.widget.SpinnerAdapter;
29
30import java.util.ArrayList;
31import 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 */
38public 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    @Override
78    public boolean isEnabled(int position) {
79        return true;
80    }
81
82    @Override
83    public int getCount() {
84        return mItems.size();
85    }
86
87    @Override
88    public Object getItem(int position) {
89        return mItems.get(position);
90    }
91
92    @Override
93    public long getItemId(int position) {
94        return position;
95    }
96
97    @Override
98    public int getItemViewType(int position) {
99        return mItems.get(position).getType();
100    }
101
102    @Override
103    public View getView(int position, View convertView, ViewGroup parent) {
104        // we don't care about recycling here because we never scroll.
105        AdapterItem item = mItems.get(position);
106        return getView(item, null /*parentGroup*/, convertView, parent);
107    }
108
109    @Override
110    public int getViewTypeCount() {
111        return mTypes.size();
112    }
113
114    // ---- SpinnerAdapter
115
116    @Override
117    public View getDropDownView(int position, View convertView, ViewGroup parent) {
118        // pass
119        return null;
120    }
121}
122