如何在Android ExpandableListView中检查组是展开还是折叠?

问题描述 投票:9回答:4

我正在寻找像isExpanded()或isCollapsed()这样的API来告诉我一个组是展开还是折叠。

android expandablelistview collapse expand expandablelistadapter
4个回答
13
投票

你可以使用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;
                }
            });

有关详细信息,请访问此处

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


1
投票

你的getGroupView()上的ExpandableListAdapter中有一个参数,一个表示该组扩展与否的布尔值。

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

获取显示给定组的视图。此视图仅适用于组 - 将使用getChildView(int,int,boolean,View,ViewGroup)获取组的子视图。

参数

groupPosition返回View的组的位置

isExpanded组是展开还是折叠

如果可能的话,将旧视图转换为重用。在使用之前,您应该检查此视图是否为非null且具有适当的类型。如果无法转换此视图以显示正确的数据,则此方法可以创建新视图。不能保证convertView以前是由getGroupView(int,boolean,View,ViewGroup)创建的。 parent最终将附加此视图的父级


1
投票

如果你使用BaseExpandableListAdapter类,你有getGroupView()覆盖方法。

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

所以使用这个简单的代码,你可以向上或向下显示箭头


0
投票

正如@George D所写,有ExpandableListView .isGroupExpanded(int groupPosition)方法。那么你可以添加代码来获得扩展的组位置或-1

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

    return -1;
}
© www.soinside.com 2019 - 2024. All rights reserved.