ExpandableListview OnGroupClickListener Not Firing

Jumpa picture Jumpa · Aug 17, 2013 · Viewed 11k times · Source

I'm following this: Programmatically collapse a group in ExpandableListView. I want the user capable to expand only one group at a time and smoothly scroll to the right position. I've written a custom adapter that implements OnGroupClickListener:

public class CategoriesListAdapter extends BaseExpandableListAdapter implements OnGroupClickListener {

    private ExpandableListView expListView;
    private int lastExpandedGroupPosition = -1;

    // I get the expListView in the constructor...

    @Override
    public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) {

        parent.smoothScrollToPosition(groupPosition);

        if (parent.isGroupExpanded(groupPosition)) {
            parent.collapseGroup(groupPosition);
        } else {
            parent.expandGroup(groupPosition);
        }

        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

        if (groupPosition != lastExpandedGroupPosition) {
            expListView.collapseGroup(lastExpandedGroupPosition);
        }

        super.onGroupExpanded(groupPosition);
        lastExpandedGroupPosition = groupPosition;
    }
}

Unfortunately the onGroupClick event is never fired...(I've put some logs inside the method). I've also tried:

expListView.setOnGroupClickListener(new OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) {
        // Same as above...
    });
}

Same results for this alternative...any ideas?

Answer

Ritesh Gune picture Ritesh Gune · Aug 17, 2013

I would suggest you to check for the imports first (not shown in your question),and make sure you have imported android.widget.ExpandableListView.OnGroupClickListener

or replace : new OnGroupClickListener() {

with new android.widget.ExpandableListView.OnGroupClickListener {

EDIT :

I also noticed that you are returning 'true' at the body of boolean onGroupClick(...) that means "the click was handled" and groups will never be collapsed, expanded.

You should return false if you want to expand.So I suggest you to do like this :

expListView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Log.d("onGroupClick:", "worked");

                parent.smoothScrollToPosition(groupPosition);

                if (parent.isGroupExpanded(groupPosition)) {
                    parent.collapseGroup(groupPosition);
                } else {
                   parent.expandGroup(groupPosition);
                }

                return false;
            }
        });