<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_expanded="true" android:drawable="@drawable/ic_launcher" />
<item android:drawable="@drawable/ic_launcher"></item>
</selector>
package com.example.expendlistview;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<String> group = new ArrayList<String>();
private List<List<String>> childs = new ArrayList<List<String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expendlistview = (ExpandableListView) findViewById(R.id.expandlistview);
initData();
// 设置拖动列表的时候防止出现黑色背景
expendlistview.setCacheColorHint(0);
expendlistview.setGroupIndicator(getResources().getDrawable(R.drawable.ic_launcher));
expendlistview.setAdapter(new Myadapt());
//从1组开始
expendlistview.expandGroup(1);
}
private void initData() {
addInfo("李子", new String[] { "天空", "t", "id-54654654" });
addInfo("梅子", new String[] { "大海", "h", "id-67546454" });
addInfo("桃子", new String[] { "陆地", "l", "id-64546654" });
}
private void addInfo(String groups, String[] child) {
group.add(groups);
List<String> temp = new ArrayList<String>();
for (int i = 0; i < child.length; i++) {
temp.add(child[i]);
}
childs.add(temp);
}
private class Myadapt extends BaseExpandableListAdapter {
@Override
public int getGroupCount() {
return group.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childs.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs.get(groupPosition).get(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 str = group.get(groupPosition);
return getGenericView(str);
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String str = childs.get(groupPosition).get(childPosition);
return getGenericView(str);
}
public TextView getGenericView(String s) {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 40);
TextView text = new TextView(MainActivity.this);
text.setLayoutParams(lp);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setText(s);
return text;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}