如何在 ExpandableListView 中获取没有 child 的组

how to get group having no child in ExpandableListView

我是第一次使用自定义 ExpandableListview。我有自定义的可扩展列表视图,其中包含 10 个组,其中一些组有 child,一些组没有 child。

单击 child 项时,我想要 child。 (如果组有 child)

我想要在点击组项时进行分组。 (如果组没有 child)

我通过使用 OnChildClickListener(first case) 得到了 child,但是我没有得到分组。 (第二种情况)

有什么办法可以同时获得两者。如果组有 child,则 return 单击 child 否则 return 单击组。

请帮帮我

您可以使用此方法获取父视图的索引,并检查该位置的父视图是否包含子视图。

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        //here you can check whether your parent contains a child or not by getting the parent position.
        return false;
    }
});

首先将OnChildClickListener设为列表

接下来将OnGroupExpandListener设置为列表

exList.setOnChildClickListener(this);
exList.setOnGroupExpandListener(this);

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    Group gr = ds_list.get(groupPosition);
    //here you can get child from group
    return true;
}

@Override
public void onGroupExpand(int i) {
    if(group don't have child) {
        //get group by using group position(i)
    }
}

您还可以添加OnGroupCollapseListener

不要将 OnChildClickListenerOnGroupExpandListener 一起使用,因为组项目不可扩展,其行为与正常 ListView

一样