不足之处请多多指正,共同学习
public class MainActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListAdapter adapter = new ExpandableListAdapter() {
String[] groups = new String[]
{ "神族兵种", "虫族兵种", "人族兵种"};
String[][] child = new String[][]
{
{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },
{ "小狗", "刺蛇", "飞龙", "自爆飞机" },
{ "机枪兵", "护士MM" , "幽灵" }
};
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return child[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child[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 true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView v;
if (convertView == null){
v = new TextView(MainActivity.this);
v.setPadding(45,0,0,0);
v.setTextSize(20);
}else{
v = (TextView) convertView;
}
v.setText(groups[groupPosition]);
return v;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView v;
if (convertView == null){
v = new TextView(MainActivity.this);
v.setPadding(60,0,0,0);
v.setTextSize(15);
}else{
v = (TextView) convertView;
}
v.setText(child[groupPosition][childPosition]);
return v;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void onGroupExpanded(int groupPosition) {
//选项组展开
}
@Override
public void onGroupCollapsed(int groupPosition) {
//选项组折叠
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return childId;
}
@Override
public long getCombinedGroupId(long groupId) {
return groupId;
}
};
setListAdapter(adapter);
}
}