如何防止 notifyDataSetChanged 折叠 ExpandableListView 中的打开组?

How to prevent notifyDataSetChanged from collapsing open groups in ExpandableListView?

在我的应用程序中,我有一个动态的 ExpandableListView 以及一个刷新按钮。当用户点击刷新按钮时,最新的数据被下载,我调用 notifyDataSetChanged() 来更新 UI.

问题是 notifyDataSetChanged() 折叠了之前打开的所有组,这给用户带来了糟糕的体验。有什么办法可以避免这种情况?

谢谢

这是我使用的方法。 setExpandedGroup() 在更新您的 UI 之前,然后 getExpandedIds() 在更新之后。我的 idsExpand = new ArrayList<Integer>() 也是 Integer 数组。

public static void setExpandedGroup() {
        if (expListView != null
                && expListView.getExpandableListAdapter() != null) {
            final int groupCount = expListView.getExpandableListAdapter().getGroupCount();
            for (int i = 0; i < groupCount; i++) {
                if (expListView.isGroupExpanded(i)) {
                    idsExpand.add(i);
                }
            }
        }

    }

    public static void getExpandedIds() {
        if (idsExpand != null) {
            final int groupCount = idsExpand.size();
            for (int i = 0; i < groupCount; i++) {
                if (expListView != null) {
                    try {
                        expListView.expandGroup(idsExpand.get(i));
                    } catch (Exception ignored) {
                    }

                }

            }
        }
    }

希望对您有所帮助!