ExpandableListView OnGroupCollapseListener -any listener / watcher before crashing?

ExpandableListView has a listener to detect when a group is collapsed named OnGroupCollapsedListener ( doc ). Whatever code you put into this listener is called AFTER the group breaks up.

Does anyone know how I can call some code BEFORE the group is collapsed? Perhaps BeforeCollapsedListener ???

+3


source to share


2 answers


This short snippet can be used to save the group states and check them each time the group is clicked. The group click is registered before the group is expanded and / or compensated.



private boolean[] mGroupStates;

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    mGroupStates = new boolean[mExpAdapter.getGroupCount()]

    mExpList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPos, long id) {
            // Switch boolean
            mGroupStates[groupPos] = !mGroupStates[groupPos];
            // Check expanding or collapsing
            if (mGroupStates[groupPos]) {
                // group is being expanded
            } else {
                // group is being collapsed
            }
            // False will make sure that the click continues with it operation
            return false;
        }
    });
    ...
}

      

+4


source


You can use onGroupClickListener to trigger shrink / expand



ExpandableListView lv ; // init the listView with your stuff
boolean animated = true;
lv.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            // do your stuff

            lv.expandGroup(groupPosition, animated);
            // let system handle event
            return false;
}

      

+2


source







All Articles