How to handle Android SimpleExpandableListAdapter

2019-08-17 11:35发布

问题:

I have created an expandable list in my Android application using the SimpleExpandableListAdapter type. But I'm at a complete loss as to how I detect events when one of the child entries has been selected/clicked.

I've tried all the usual OnClickListener/OnChildClickListener etc, but can't seem to find (by experimentation, or half an hour googling) what the correct handler routines should be.

Any help greatfully appreciated.

回答1:

It should be:

list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    public void onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Object o = (Object)adapter.getChild(groupPosition, childPosition);
        // perform work on child object here
    }
}  

Though, it sounds like you tried this... ExpandableListView.OnChildClickListener says that it is, in fact, the way to do it.

Also, have you defined the methods allItemsAreEnabled() and/or isEnabled() for your ListAdapter? You shouldn't have to, but maybe they are currently defined and returning the wrong values?



回答2:

Also...

If you happen to be using a class that extends BaseExpandableListAdapter then their is a default implemented method you'll have to set the return boolean for.

    @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}

By default this method is returning false, swap to true(if this is the case) and your OnChildClickListener should start resolving correctly.