Retain ExpandableGroups' expanded status while calling GroupAdapter.update()
See original GitHub issueHi,
I’m using ExpandableGroups, the list is dynamic, could refresh (including subitems) every few seconds, and my current approach is I’m recreating all the items at every update, letting DiffUtil figure out the changes (I also override the Items’ equals() to prevent unnecessary redraws of the items if the contents weren’t changed). While updating, I want to keep the expanded or collapsed state of the ExpandableGroups, so I’m wondering what’s the recommended way to do that. My current solution is to extend GroupAdapter and override the update() method where I search for the Items and if found, restore the expanded state manually. This runs on the UI thread (I guess it should to make sure I get the latest valid expanded states), and even though I don’t have a lot of items, it can take up to a few ms to run on my test devices. I wonder if there’s any better way to achieve this?
Any comments appreciated. See sample code below (it’s Java, obviously in Kotlin it would be more concise, but that’s not the point) Note, that I only have expandable items at root level - otherwise the method would need to be recursive.
import androidx.annotation.NonNull;
import com.xwray.groupie.ExpandableGroup;
import com.xwray.groupie.Group;
import com.xwray.groupie.GroupAdapter;
import com.xwray.groupie.Item;
import com.xwray.groupie.ViewHolder;
import java.util.Collection;
public class MyGroupAdapter<VH extends ViewHolder> extends GroupAdapter<VH> {
@Override
public void update(@NonNull Collection<? extends Group> newGroups) {
// restore expanded statuses
for (Group newGroup : newGroups) {
if (newGroup instanceof ExpandableGroup) {
ExpandableGroup newExpandableGroup = (ExpandableGroup) newGroup;
for (int i = 0; i < getGroupCount(); i++) {
Group oldGroup = getGroup(i);
if (oldGroup instanceof ExpandableGroup) {
ExpandableGroup oldExpandableGroup = (ExpandableGroup) oldGroup;
if (newExpandableGroup.getGroup(0) instanceof Item && oldExpandableGroup.getGroup(0) instanceof Item) {
Item newItem = (Item) newExpandableGroup.getGroup(0);
Item oldItem = (Item) oldExpandableGroup.getGroup(0);
if (newItem.getId() == oldItem.getId()) {
newExpandableGroup.setExpanded(oldExpandableGroup.isExpanded());
break;
}
}
}
}
}
}
super.update(newGroups);
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6
Top GitHub Comments
Store a reference to the ExpandableGroup and update the content instead of recreating the group.
Hello, any update on these issues ? I am currently retaining groups IDs in a
SparseArray
to restore the expanded state every time I reload data. But I am facing a wall to this click listener not working anymore after diffing myExpandableGroup
…