/** * make sure we return the Item from the FastAdapter so we retrieve the item from all adapters * * @param position * @return */ public Item getItem(int position) { return mFastAdapter.getItem(position); }
/** * @return a set with the global positions of all selected items (which are currently in the list (includes expanded expandable items)) */ public Set<Integer> getSelections() { Set<Integer> selections = new ArraySet<>(); for (int i = 0, size = mFastAdapter.getItemCount(); i < size; i++) { if (mFastAdapter.getItem(i).isSelected()) { selections.add(i); } } return selections; }
/** * Gets all items (including sub items) from the FastAdapter * * @param fastAdapter the FastAdapter * @return a list of all items including the whole subItem hirachy */ public static <Item extends IItem> List<Item> getAllItems(FastAdapter<Item> fastAdapter) { int size = fastAdapter.getItemCount(); List<Item> items = new ArrayList<>(size); for (int i = 0; i < size; i++) { Item item = fastAdapter.getItem(i); items.add(item); addAllSubItems(item, items); } return items; }
/** * make sure we return the Item from the FastAdapter so we retrieve the item from all adapters * * @param position * @return */ public Item getItem(int position) { return mFastAdapter.getItem(position); }
@Override public void withSavedInstanceState(@Nullable Bundle savedInstanceState, String prefix) { if (savedInstanceState == null) { return; } ArrayList<String> expandedItems = savedInstanceState.getStringArrayList(BUNDLE_EXPANDED + prefix); String id; for (int i = 0, size = mFastAdapter.getItemCount(); i < size; i++) { Item item = mFastAdapter.getItem(i); id = String.valueOf(item.getIdentifier()); if (expandedItems != null && expandedItems.contains(id)) { expand(i); size = mFastAdapter.getItemCount(); } } }
/** * check if the item is within the bounds of the list * * @param position * @param includeOffset * @return */ protected boolean checkDrawerItem(int position, boolean includeOffset) { return getAdapter().getItem(position) != null; }
/** * calculates the position of an drawerItem. searching by it's identifier * * @param identifier * @return */ public static int getPositionByIdentifier(DrawerBuilder drawer, long identifier) { if (identifier != -1) { for (int i = 0; i < drawer.getAdapter().getItemCount(); i++) { if (drawer.getAdapter().getItem(i).getIdentifier() == identifier) { return i; } } } return -1; }
/** * get the drawerItem at a specific position * * @param position * @return */ protected IDrawerItem getDrawerItem(int position) { return (IDrawerItem) getAdapter().getItem(position); }
@Override public void saveInstanceState(Bundle savedInstanceState, String prefix) { if (savedInstanceState == null) { return; } ArrayList<String> expandedItems = new ArrayList<>(); Item item; for (int i = 0, size = mFastAdapter.getItemCount(); i < size; i++) { item = mFastAdapter.getItem(i); if (item instanceof IExpandable && ((IExpandable) item).isExpanded()) { expandedItems.add(String.valueOf(item.getIdentifier())); } } //remember the collapsed states savedInstanceState.putStringArrayList(BUNDLE_EXPANDED + prefix, expandedItems); }
/** * finds the int ItemId from the IItem which exists at the given position * * @param position the global position * @return the itemId for this position */ @Override public long getItemId(int position) { return getItem(position).getIdentifier(); }
/** * returns the expanded items this contains position and the count of items * which are expanded by this position * * @return the expanded items */ public SparseIntArray getExpanded() { SparseIntArray expandedItems = new SparseIntArray(); Item item; for (int i = 0, size = mFastAdapter.getItemCount(); i < size; i++) { item = mFastAdapter.getItem(i); if (item instanceof IExpandable && ((IExpandable) item).isExpanded()) { expandedItems.put(i, ((IExpandable) item).getSubItems().size()); } } return expandedItems; }
/** * finds the int ItemViewType from the IItem which exists at the given position * * @param position the global position * @return the viewType for this position */ @Override public int getItemViewType(int position) { return getItem(position).getType(); }
/** * set the selection of the MiniDrawer * * @param identifier the identifier of the item which should be selected (-1 for none) */ public void setSelection(long identifier) { if (identifier == -1) { mAdapter.deselect(); } int count = mAdapter.getItemCount(); for (int i = 0; i < count; i++) { IDrawerItem item = mAdapter.getItem(i); if (item.getIdentifier() == identifier && !item.isSelected()) { mAdapter.deselect(); mAdapter.select(i); } } }
/** * deselects an item and removes its position in the selections list * also takes an iterator to remove items from the map * * @param position the global position * @param entries the iterator which is used to deselect all */ public void deselect(int position, @Nullable Iterator<Integer> entries) { Item item = mFastAdapter.getItem(position); if (item == null) { return; } deselect(item, position, entries); }
@Override public void notifyAdapterItemRangeChanged(int position, int itemCount, Object payload) { for (int i = position; i < position + itemCount; i++) { Item item = mFastAdapter.getItem(position); if (item instanceof IExpandable && ((IExpandable) item).isExpanded()) { collapse(position); } } }
/** * toggles the selection of the item at the given position * * @param position the global position */ public void toggleSelection(int position) { if (mFastAdapter.getItem(position).isSelected()) { deselect(position); } else { select(position); } }
/** * toggles the expanded state of the given expandable item at the given position * * @param position the global position */ public void toggleExpandable(int position) { Item item = mFastAdapter.getItem(position); if (item instanceof IExpandable && ((IExpandable) item).isExpanded()) { collapse(position); } else { expand(position); } }
private void notifySelect(int position, boolean fireOnClick) { if (fireOnClick && position >= 0) { IDrawerItem item = mDrawerBuilder.mAdapter.getItem(position); if (item instanceof AbstractDrawerItem && ((AbstractDrawerItem) item).getOnDrawerItemClickListener() != null) { ((AbstractDrawerItem) item).getOnDrawerItemClickListener().onItemClick(null, position, item); } if (mDrawerBuilder.mOnDrawerItemClickListener != null) { mDrawerBuilder.mOnDrawerItemClickListener.onItemClick(null, position, item); } } //we set the selection on a normal item in the drawer so we have to deselect the items in the StickyDrawer mDrawerBuilder.resetStickyFooterSelection(); }
@Test public void getItem() throws Exception { List<TestItem> items = TestDataGenerator.genTestItemList(100); TestItem item = items.get(40); itemAdapter.set(items); assertThat(adapter.getItem(40)).isEqualTo(item); }
@Test public void getItem() throws Exception { List<TestItem> items = TestDataGenerator.genTestItemList(100); TestItem item = items.get(40); itemAdapter.set(items); assertThat(adapter.getItem(40)).isEqualTo(item); }