pavaniiitn
pavaniiitn

Reputation: 311

how to get group having no child in ExpandableListView

I am using Custom ExpandableListview for first time. I have custom expandable listview which contains 10 groups in which some groups have child and some groups don't have child.

I want child when the child item is clicked. (in case of groups having child)

I want group when the group item is clicked. (in case of groups don't have child)

I am getting child by using OnChildClickListener(first case) , but I didn't getting group. (second case)

Is there any way to get both at a time. If group have child then return clicked child otherwise return clicked group.

Please help me

Upvotes: 0

Views: 1476

Answers (2)

Hari Krishnan
Hari Krishnan

Reputation: 6302

You can use this method to get the index of parent view, and check whether the parent in that position contains a child or not.

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;
    }
});

Upvotes: 0

pavaniiitn
pavaniiitn

Reputation: 311

First set OnChildClickListener to list

Next set OnGroupExpandListener to list

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)
    }
}

You can add OnGroupCollapseListener also

Don't use OnChildClickListener and OnGroupExpandListener together because group items are not expandable behaves like a normal ListView

Upvotes: 1

Related Questions