第一种:
1.main.xml中设置ExpandableListView的格式,其中id使用android自带的list
2.新建一个group.xml,这个布局文件用于控制一级条目的格式
3.新建一个child.xml,这个布局文件用于控制二级条目的格式
4.正文来了哟~~
-
定义一个List,该List对象为一级条目提供数据
List<Map<String,String>> groups = new ArrayList<Map<String,String>>();
Map<String,String> Ts = new HashMap<String,String>();
Ts.put("group", "Ts");
Map<String,String> Ps = new HashMap<String,String>();
Ps.put("group", "Ps");
groups.add(Ts);
groups.add(Ps);
定义一个List,该List对象为第一个一级条目提供二级条目的数据
List<Map<String,String>> tts = new ArrayList<Map<String,String>>();
Map<String,String> tt1 = new HashMap<String,String>();
tt1.put("tp","small gui");
tts.add(tt1);
Map<String,String> tt2 = new HashMap<String,String>();
tt2.put("tp","wang ye");
tts.add(tt2);
定义一个List,该List对象为第二个一级条目提供二级条目的数据
List<Map<String,String>> pps = new ArrayList<Map<String,String>>();
Map<String,String> pp1 = new HashMap<String,String>();
pp1.put("tp", "small wan");
pps.add(pp1);
Map<String,String> pp2 = new HashMap<String,String>();
pp2.put("tp", "wang fei");
pps.add(pp2);
定义一个List,该List对象用于存储所有二级条目的数据
List<List<Map<String,String>>> tps = new ArrayList<List<Map<String,String>>>();
tps.add(tts);
tps.add(pps);
SimpleExpandableListAdap ter sela = new SimpleExpandableListAdap ter(
this, groups, R.layout.group, new String[] { "group" },
new int[] { R.id.groupTo }, tps, R.layout.child,
new String[] { "tp" }, new int[] { R.id.childTo });
-
将SimpleExpandableListAdap ter对象设置给当前的ExpandableListAdapter
setListAdapter(sela);
- 两个图片数组,一个放group的图片,一个放child的图片
- 两个字符数组,一个放group的文字,一个放child的文字
- 四个和group相关的方法
- 四个跟child有关的方法,与上面类似
public Object getChild(int groupPosition, int childPosition) {
return tph[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return tph[groupPosition].length;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
LinearLayout cl = new LinearLayout(expandlistview.this);
cl.setOrientation(0);
ImageView les = new ImageView(expandlistview.this);
les.setImageResource(leses[groupPosition][childPosition]);
cl.addView(les);
TextView textView = getTextView();
textView.setText(getChild(groupPosition,childPosition).toString());
cl.addView(textView);
return cl;
}
- group和child都用到了的一个控制文字格式的方法,具体的每行的意思。。。有些不懂。。。
- 还有两个方法,看名字就知道意思了
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}