<colors.xml>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gray" >#ccc</color>
</resources>
<ids.xml>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="list"/>
</resources>
<main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:childIndicator="@drawable/ic_launcher"
android:childDivider="@color/gray"
/>
</LinearLayout>
<mainActivity.java>
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//做一个ExpandableListAdapter对象
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
int [] logos=new int[]
{R.drawable.p,R.drawable.z,R.drawable.s};
private String[] armTiypes=new String[]{
"神族兵种","虫族兵种","人族 兵种"
};
private String[][]arms=new String[][]{
{"神族兵种","龙骑士","黑暗圣堂","电兵"},
{"小狗","刺蛇","飞龙","自爆飞机"},
{"机枪兵","护士MM","幽灵"}
};
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll =new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView logo= new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return armTiypes.length;
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int groupPosition) {
return armTiypes[groupPosition];
}
@Override
public int getChildrenCount(int groupPosition) {
return arms[groupPosition].length;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView=getTextView();
textView.setText(getChild(groupPosition,childPosition).toString());
return textView;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition;
}
//获取指定组位置,指定子列表项处理的子列表数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return arms[groupPosition][childPosition];
}
private TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,64
);
TextView textview=new TextView(MainActivity.this);
textview.setLayoutParams(lp);
textview.setGravity(Gravity.CENTER_VERTICAL| Gravity.LEFT);
textview.setPadding(36, 0, 0, 0);
textview.setTextSize(20);
return textview;
}
};
ExpandableListView expandListView=(ExpandableListView) findViewById(R.id.list);
expandListView.setAdapter(adapter);
}
}
转载于:https://blog.51cto.com/zjunli/1294738