private int recursiveCollapse(@IntRange(from = 0) int position) { T item = getItem(position); if (!isExpandable(item)) { return 0; } IExpandable expandable = (IExpandable) item; int subItemCount = 0; if (expandable.isExpanded()) { List<T> subItems = expandable.getSubItems(); for (int i = subItems.size() - 1; i >= 0; i--) { T subItem = subItems.get(i); int pos = getItemPosition(subItem); if (pos < 0) { continue; } if (subItem instanceof IExpandable) { subItemCount += recursiveCollapse(pos); } mData.remove(pos); subItemCount++; } } return subItemCount; }
/** * Collapse an expandable item that has been expanded.. * * @param position the position of the item, which includes the header layout count. * @param animate collapse with animation or not. * @param notify notify the recyclerView refresh UI or not. * @return the number of subItems collapsed. */ public int collapse(@IntRange(from = 0) int position, boolean animate, boolean notify) { position -= getHeaderLayoutCount(); IExpandable expandable = getExpandableItem(position); if (expandable == null) { return 0; } int subItemCount = recursiveCollapse(position); expandable.setExpanded(false); int parentPos = position + getHeaderLayoutCount(); if (notify) { if (animate) { notifyItemChanged(parentPos); notifyItemRangeRemoved(parentPos + 1, subItemCount); } else { notifyDataSetChanged(); } } return subItemCount; }