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

自闭症网瘾萝莉.ら 提交于 2019-12-05 20:24:16

问题


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


回答1:


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)




回答2:


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




回答3:


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




回答4:


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;
}


来源:https://stackoverflow.com/questions/28907941/how-to-check-if-group-is-expanded-or-collapsed-in-android-expandablelistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!