ItemAdapter.java revision 960c0ea0b1d36904beef0f01715dd43a211e88ca
1/* 2 * Copyright (C) 2015 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.setupwizardlib.items; 18 19import android.util.SparseIntArray; 20import android.view.LayoutInflater; 21import android.view.View; 22import android.view.ViewGroup; 23import android.widget.BaseAdapter; 24 25/** 26 * An adapter typically used with ListView to display an 27 * {@link com.android.setupwizardlib.items.ItemHierarchy}. The item hierarchy used to create this 28 * adapter can be inflated by {@link ItemInflater} from XML. 29 */ 30public class ItemAdapter extends BaseAdapter implements ItemHierarchy.Observer { 31 32 private final ItemHierarchy mItemHierarchy; 33 private ViewTypes mViewTypes = new ViewTypes(); 34 35 public ItemAdapter(ItemHierarchy hierarchy) { 36 mItemHierarchy = hierarchy; 37 mItemHierarchy.registerObserver(this); 38 refreshViewTypes(); 39 } 40 41 @Override 42 public int getCount() { 43 return mItemHierarchy.getCount(); 44 } 45 46 @Override 47 public IItem getItem(int position) { 48 return mItemHierarchy.getItemAt(position); 49 } 50 51 @Override 52 public long getItemId(int position) { 53 return position; 54 } 55 56 @Override 57 public int getItemViewType(int position) { 58 IItem item = getItem(position); 59 int layoutRes = item.getLayoutResource(); 60 return mViewTypes.get(layoutRes); 61 } 62 63 @Override 64 public int getViewTypeCount() { 65 return mViewTypes.size(); 66 } 67 68 private void refreshViewTypes() { 69 for (int i = 0; i < getCount(); i++) { 70 IItem item = getItem(i); 71 mViewTypes.add(item.getLayoutResource()); 72 } 73 } 74 75 @Override 76 public View getView(int position, View convertView, ViewGroup parent) { 77 IItem item = getItem(position); 78 if (convertView == null) { 79 LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 80 convertView = inflater.inflate(item.getLayoutResource(), parent, false); 81 } 82 item.onBindView(convertView); 83 return convertView; 84 } 85 86 @Override 87 public void onChanged(ItemHierarchy hierarchy) { 88 refreshViewTypes(); 89 notifyDataSetChanged(); 90 } 91 92 @Override 93 public boolean isEnabled(int position) { 94 return getItem(position).isEnabled(); 95 } 96 97 public ItemHierarchy findItemById(int id) { 98 return mItemHierarchy.findItemById(id); 99 } 100 101 public ItemHierarchy getRootItemHierarchy() { 102 return mItemHierarchy; 103 } 104 105 /** 106 * A helper class to pack a sparse set of integers (e.g. resource IDs) to a contiguous list of 107 * integers (e.g. adapter positions), providing mapping to retrieve the original ID from a given 108 * position. This is used to pack the view types of the adapter into contiguous integers from 109 * a given layout resource. 110 */ 111 private static class ViewTypes { 112 private SparseIntArray mPositionMap = new SparseIntArray(); 113 private int nextPosition = 0; 114 115 public int add(int id) { 116 if (mPositionMap.indexOfKey(id) < 0) { 117 mPositionMap.put(id, nextPosition); 118 nextPosition++; 119 } 120 return mPositionMap.get(id); 121 } 122 123 public int size() { 124 return mPositionMap.size(); 125 } 126 127 public int get(int id) { 128 return mPositionMap.get(id); 129 } 130 } 131} 132