首先进行模拟数据
List list = null;
Map
list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
map = new HashMap<String, List<String>>();
List<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("a");
list1.add("a");
list1.add("a");
map.put("A", list1);
List<String> list2 = new ArrayList<String>();
list2.add("b");
list2.add("b");
list2.add("b");
list2.add("b");
map.put("B", list2);
List<String> list3 = new ArrayList<String>();
list3.add("c");
list3.add("c");
list3.add("c");
list3.add("c");
map.put("C", list3);
List<String> list4 = new ArrayList<String>();
list4.add("d");
list4.add("d");
list4.add("d");
list4.add("d");
map.put("D", list4);
自定义适配器,这里继承BaseExpandableListAdapter
public class MyAdapter extends BaseExpandableListAdapter {
private List<String> list;
private Map<String, List<String>> map;
private Context context;
private LayoutInflater inflater;
public MyAdapter(Context context, List<String> list,
Map<String, List<String>> map) {
// TODO Auto-generated constructor stub
this.context = context;
this.inflater = inflater.from(context);
this.list = list;
this.map = map;
}
// 得到子item需要关联的数据,即父item
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
String key = list.get(groupPosition);
return map.get(key).get(childPosition);
}
// 得到子item的id
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
// 设置子items的控件
@Override
public View getChildView(int groupPosition, int childPosition,
boolean islastChild, View converView, ViewGroup parent) {
// TODO Auto-generated method stub
String key = list.get(groupPosition);
String info = map.get(key).get(childPosition);
ViewHolder holder = new ViewHolder();
if (converView == null) {
converView = inflater.inflate(R.layout.items_sun, null, false);
holder.textView2 = (TextView) converView
.findViewById(R.id.textView2);
converView.setTag(holder);
} else {
holder = (ViewHolder) converView.getTag();
}
holder.textView2.setText(info);
return converView;
}
// 获取父items下子items的个数
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
String key = list.get(groupPosition);
return map.get(key).size();
}
// 获取父items的数据
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return list.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean is, View converView,
ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = new ViewHolder();
if (converView == null) {
converView = inflater.inflate(R.layout.items_parent, null, false);
holder.textView1 = (TextView) converView
.findViewById(R.id.textView1);
converView.setTag(holder);
} else {
holder = (ViewHolder) converView.getTag();
}
holder.textView1.setText(list.get(groupPosition));
return converView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
class ViewHolder {
TextView textView1, textView2;
}
}
这里我用了listView的优化处理,熟悉BaseAdapter的同学一定很熟悉,如果不熟悉的同学可以看一下我之前写的关于BaseAdapter的详解
地址:http://blog.csdn.net/wu996489865/article/details/46884317