3

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?

Community
  • 1
  • 1
Jumpa
  • 4,319
  • 11
  • 52
  • 100

1 Answers1

4

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;
            }
        });
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
  • You can refer to [this](http://developer.android.com/reference/android/widget/ExpandableListView.OnGroupClickListener.html) – Ritesh Gune Aug 17 '13 at 18:19
  • 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? – Jumpa Aug 18 '13 at 08:01