How to check if group is expanded or collapsed in Android ExpandableListView?
Asked Answered
D

4

11

I'm looking for an API like isExpanded() or isCollapsed() that tells me if a group is expanded or collapsed.

Delusive answered 6/3, 2015 at 21:25 Comment(1)
isGroupExpanded (int groupPosition) perhaps?Cineaste
P
16

You can use isGroupExpanded .

 expListViewObj.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

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

                    if(parent.isGroupExpanded(groupPosition))
                    {

                     // Do your Staff
                    }
                    else{

                       // Expanded ,Do your Staff

                    }


                    return false;
                }
            });

For more details you can visit Here

http://developer.android.com/reference/android/widget/ExpandableListView.html#setOnGroupClickListener(android.widget.ExpandableListView.OnGroupClickListener)

Palliasse answered 6/6, 2015 at 5:35 Comment(0)
H
2

As @George D wrote there is ExpandableListView .isGroupExpanded(int groupPosition) method. Well you could add the code to get expanded group position or -1

public int getExpandedGroupPosition() {
    for (int i = 0; i < listView.getExpandableListAdapter().getGroupCount(); i++) {
        if ( listView.isGroupExpanded(i) ) {
            return i;
        }
    }

    return -1;
}
Heal answered 17/2, 2016 at 8:51 Comment(0)
D
2

If u use BaseExpandableListAdapter class you have getGroupView() override method.

public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

    // Here is your expand list view parent id
    // b specify that parent expand or not.

    .............

    if(b){
        imdDownArrow.setVisibility(View.GONE);
        imdUpArrow.setVisibility(View.VISIBLE);
    }else{
        imdDownArrow.setVisibility(View.VISIBLE);
        imdUpArrow.setVisibility(View.GONE);
    }
    ...............
    return view;
}

so using this simple code you can show arrow up or down

Dresser answered 23/2, 2019 at 15:33 Comment(0)
V
1

There is a parameter in getGroupView() on your ExpandableListAdapter, a boolean that represents wheter the group is expanded or not.

From (http://developer.android.com/reference/android/widget/ExpandableListAdapter.html#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)

Gets a View that displays the given group. This View is only for the group--the Views for the group's children will be fetched using getChildView(int, int, boolean, View, ViewGroup)).

Parameters

groupPosition the position of the group for which the View is returned

isExpanded whether the group is expanded or collapsed

convertView the old view to reuse, if possible. You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. It is not guaranteed that the convertView will have been previously created by getGroupView(int, boolean, View, ViewGroup). parent the parent that this view will eventually be attached to

Vair answered 6/3, 2015 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.