ExpandableListView是listView的扩展类.之前做过测试,使用ExpandableListView结合baseAdapter的getView 实现扩展组项跟子项,但是会报错.不多说,上代码
layout布局: <ExpandableListView
android:id="@+id/store_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:divider="@color/touming" />
Activity代码:
/*去掉自带的箭头图标*/
listView.setGroupIndicator(null);
/*屏蔽收缩功能*/
listView.setOnGroupClickListener(new OnGroupClickListenerImpl());
其中 OnGroupClickListenerImpl直接重写return true
/*只展开一个item*/
listView.setOnGroupExpandListener(new OnGroupExpandListener() {
int lastItem = -1;
@Override
public void onGroupExpand(int groupPosition) {
if( lastItem >= 0){
listView.collapseGroup(lastItem);
}
lastItem = groupPosition;
}
});
/*子项点击事件*/
listView.setOnChildClickListener(new OnChildClickListenerImpl());
/**子项点击事件监听*/
private class OnChildClickListenerImpl implements OnChildClickListener {// 监听子项点击事件
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
int gourpsSum = adapter.getGroupCount();// 组量
for (int i = 0; i < gourpsSum; i++) {
int groupcount = adapter.getChildrenCount(i);// 组中子项数量
for (int k = 0; k < groupcount; k++) {
boolean isLast = false;
if (k == (groupcount - 1)) {
isLast = true;
}
/*被选中的子项*/
if (i == groupPosition && k == childPosition) {
adapter.getChildView(i, k, isLast, null, null);//获取子项view
adapter.getChildView(i, k, isLast, null, null).findViewById(R.id.xxxxxx);//获取子项view中的某个组件
}
}
}
}
BaseExpandableListAdapter 就不详细的多说了 网上到处都是