/** * get the adapter (null safe) * * @return the FastAdapter used with this drawer */ protected FastAdapter<IDrawerItem> getAdapter() { if (mAdapter == null) { mAdapter = FastAdapter.with(Arrays.asList(mHeaderAdapter, mItemAdapter, mFooterAdapter), Arrays.<IAdapterExtension<IDrawerItem>>asList(mExpandableExtension)); mAdapter.withSelectable(true); mAdapter.withMultiSelect(false); mAdapter.withAllowDeselection(false); mAdapter.setHasStableIds(mHasStableIds); } return mAdapter; }
/** * 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; }
public ActionModeHelper(FastAdapter<Item> fastAdapter, int cabMenu, ActionMode.Callback callback) { this.mFastAdapter = fastAdapter; this.mCabMenu = cabMenu; this.mCallback = callback; this.mInternalCallback = new ActionBarCallBack(); this.mSelectExtension = fastAdapter.getExtension(SelectExtension.class); if (mSelectExtension == null) { throw new IllegalStateException("The provided FastAdapter requires the `SelectExtension` or `withSelectable(true)`"); } }
/** * Define a custom Adapter which will be used in the drawer * NOTE: this is not recommender * WARNING: if you do this after adding items you will loose those! * * @param adapter the FastAdapter to use with this drawer * @return this */ public DrawerBuilder withAdapter(@NonNull FastAdapter<IDrawerItem> adapter) { this.mAdapter = adapter; //we have to rewrap as a different FastAdapter was provided adapter.addAdapter(0, mHeaderAdapter); adapter.addAdapter(1, mItemAdapter); adapter.addAdapter(2, mFooterAdapter); adapter.addExtension(mExpandableExtension); return this; }
/** * 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); } } }
mFastAdapter = FastAdapter.with(Arrays.asList(headerAdapter, itemAdapter)); mFastAdapter.setHasStableIds(true); mFastAdapter.withSelectable(true); mFastAdapter.withMultiSelect(true); mFastAdapter.withSelectOnLongClick(true); mFastAdapter.withSelectionListener(new ISelectionListener<SimpleItem>() { @Override public void onSelectionChanged(SimpleItem item, boolean selected) { mFastAdapter.withOnPreClickListener(new OnClickListener<SimpleItem>() { @Override public boolean onClick(View v, IAdapter<SimpleItem> adapter, @NonNull SimpleItem item, int position) { mFastAdapter.withOnClickListener(new OnClickListener<SimpleItem>() { @Override public boolean onClick(View v, IAdapter<SimpleItem> adapter, @NonNull SimpleItem item, int position) { mFastAdapter.withOnPreLongClickListener(new OnLongClickListener<SimpleItem>() { @Override public boolean onLongClick(View v, IAdapter<SimpleItem> adapter, SimpleItem item, int position) { mFastAdapter.withSavedInstanceState(savedInstanceState);
mAdapter.withMultiSelect(mMultiSelect); if (mMultiSelect) { mAdapter.withSelectOnLongClick(false); mAdapter.withAllowDeselection(true); mSelectedItemPosition = 1; mAdapter.deselect(); mAdapter.select(mSelectedItemPosition); mAdapter.withOnClickListener(new OnClickListener<IDrawerItem>() { @Override public boolean onClick(final View view, IAdapter<IDrawerItem> adapter, final IDrawerItem item, final int position) { mAdapter.withOnLongClickListener(new OnLongClickListener<IDrawerItem>() { @Override public boolean onLongClick(View view, IAdapter<IDrawerItem> adapter, final IDrawerItem item, final int position) { mAdapter.deselect(); mAdapter.withSavedInstanceState(mSavedInstance, Drawer.BUNDLE_SELECTION); DrawerUtils.setStickyFooterSelection(this, mSavedInstance.getInt(Drawer.BUNDLE_STICKY_FOOTER_SELECTION, -1), null); } else { mAdapter.deselect(); mAdapter.withSavedInstanceState(mSavedInstance, Drawer.BUNDLE_SELECTION_APPENDED); DrawerUtils.setStickyFooterSelection(this, mSavedInstance.getInt(Drawer.BUNDLE_STICKY_FOOTER_SELECTION_APPENDED, -1), null); int selection = mAdapter.getSelections().size() == 0 ? -1 : mAdapter.getSelections().iterator().next(); mOnDrawerItemClickListener.onItemClick(null, selection, getDrawerItem(selection));
mFastAdapter = FastAdapter.with(Arrays.asList(mHeaderAdapter, mItemAdapter), Arrays.<IAdapterExtension<IItem>>asList(mExpandableExtension)); mFastAdapter.withSelectable(true); mFastAdapter.withMultiSelect(true); mFastAdapter.withSelectOnLongClick(true); mFastAdapter.withOnPreClickListener(new OnClickListener<IItem>() { @Override public boolean onClick(View v, IAdapter adapter, @NonNull IItem item, int position) { mFastAdapter.withOnPreLongClickListener(new OnLongClickListener<IItem>() { @Override public boolean onLongClick(View v, IAdapter adapter, IItem item, int position) { mFastAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { @Override public void onChanged() { mFastAdapter.withSavedInstanceState(savedInstanceState);
fastAdapter = FastAdapter.with(itemAdapter); fastAdapter.withSelectable(true); fastAdapter.withOnClickListener(new OnClickListener<SimpleItem>() { @Override public boolean onClick(View v, IAdapter<SimpleItem> adapter, fastAdapter.withSavedInstanceState(savedInstanceState);
mFastAdapter = FastAdapter.with(Arrays.asList(mItemAdapter)); mFastAdapter.withSelectable(true); mFastAdapter.withMultiSelect(true); mFastAdapter.withSelectOnLongClick(false); selectExtension = mFastAdapter.getExtension(SelectExtension.class);
/** * 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); }
fastAdapter = FastAdapter.with(Arrays.asList(itemAdapter)); fastAdapter.withSelectable(true); fastAdapter.withSavedInstanceState(savedInstanceState);
identifier = it.next(); pos = fastAdapter.getPosition(identifier); item = fastAdapter.getItem(pos); parentPos = fastAdapter.getPosition(parent); boolean success = ((IExpandable) parent).getSubItems().remove(item); fastAdapter.notifyAdapterItemChanged(parentPos); IAdapter adapter = fastAdapter.getAdapter(pos); boolean success = false; if (adapter instanceof IItemAdapter) { success = ((IItemAdapter) adapter).remove(pos) != null; if (success) { fastAdapter.notifyAdapterItemRemoved(pos);
/** * @param position the global position of the current item * @return a set with the global positions of all expanded items on the root level */ public int[] getExpandedItemsRootLevel(int position) { int[] expandedItems; ArraySet<Integer> expandedItemsList = new ArraySet<>(); Item item = mFastAdapter.getItem(position); for (int i = 0, size = mFastAdapter.getItemCount(); i < size; i++) { Item currItem = mFastAdapter.getItem(i); if (currItem instanceof ISubItem) { IItem parent = ((ISubItem) currItem).getParent(); if (parent instanceof IExpandable && ((IExpandable) parent).isExpanded()) { i += ((IExpandable) parent).getSubItems().size(); if (parent != item) expandedItemsList.add(mFastAdapter.getPosition((Item) parent)); } } } int expandedItemsListLength = expandedItemsList.size(); expandedItems = new int[expandedItemsListLength]; for (int i = 0; i < expandedItemsListLength; i++) { expandedItems[i] = expandedItemsList.valueAt(i); } return expandedItems; }
fastAdapter = FastAdapter.with(Arrays.asList(headerAdapter, itemAdapter)); fastAdapter.withSelectable(true); fastAdapter.setHasStableIds(true); fastAdapter.withSavedInstanceState(savedInstanceState);
/** * creates a new FastAdapter with the provided adapters * if adapters is null, a default ItemAdapter is defined * * @param adapters the adapters which this FastAdapter should use * @return a new FastAdapter */ public static <Item extends IItem, A extends IAdapter> FastAdapter<Item> with(@Nullable Collection<A> adapters) { return with(adapters, null); }
@Override public void onClick(View v, int position, FastAdapter<RadioButtonSampleItem> fastAdapter, RadioButtonSampleItem item) { if (!item.isSelected()) { Set<Integer> selections = fastAdapter.getSelections(); if (!selections.isEmpty()) { int selectedPosition = selections.iterator().next(); fastAdapter.deselect(); fastAdapter.notifyItemChanged(selectedPosition); } fastAdapter.select(position); } } }
@Test public void select() throws Exception { adapter.withSelectable(true); itemAdapter.set(TestDataGenerator.genTestItemList(100)); assertThat(adapter.getSelectedItems().size()).isEqualTo(0); assertThat(adapter.getSelections().size()).isEqualTo(0); adapter.select(10); assertThat(adapter.getSelectedItems().size()).isEqualTo(1); assertThat(adapter.getSelectedItems().iterator().next().getIdentifier()).isEqualTo(10); assertThat(adapter.getSelections().size()).isEqualTo(1); assertThat(adapter.getSelections().iterator().next()).isEqualTo(10); }
.withSelectable(true) .withMultiSelect(true) .withSelectOnLongClick(true) .withOnPreClickListener(new OnClickListener<IItem>() { @Override public boolean onClick(View v, IAdapter<IItem> adapter, @NonNull IItem item, int position) { .withOnClickListener(new OnClickListener<IItem>() { @Override public boolean onClick(View v, IAdapter<IItem> adapter, @NonNull IItem item, int position) { .withOnPreLongClickListener(new OnLongClickListener<IItem>() { @Override public boolean onLongClick(View v, IAdapter<IItem> adapter, IItem item, int position) {
/** * opens the expandable item at the given position * * @param position the global position * @param notifyItemChanged true if we need to call notifyItemChanged. DEFAULT: false */ public void expand(int position, boolean notifyItemChanged) { Item item = mFastAdapter.getItem(position); if (item != null && item instanceof IExpandable) { IExpandable expandable = (IExpandable) item; //if this item is not already expanded and has sub items we go on if (!expandable.isExpanded() && expandable.getSubItems() != null && expandable.getSubItems().size() > 0) { IAdapter<Item> adapter = mFastAdapter.getAdapter(position); if (adapter != null && adapter instanceof IItemAdapter) { ((IItemAdapter<?, Item>) adapter).addInternal(position + 1, expandable.getSubItems()); } //remember that this item is now opened (not collapsed) expandable.withIsExpanded(true); //we need to notify to get the correct drawable if there is one showing the current state if (notifyItemChanged) { mFastAdapter.notifyItemChanged(position); } } } }