需求:只能打开一个Group,即实现group打开状态互斥
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
for (int i = 0; i < expandableListView.getExpandableListAdapter().getGroupCount(); i++) {
if (expandableListView.isGroupExpanded(i) && i != groupPosition) {
expandableListView.collapseGroup(i);
}
}
}
});
需求:默认打开某一个Group
expandableListView.expandGroup(0);
需求:隐藏默认的Indicator(小箭头)
android:groupIndicator="@null"
需求:group和child之间需要有间隔
divider:设置了divider之后group和child之间会增加dividerHeight的高度,所以我将不使用divider,而是在group的布局中添加一个View,然后根据group的展开/关闭状态来显示/隐藏 这个View。
ChildView为GridView,GridView需要单独重写onMeasure方法,否则只会显示一行;包含的childCount和gridView的count要区分开,很容易混。
GridView的ItemOnclickListener设置的地方需要注意,否则groupPosition容易出问题。
在Adapter的getGroupView方法中有个状态isExpanded是有大用处的,可以根据这个状态来设置不同的GroupView展示
注意:(STO上有哥们踩过坑)
Note: You cannot use the valuewrap_content
for theandroid:layout_height
attribute of a ExpandableListView in XML if the parent's size is also not strictly specified (for example, if the parent were ScrollView you could not specify wrap_content since it also can be any length. However, you can use wrap_content if the ExpandableListView parent has a specific size, such as 100 pixels.
参考:http://my.oschina.net/amigos/blog/62614