diff options
Diffstat (limited to 'library/main/src/com/android/setupwizardlib/items/ItemAdapter.java')
-rw-r--r-- | library/main/src/com/android/setupwizardlib/items/ItemAdapter.java | 246 |
1 files changed, 123 insertions, 123 deletions
diff --git a/library/main/src/com/android/setupwizardlib/items/ItemAdapter.java b/library/main/src/com/android/setupwizardlib/items/ItemAdapter.java index 53285e7..851736c 100644 --- a/library/main/src/com/android/setupwizardlib/items/ItemAdapter.java +++ b/library/main/src/com/android/setupwizardlib/items/ItemAdapter.java @@ -23,130 +23,130 @@ import android.view.ViewGroup; import android.widget.BaseAdapter; /** - * An adapter typically used with ListView to display an - * {@link com.android.setupwizardlib.items.ItemHierarchy}. The item hierarchy used to create this - * adapter can be inflated by {@link ItemInflater} from XML. + * An adapter typically used with ListView to display an {@link + * com.android.setupwizardlib.items.ItemHierarchy}. The item hierarchy used to create this adapter + * can be inflated by {@link ItemInflater} from XML. */ public class ItemAdapter extends BaseAdapter implements ItemHierarchy.Observer { - private final ItemHierarchy mItemHierarchy; - private ViewTypes mViewTypes = new ViewTypes(); - - public ItemAdapter(ItemHierarchy hierarchy) { - mItemHierarchy = hierarchy; - mItemHierarchy.registerObserver(this); - refreshViewTypes(); - } - - @Override - public int getCount() { - return mItemHierarchy.getCount(); - } - - @Override - public IItem getItem(int position) { - return mItemHierarchy.getItemAt(position); - } - - @Override - public long getItemId(int position) { - return position; - } - - @Override - public int getItemViewType(int position) { - IItem item = getItem(position); - int layoutRes = item.getLayoutResource(); - return mViewTypes.get(layoutRes); - } - - @Override - public int getViewTypeCount() { - return mViewTypes.size(); - } - - private void refreshViewTypes() { - for (int i = 0; i < getCount(); i++) { - IItem item = getItem(i); - mViewTypes.add(item.getLayoutResource()); - } - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - IItem item = getItem(position); - if (convertView == null) { - LayoutInflater inflater = LayoutInflater.from(parent.getContext()); - convertView = inflater.inflate(item.getLayoutResource(), parent, false); - } - item.onBindView(convertView); - return convertView; - } - - @Override - public void onChanged(ItemHierarchy hierarchy) { - refreshViewTypes(); - notifyDataSetChanged(); - } - - @Override - public void onItemRangeChanged(ItemHierarchy itemHierarchy, int positionStart, int itemCount) { - onChanged(itemHierarchy); - } - - @Override - public void onItemRangeInserted(ItemHierarchy itemHierarchy, int positionStart, int itemCount) { - onChanged(itemHierarchy); - } - - @Override - public void onItemRangeMoved(ItemHierarchy itemHierarchy, int fromPosition, int toPosition, - int itemCount) { - onChanged(itemHierarchy); - } - - @Override - public void onItemRangeRemoved(ItemHierarchy itemHierarchy, int positionStart, int itemCount) { - onChanged(itemHierarchy); - } - - @Override - public boolean isEnabled(int position) { - return getItem(position).isEnabled(); - } - - public ItemHierarchy findItemById(int id) { - return mItemHierarchy.findItemById(id); - } - - public ItemHierarchy getRootItemHierarchy() { - return mItemHierarchy; - } - - /** - * A helper class to pack a sparse set of integers (e.g. resource IDs) to a contiguous list of - * integers (e.g. adapter positions), providing mapping to retrieve the original ID from a given - * position. This is used to pack the view types of the adapter into contiguous integers from - * a given layout resource. - */ - private static class ViewTypes { - private SparseIntArray mPositionMap = new SparseIntArray(); - private int nextPosition = 0; - - public int add(int id) { - if (mPositionMap.indexOfKey(id) < 0) { - mPositionMap.put(id, nextPosition); - nextPosition++; - } - return mPositionMap.get(id); - } - - public int size() { - return mPositionMap.size(); - } - - public int get(int id) { - return mPositionMap.get(id); - } - } + private final ItemHierarchy itemHierarchy; + private final ViewTypes viewTypes = new ViewTypes(); + + public ItemAdapter(ItemHierarchy hierarchy) { + itemHierarchy = hierarchy; + itemHierarchy.registerObserver(this); + refreshViewTypes(); + } + + @Override + public int getCount() { + return itemHierarchy.getCount(); + } + + @Override + public IItem getItem(int position) { + return itemHierarchy.getItemAt(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public int getItemViewType(int position) { + IItem item = getItem(position); + int layoutRes = item.getLayoutResource(); + return viewTypes.get(layoutRes); + } + + @Override + public int getViewTypeCount() { + return viewTypes.size(); + } + + private void refreshViewTypes() { + for (int i = 0; i < getCount(); i++) { + IItem item = getItem(i); + viewTypes.add(item.getLayoutResource()); + } + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + IItem item = getItem(position); + if (convertView == null) { + LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + convertView = inflater.inflate(item.getLayoutResource(), parent, false); + } + item.onBindView(convertView); + return convertView; + } + + @Override + public void onChanged(ItemHierarchy hierarchy) { + refreshViewTypes(); + notifyDataSetChanged(); + } + + @Override + public void onItemRangeChanged(ItemHierarchy itemHierarchy, int positionStart, int itemCount) { + onChanged(itemHierarchy); + } + + @Override + public void onItemRangeInserted(ItemHierarchy itemHierarchy, int positionStart, int itemCount) { + onChanged(itemHierarchy); + } + + @Override + public void onItemRangeMoved( + ItemHierarchy itemHierarchy, int fromPosition, int toPosition, int itemCount) { + onChanged(itemHierarchy); + } + + @Override + public void onItemRangeRemoved(ItemHierarchy itemHierarchy, int positionStart, int itemCount) { + onChanged(itemHierarchy); + } + + @Override + public boolean isEnabled(int position) { + return getItem(position).isEnabled(); + } + + public ItemHierarchy findItemById(int id) { + return itemHierarchy.findItemById(id); + } + + public ItemHierarchy getRootItemHierarchy() { + return itemHierarchy; + } + + /** + * A helper class to pack a sparse set of integers (e.g. resource IDs) to a contiguous list of + * integers (e.g. adapter positions), providing mapping to retrieve the original ID from a given + * position. This is used to pack the view types of the adapter into contiguous integers from a + * given layout resource. + */ + private static class ViewTypes { + private final SparseIntArray positionMap = new SparseIntArray(); + private int nextPosition = 0; + + public int add(int id) { + if (positionMap.indexOfKey(id) < 0) { + positionMap.put(id, nextPosition); + nextPosition++; + } + return positionMap.get(id); + } + + public int size() { + return positionMap.size(); + } + + public int get(int id) { + return positionMap.get(id); + } + } } |