packageorg.crazyit.helloworld;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Gravity;importandroid.view.Menu;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.AbsListView;importandroid.widget.BaseExpandableListAdapter;importandroid.widget.ExpandableListAdapter;importandroid.widget.ExpandableListView;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.widget.TextView;public class ExpandableListViewTest extendsActivity {
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view_test);//创建一个BaseExpandableListAdapter对象
ExpandableListAdapter adapter=newBaseExpandableListAdapter(){int[] logos=new int[]{
R.drawable.p,
R.drawable.z,
R.drawable.t
};private String[] armTypes=new String[]{"神族兵种","虫族兵种","人族兵种"};private String[][] arms=newString[][]{
{"狂战士","龙骑士","黑暗圣堂","电兵"},
{"小狗","刺蛇","飞龙","自爆飞机"},
{"机枪兵","护士MM","幽灵"}
};
@Overridepublic Object getChild(int groupPosition, intchildPosition) {//TODO Auto-generated method stub
returnarms[groupPosition][childPosition];
}
@Overridepublic long getChildId(int groupPosition, intchildPosition) {//TODO Auto-generated method stub
returnchildPosition;
}privateTextView getTextView(){
AbsListView.LayoutParams lp=newAbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,64);
TextView textView=new TextView(ExpandableListViewTest.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(70, 0, 0, 0);
textView.setTextSize(20);returntextView;
}//该方法决定每个子选项的外观
@Overridepublic View getChildView(int groupPosition, intchildPosition,booleanisLastChild, View convertView, ViewGroup parent) {//TODO Auto-generated method stub
TextView textView=getTextView();
textView.setText(getChild(groupPosition,childPosition).toString());returntextView;
}
@Overridepublic int getChildrenCount(intgroupPosition) {//TODO Auto-generated method stub
returnarms[groupPosition].length;
}
@Overridepublic Object getGroup(intgroupPosition) {//TODO Auto-generated method stub
returnarmTypes[groupPosition];
}
@Overridepublic intgetGroupCount() {//TODO Auto-generated method stub
returnarmTypes.length;
}
@Overridepublic long getGroupId(intgroupPosition) {//TODO Auto-generated method stub
returngroupPosition;
}//该方法决定每个组选项的外观
@Overridepublic View getGroupView(int groupPosition, booleanisExpanded,
View convertView, ViewGroup parent) {//TODO Auto-generated method stub
LinearLayout ll=new LinearLayout(ExpandableListViewTest.this);
ll.setOrientation(0);
ImageView logo=new ImageView(ExpandableListViewTest.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);returnll;
}
@Overridepublic booleanhasStableIds() {//TODO Auto-generated method stub
return true;
}
@Overridepublic boolean isChildSelectable(intgroupPosition,intchildPosition) {//TODO Auto-generated method stub
return true;
}
};
ExpandableListView expandListView=(ExpandableListView)findViewById(R.id.list);
expandListView.setAdapter(adapter);
}
@Overridepublic booleanonCreateOptionsMenu(Menu menu) {//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.expandable_list_view_test, menu);return true;
}
}