ExpandableListview OnGroupClickListener Not Firing

11,188

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;
            }
        });
Share:
11,188
Jumpa
Author by

Jumpa

Updated on June 06, 2022

Comments

  • Jumpa
    Jumpa about 2 years

    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?

  • Ritesh Gune
    Ritesh Gune almost 11 years
    You can refer to this
  • Jumpa
    Jumpa almost 11 years
    Imports are correct. If I set return to false, the group is not being correctly collapsed at the first click (I need 2 click) and no event at all is being fired. Anyway, why the onGroupClickListener override on the adapter doesn't work? Last one: why the scroll is smooth on 4.3 and too fast on 2.3.3?