private static <T extends IExpandable & IItem> T getParent(IItem item) { if (item instanceof ISubItem) { return (T) ((ISubItem) item).getParent(); } return null; }
@Override public AbstractExpandableItem<Parent, VH, SubItem> withSubItems(List<SubItem> subItems) { this.mSubItems = subItems; for (SubItem subItem : subItems) { subItem.withParent(this); } return this; }
@Override public boolean apply(@NonNull IAdapter<Item> lastParentAdapter, int lastParentPosition, Item item, int position) { if (item.isSelected()) { //if it's a subitem remove it from the parent if (item instanceof ISubItem) { //a sub item which is not in the list can be instantly deleted IExpandable parent = (IExpandable) ((ISubItem) item).getParent(); //parent should not be null, but check in any case.. if (parent != null) { parent.getSubItems().remove(item); } } if (position != -1) { //a normal displayed item can only be deleted afterwards positions.add(position); } } return false; } }, false);
@Override public ModelAbstractExpandableItem<Model, Parent, VH, SubItem> withSubItems(List<SubItem> subItems) { this.mSubItems = subItems; for (SubItem subItem : subItems) { subItem.withParent(this); } return this; }
@Override public boolean apply(@NonNull IAdapter<Item> lastParentAdapter, int lastParentPosition, Item item, int position) { if (identifier == item.getIdentifier()) { //if it's a subitem remove it from the parent if (item instanceof ISubItem) { //a sub item which is not in the list can be instantly deleted IExpandable parent = (IExpandable) ((ISubItem) item).getParent(); //parent should not be null, but check in any case.. if (parent != null) { parent.getSubItems().remove(item); } } if (position != -1) { //a normal displayed item can only be deleted afterwards remove(position); } } return false; } }, false);
IItem parent = ((ISubItem) item).getParent(); if (!(parent instanceof IExpandable)) {
@Override public boolean apply(@NonNull IAdapter<Item> lastParentAdapter, int lastParentPosition, @NonNull Item item, int position) { //we do not care about non visible items if (position == -1) { return false; } //this is the entrance parent if (allowedParents.size() > 0 && item instanceof ISubItem) { // Go on until we hit an item with a parent which was not in our expandable hierarchy IItem parent = ((ISubItem) item).getParent(); if (parent == null || !allowedParents.contains(parent)) { return true; } } if (item instanceof IExpandable) { IExpandable expandable = (IExpandable) item; if (expandable.isExpanded()) { expandable.withIsExpanded(false); if (expandable.getSubItems() != null) { expandedItemsCount[0] += expandable.getSubItems().size(); allowedParents.add(item); } } } return false; } }, position, true);
/** * @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; }