顾名思义,ExpandableListView是ListView的子类。它在普通的ListView的基础上进行了拓展,它把应用中的列表分为几组,每组又包含多个列表项。
基本的属性配置
> android:childDivider="#ddd" //各组内子项分隔条的颜色
> android:listSelector="#ff9" //条目选中的颜色
> android:groupIndicator="@drawable/group_indicator" //组项的图标
> android:childIndicator="@drawable/child_indicator"//子项的图标
这里子项的图标设置有问题,并没有显示,可以在自己在代码中写上(这个问题记录在这里,后期找到原因会补上,大家知道的也可以告诉我,谢谢)
效果图:
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ExpandableListView
android:id="@+id/expandableView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:childDivider="#ddd"
android:listSelector="#ff9"
android:groupIndicator="@drawable/group_indicator"
android:childIndicator="@drawable/child_indicator"
></ExpandableListView>
</RelativeLayout>
适配器设置:
private List<String> childList = null;;
private Map<String,List<String>> groupList = null;
private Context context=null;
private ViewHolder1 holderGroup;
private ViewHolder2 holderChild;
public ExpandbaleListViewAdapter( Map<String,List<String>> groupList, List<String> childList, Context context) {
super();
this.groupList = groupList;
this.childList = childList;
this.context = context;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return groupList.get(childList.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return (groupList.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
//父组件,类似于listview的getview()
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=View.inflate(context, R.layout.group_item,null);
holderGroup =new ViewHolder1();
convertView.setTag(holderGroup);
holderGroup.group_item=(TextView) convertView.findViewById(R.id.group_text);
}else{
holderGroup=(ViewHolder1) convertView.getTag();
}
if(isExpanded){//展开了
holderGroup.group_item.setTextColor(Color.RED);
}else{
holderGroup.group_item.setTextColor(Color.BLACK);
}
Log.i("yqy", groupPosition+"===="+childList.size());
holderGroup.group_item.setText(childList.get(groupPosition).toString());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
if(convertView==null){
convertView=View.inflate(context, R.layout.child_item,null);
holderChild =new ViewHolder2();
convertView.setTag(holderChild);
holderChild.child_item=(TextView) convertView.findViewById(R.id.child_text);
}else{
holderChild=(ViewHolder2) convertView.getTag();
}
String key=childList.get(groupPosition);
List<String> child =groupList.get(key);
Log.i("yqy",child.size()+"==="+child.get(childPosition));
holderChild.child_item.setText(groupList.get(key).get(childPosition));
return convertView;
}
//子项是否可以点击
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
class ViewHolder1{
TextView group_item;
}
class ViewHolder2{
TextView child_item;
}
//MainActivity:
private ExpandableListView expandalbeView;
private ExpandbaleListViewAdapter mAdapter = null;
// 显示的数据
private Map<String,List<String>> groupList =new HashMap<String,List<String>>();//所有数据
private List<String> childList = new ArrayList<String>();//这个是组的数据
private List<String> childList1 = new ArrayList<String>();//下面是四个子项的数据
private List<String> childList2 = new ArrayList<String>();
private List<String> childList3 = new ArrayList<String>();
private List<String> childList4= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.expandview);
//虚拟数据
initData();
expandalbeView =(ExpandableListView) findViewById(R.id.expandableView);
mAdapter =new ExpandbaleListViewAdapter(groupList, childList, this);
expandalbeView.setAdapter(mAdapter);
initEvent();
}
private void initEvent() {
expandalbeView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
String key=childList.get(groupPosition);
Toast.makeText(MainActivity.this, "点击了"+childList.get(groupPosition)+"下的:"+groupList.get(key).get(childPosition),1).show();
return true;
}
});
}
private void initData() {
childList.add("院系");
childList.add("教学楼");
childList.add("宿舍");
childList.add("食堂");
childList1.add("数科院");
childList1.add("音乐学院");
childList1.add("计算机科学与技术院");
childList1.add("文学院");
groupList.put("院系",childList1);
childList2.add("教A楼");
childList2.add("教B楼");
childList2.add("生化楼");
groupList.put("教学楼",childList2);
childList3.add("20#305-1");
childList3.add("20#305-2");
childList3.add("20#305-3");
childList3.add("20#305-4");
childList3.add("20#305-5");
groupList.put("宿舍",childList3);
childList4.add("南苑食堂");
childList4.add("北苑食堂");
childList4.add("学苑食堂");
groupList.put("食堂", childList4);
}