/** * @param flatPos the flattened position of an item in the list * @return true if {@code group} is expanded, false if it is collapsed */ public boolean isGroupExpanded(int flatPos) { ExpandableListPosition listPosition = expandableList.getUnflattenedPosition(flatPos); return expandableList.expandedGroupIndexes[listPosition.groupPos]; }
/** * Gets the view type of the item at the given position. * * @param position The flat position in the list to get the view type of * @return {@value ExpandableListPosition#CHILD} or {@value ExpandableListPosition#GROUP} * @throws RuntimeException if the item at the given position in the list is not found */ @Override public int getItemViewType(int position) { return expandableList.getUnflattenedPosition(position).type; }
/** * @param flatPos The flat list position of the group * @return false if the group is expanded, *after* the toggle, true if the group is now collapsed */ public boolean toggleGroup(int flatPos) { ExpandableListPosition listPos = expandableList.getUnflattenedPosition(flatPos); boolean expanded = expandableList.expandedGroupIndexes[listPos.groupPos]; if (expanded) { collapseGroup(listPos); } else { expandGroup(listPos); } return expanded; }
@Override public void onBindChildViewHolder(CCVH holder, int flatPosition, ExpandableGroup group, int childIndex) { ExpandableListPosition listPosition = expandableList.getUnflattenedPosition(flatPosition); holder.onBindViewHolder(flatPosition, childCheckController.isChildChecked(listPosition)); onBindCheckChildViewHolder(holder, flatPosition, (CheckedExpandableGroup) group, childIndex); }
@Override public void onChildCheckChanged(View view, boolean checked, int flatPos) { ExpandableListPosition listPos = expandableList.getUnflattenedPosition(flatPos); childCheckController.onChildCheckChanged(checked, listPos); if (childClickListener != null) { childClickListener.onCheckChildCLick(view, checked, (CheckedExpandableGroup) expandableList.getExpandableGroup(listPos), listPos.childPos); } }
@Override public void onChildCheckChanged(View view, boolean checked, int flatPos) { ExpandableListPosition listPos = expandableList.getUnflattenedPosition(flatPos); childCheckController.onChildCheckChanged(checked, listPos); if (childClickListener != null) { childClickListener.onCheckChildCLick(view, checked, (CheckedExpandableGroup) expandableList.getExpandableGroup(listPos), listPos.childPos); } }
public boolean toggleGroup(ExpandableGroup group) { ExpandableListPosition listPos = expandableList.getUnflattenedPosition(expandableList.getFlattenedGroupIndex(group)); boolean expanded = expandableList.expandedGroupIndexes[listPos.groupPos]; if (expanded) { collapseGroup(listPos); } else { expandGroup(listPos); } return expanded; }
/** * Gets the view type of the item at the given position. * * @param position The flat position in the list to get the view type of * @return if the flat position corresponds to a child item, this will return the value returned * by {@code getChildViewType}. if the flat position refers to a group item this will return the * value returned by {@code getGroupViewType} */ @Override public int getItemViewType(int position) { ExpandableListPosition listPosition = expandableList.getUnflattenedPosition(position); ExpandableGroup group = expandableList.getExpandableGroup(listPosition); int viewType = listPosition.type; switch (viewType) { case ExpandableListPosition.GROUP: return getGroupViewType(position, group); case ExpandableListPosition.CHILD: return getChildViewType(position, group, listPosition.childPos); default: return viewType; } }
/** * Called when a group is expanded * * @param positionStart the flat position of the first child in the {@link ExpandableGroup} * @param itemCount the total number of children in the {@link ExpandableGroup} */ @Override public void onGroupExpanded(int positionStart, int itemCount) { //update header int headerPosition = positionStart - 1; notifyItemChanged(headerPosition); // only insert if there items to insert if (itemCount > 0) { notifyItemRangeInserted(positionStart, itemCount); if (expandCollapseListener != null) { int groupIndex = expandableList.getUnflattenedPosition(positionStart).groupPos; expandCollapseListener.onGroupExpanded(getGroups().get(groupIndex)); } } }
/** * Called when a group is collapsed * * @param positionStart the flat position of the first child in the {@link ExpandableGroup} * @param itemCount the total number of children in the {@link ExpandableGroup} */ @Override public void onGroupCollapsed(int positionStart, int itemCount) { //update header int headerPosition = positionStart - 1; notifyItemChanged(headerPosition); // only remote if there items to remove if (itemCount > 0) { notifyItemRangeRemoved(positionStart, itemCount); if (expandCollapseListener != null) { //minus one to return the position of the header, not first child int groupIndex = expandableList.getUnflattenedPosition(positionStart - 1).groupPos; expandCollapseListener.onGroupCollapsed(getGroups().get(groupIndex)); } } }
@Test public void test_getUnflattenedPosition() { ExpandableList list = new ExpandableList(groups); int flatPos = 3; //initial state //flatPos 3 == group at index 3 ExpandableListPosition initialExpected = obtain(GROUP, 3, -1, 3); ExpandableListPosition initialActual = list.getUnflattenedPosition(flatPos); assertEquals(initialExpected, initialActual); //expand first group list.expandedGroupIndexes[0] = true; //flatPos 3 == child number 2 within group at index 0 ExpandableListPosition newExpected = obtain(CHILD, 0, 2, 3); ExpandableListPosition newActual = list.getUnflattenedPosition(flatPos); assertEquals(newExpected, newActual); } }
@Override public void onBindChildViewHolder(ChildViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) { int viewType = getItemViewType(flatPosition); Artist artist = (Artist) group.getItems().get(childIndex); switch (viewType) { case ARTIST_VIEW_TYPE: ((ArtistViewHolder) holder).setArtistName(artist.getName()); break; case FAVORITE_VIEW_TYPE: ExpandableListPosition listPosition = expandableList.getUnflattenedPosition(flatPosition); ((SingleCheckArtistViewHolder) holder) .onBindViewHolder(flatPosition, childCheckController.isChildChecked(listPosition)); ((SingleCheckArtistViewHolder) holder).setArtistName(artist.getName()); } }
/** * Implementation of Adapter.onBindViewHolder(RecyclerView.ViewHolder, int) * that determines if the list item is a group or a child and calls through * to the appropriate implementation of either {@link #onBindGroupViewHolder(GroupViewHolder, * int, * ExpandableGroup)} * or {@link #onBindChildViewHolder(ChildViewHolder, int, ExpandableGroup, int)}. * * @param holder Either the GroupViewHolder or the ChildViewHolder to bind data to * @param position The flat position (or index in the list of {@link * ExpandableList#getVisibleItemCount()} in the list at which to bind */ @Override public void onBindViewHolder(ViewHolder holder, int position) { ExpandableListPosition listPos = expandableList.getUnflattenedPosition(position); ExpandableGroup group = expandableList.getExpandableGroup(listPos); switch (listPos.type) { case ExpandableListPosition.GROUP: onBindGroupViewHolder((GVH) holder, position, group); if (isGroupExpanded(group)) { ((GVH) holder).expand(); } else { ((GVH) holder).collapse(); } break; case ExpandableListPosition.CHILD: onBindChildViewHolder((CVH) holder, position, group, listPos.childPos); break; } }
/** * Implementation of Adapter.onBindViewHolder(RecyclerView.ViewHolder, int) * that determines if the list item is a group or a child and calls through * to the appropriate implementation of either {@link #onBindGroupViewHolder(GroupViewHolder, * int, * ExpandableGroup)} * or {@link #onBindChildViewHolder(ChildViewHolder, int, ExpandableGroup, int)}. * * @param holder Either the GroupViewHolder or the ChildViewHolder to bind data to * @param position The flat position (or index in the list of {@link * ExpandableList#getVisibleItemCount()} in the list at which to bind */ @Override public void onBindViewHolder(ViewHolder holder, int position) { ExpandableListPosition listPos = expandableList.getUnflattenedPosition(position); ExpandableGroup group = expandableList.getExpandableGroup(listPos); if (isGroup(getItemViewType(position))) { onBindGroupViewHolder((GVH) holder, position, group); if (isGroupExpanded(group)) { ((GVH) holder).expand(); } else { ((GVH) holder).collapse(); } } else if (isChild(getItemViewType(position))) { onBindChildViewHolder((CVH) holder, position, group, listPos.childPos); } }