参考:
ExpandableListView总结
Android ExpandableListView使用小结(一)
简介
ExpandableListView
可以分层次显示,用来显示两级列表。
效果图
简单使用
1 布局中使用
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2 准备数据:定义数组
String[] groupArray = new String[]{"西游记", "水浒传", "三国演义", "红楼梦"};
String[] childArray = new String[][]{{"唐三藏", "孙悟空", "猪八戒", "沙和尚"}, {"宋江", "林冲", "李逵", "鲁智深"}, {"曹操", "刘备", "孙权", "诸葛亮", "周瑜"}, {"贾宝玉", "林黛玉", "薛宝钗", "王熙凤"}};
3 设置适配器:adapter
ExpandableListView
的适配器都是实现了接口ExpandableListAdapter
,通常采用extends BaseExpandableListAdapter
的方式。
expanListView.setAdapter(new MyExpandableAdapter());
方法 | 含义 |
---|---|
getGroupCount() | 组的数量 |
getChildrenCount(int i) | 分组数量 |
getGroup(int i) | 获取组内数据 |
getChild(int i, int i1) | 获取分组数据 |
getGroupId(int i) | groupId必须唯一 |
getChildId(int i, int i1) | childId必须唯一 |
hasStableIds() | 分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们 |
getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) | 获取显示指定分组的视图 |
getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) | 获取显示指定分组中的指定子选项的视图 |
isChildSelectable(int i, int i1) | 指定位置上的子元素是否可选中 |
private class MyExpandableAdapter extends BaseExpandableListAdapter {
@Override
public int getGroupCount() {
return groupArray.length;
}
@Override
public int getChildrenCount(int i) {
return childArray[i].length;
}
@Override
public Object getGroup(int i) {
return groupArray[i];
}
@Override
public Object getChild(int i, int i1) {
return childArray[i][i1];
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) {
GroupViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.item_group, null);
holder = new GroupViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (GroupViewHolder) convertView.getTag();
}
holder.tv_group.setText(groupArray[i]);
return convertView;
}
@Override
public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {
ChildViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.item_child, null);
holder = new ChildViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ChildViewHolder) convertView.getTag();
}
holder.tv_child.setText(childArray[i][i1]);
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
public class GroupViewHolder {
TextView tv_group;
public GroupViewHolder(View convertView) {
tv_group = (TextView) convertView.findViewById(R.id.tv_group);
}
}
public class ChildViewHolder {
TextView tv_child;
public ChildViewHolder(View convertView) {
tv_child = (TextView) convertView.findViewById(R.id.tv_child);
}
}
4 监听
监听 | 含义 |
---|---|
setOnGroupClickListener(…) | group的点击事件 |
setOnChildClickListener(…) | child的点击事件 |
setOnGroupCollapseListener(…) | group收缩的监听 |
setOnGroupExpandListener(…) | group展开的监听 |
expanListView.setAdapter(new MyExpandableAdapter());
expanListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
ToastUtil.showShortToast(MainActivity.this, groupArray[i]);
return false;//true/false都可以,但是true,不会展开group
}
});
expanListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
ToastUtil.showShortToast(MainActivity.this, childArray[i][i1]);
return false;//true/false都可以,
}
});
expanListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int i) {
ToastUtil.showShortToast(MainActivity.this, groupArray[i]);
}
});
expanListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int i) {
ToastUtil.showShortToast(MainActivity.this, groupArray[i]);
}
});
源码
https://git.oschina.net/AndroidUI/ExpandableListView
高级
1 怎么让ExpandableListView默认是展开的?
for (int i = 0; i < parentMapList.size(); i++) {
expandableListView.expandGroup(i);
}
怎么把折叠图片放到右边?
准备2张图片,箭头朝上+箭头朝下。
自定义group的layout,右边放ImageView,根据isExpanded的值,判断设置哪张图片。
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
...
if (isExpanded) {
holder.iv_group.setImageResource(R.mipmap.arrow_down);
}else{
holder.iv_group.setImageResource(R.mipmap.arrow_up);
}
return convertView;
}