public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
private ExpandableListView exp;
private AdapterDemo ad;
String[] group = { "魏", "蜀", "吴" };
String[][] names = { { "曹胚", "曹冲", "曹植", "曹洪" },
{ "郭嘉", "陈宫", "陈群", "陆抗" }, { "华雄", "费伟", "黄忠", "黄盖", } };
int[][] image = {
{ R.drawable.cp, R.drawable.cc, R.drawable.cz, R.drawable.ch },
{ R.drawable.gj, R.drawable.cg, R.drawable.cq, R.drawable.lk },
{ R.drawable.hx, R.drawable.fw, R.drawable.hz, R.drawable.hg } };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
ad = new AdapterDemo(group, names, image, this);
exp.setAdapter(ad);
exp.setOnChildClickListener(listener);
}
private void init() {
exp = (ExpandableListView) findViewById(R.id.exp);
}
OnChildClickListener listener = new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d(TAG,"=====");
Toast.makeText(MainActivity.this,"你点击了"+names[groupPosition][childPosition],0).show();
return false;
}
};
}
public class AdapterDemo extends BaseExpandableListAdapter {
private String[] group;
private String[][] names;
private int[][] image;
private Context context;
public AdapterDemo(String[] group, String[][] names, int[][] image,
Context context) {
super();
this.group = group;
this.names = names;
this.image = image;
this.context = context;
}
@Override
public int getGroupCount() {
return group.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return names[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return group[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return names[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String gp = group[groupPosition];
convertView = View.inflate(context,
android.R.layout.simple_expandable_list_item_1, null);
TextView tx=(TextView) convertView.findViewById(android.R.id.text1);
tx.setText(gp);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String name=names[groupPosition][childPosition];
int tp=image[groupPosition][childPosition];
convertView=View.inflate(context,R.layout.item,null);
ImageView ima=(ImageView) convertView.findViewById(R.id.image);
TextView tv=(TextView) convertView.findViewById(R.id.name);
ima.setImageResource(tp);
tv.setText(name);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}