question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retain ExpandableGroups' expanded status while calling GroupAdapter.update()

See original GitHub issue

Hi,

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:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
MrLogic85commented, Sep 25, 2020

Store a reference to the ExpandableGroup and update the content instead of recreating the group.

0reactions
ozonePoweredcommented, Apr 9, 2020

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 my ExpandableGroup

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expandable RecyclerViews with the groupie library - Medium
The expandableGroup.onToggleExpanded() call simply toggles the the parent item state. ChildItem. Next we create a class for the child item views ...
Read more >
Android's ExpandableListView: Keep groups expanded ...
In my activity I can delete entries from that list using the context menu. When the user does it, I want to refresh...
Read more >
lisawray / groupie Download - JitPack
Groupie lets you treat your content as logical groups and handles change notifications for you -- think sections with headers and footers, expandable...
Read more >
Hermes - Springer Link
This book is a hands-on Xamarin.Forms primer and a cross-platform reference for building native Android,. iOS, and Windows Phone apps using C# and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found