Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 736
kyrax Avatar asked Mar 06 '15 21:03

kyrax


4 Answers

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;
}
like image 154
mbelsky Avatar answered Nov 01 '22 02:11

mbelsky


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)

like image 34
IntelliJ Amiya Avatar answered Nov 01 '22 02:11

IntelliJ Amiya


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

like image 2
Madhura Prasanna Avatar answered Nov 01 '22 03:11

Madhura Prasanna


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

like image 1
vinitius Avatar answered Nov 01 '22 03:11

vinitius