public class MainActivity extends ExpandableListActivity {
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childrens = new ArrayList<List<Map<String, String>>>();
SimpleExpandableListAdapter adapter ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new SimpleExpandableListAdapter(
this,
groupData(),
android.R.layout.simple_expandable_list_item_1,
new String[]{"group"},
new int[]{android.R.id.text1},
childData(),
android.R.layout.simple_expandable_list_item_2,
new String[]{"child"},
new int[]{android.R.id.text2}
);
setListAdapter(adapter);
}
/**
* 该方法为一级条目提供数据
* @return 一级条目数据
*/
private List<Map<String, String>> groupData(){
Map<String, String> group1 = new HashMap<String, String>();
group1.put("group", "group1");
Map<String, String> group2 = new HashMap<String, String>();
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);
return groups;
}
/**
* 该方法为二级条目提供数据
* @return 二级条目数据
*/
private List<List<Map<String, String>>> childData(){
List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
Map<String, String> child1Data = new HashMap<String, String>();
child1Data.put("child", "child1");
child1.add(child1Data);
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2Data = new HashMap<String, String>();
child2Data.put("child", "child2");
child2.add(child2Data);
childrens.add(child1);
childrens.add(child2);
return childrens;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
示例二:通过SimpleCussorTreeAdapter绑定数据:
public class MainActivity extends ExpandableListActivity {
/**
* 获取联系人ID的列位置,一般都是第一个,也就是0
*/
private int mGroupIdColumnIndex;
/**
* 要查询联系人的电话号码的字段(也就是子视图的信息)
*/
private String[] mPhoneNumberProject = new String[]{
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER};
private ExpandableListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 查询联系人并获取其Cursor对象
CursorLoader loader = new CursorLoader(this,
ContactsContract.Contacts.CONTENT_URI,
new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME},
null,
null,
null);
Cursor groupCursor =loader.loadInBackground();
// 保存取联系人ID的列位置
mGroupIdColumnIndex = groupCursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
adapter = new MyExpandableListAdapter(this,
groupCursor,
android.R.layout.simple_expandable_list_item_1,
new String[]{ContactsContract.Contacts.DISPLAY_NAME},
new int[]{android.R.id.text1},
android.R.layout.simple_expandable_list_item_2,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[]{android.R.id.text2}
);
setListAdapter(adapter);
}
private class MyExpandableListAdapter extends SimpleCursorTreeAdapter{
public MyExpandableListAdapter(Context context, Cursor cursor,
int groupLayout, String[] groupFrom, int[] groupTo,
int childLayout, String[] childFrom, int[] childTo) {
super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom,
childTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
long contactId = groupCursor.getLong(mGroupIdColumnIndex);
Uri.Builder builder = ContactsContract.CommonDataKinds
.Phone.CONTENT_URI.buildUpon();
Uri phoneNumbersUri = builder.build();
CursorLoader curLoader = new CursorLoader(MainActivity.this,
phoneNumbersUri,
mPhoneNumberProject,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)},
null
);
Cursor cursor = curLoader.loadInBackground();
return cursor;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
示例三:通过BaseExpandableListAdapter绑定数据:
public class MyExpandableList extends ExpandableListActivity {
ExpandableListAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new MyExpandableListAdapter();
setListAdapter(adapter);
}
private class MyExpandableListAdapter extends BaseExpandableListAdapter{
private String[] groups = {"电影","电视剧","综艺节目","水果","青菜"};
private String[][] children = {
{"忠犬八公","泰坦尼克号","开心鬼"},
{"谈情说案","大太监","加油妈妈"},
{"快乐大本营","职来职往","爱情保卫战"},
{"泥猴桃","橘子","柿子"},
{"麦菜","娃娃菜","生菜"}};
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//得到一级目录的数据
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textVeiw = getView();
textVeiw.setText(getChild(groupPosition, childPosition).toString());
return textVeiw;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition ;
}
//得到二级目录的数据
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textVeiw = getView();
textVeiw.setText(getGroup(groupPosition).toString());
return textVeiw;
}
private TextView getView(){
AbsListView.LayoutParams layout = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 64);
TextView tv = new TextView(MyExpandableList.this);
tv.setLayoutParams(layout);
tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
tv.setPadding(60, 0, 0, 0);
return tv;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}