diegodobelo / AndroidExpandingViewLibrary

This is a library to help creating expanding views with animation in Android
942 stars 120 forks source link

How to expand one item of an ExpandingList at a time, thus opening a another item would close the previously opened item? #8

Open SachinTanpure opened 7 years ago

diegodobelo commented 7 years ago

Hi @SachinTanpure . You can store the last expanded item and call toggleExpanded() method when other item is expanded. Something like this:

private ExpandingItem mExpandedItem;
...

item.setStateChangedListener(new ExpandingItem.OnItemStateChanged() {
                @Override
                public void itemCollapseStateChanged(boolean expanded) {
                    if (mExpandedItem != null && mExpandedItem.isExpanded()) {
                        mExpandedItem.toggleExpanded();
                    }
                    if (expanded) {
                        mExpandedItem = item;
                    } else if (mExpandedItem.equals(item)) {
                        mExpandedItem = null;
                    }
                }
            });

If you check the example code in this repository you can put the code above in the line 96 of MainActivity.java