首页 > 解决方案 > 以编程方式展开 ExpandableListView 中的特定组

问题描述

[我的问题不重复,因为我没有使用 ImageView] 我尝试了以下方法,但它扩展了所有组

 //method to expand all groups
    private void expandAll() {
        int count = expandableCategoryAdapter.getGroupCount().;
        for (int i = 0; i < count; i++) {
            expandableListView.expandGroup(i);
        }
    }

标签: androidandroid-listview

解决方案


在这里,您明确地扩展了所有组,ExpandableListView这就是所有组都被扩展的原因。

不要使用这个:

 //method to expand all groups
private void expandAll() {
    int count = expandableCategoryAdapter.getGroupCount();
    for (int i = 0; i < count; i++) {
        expandableListView.expandGroup(i);
    }
}

用这个 :

expandableListView.setOnGroupClickListener((parent, v, groupPosition, id) -> {
                    expandableListView.expandGroup(groupPosition);
                    return false;
});

推荐阅读