/*这个demo演示了为ExpanableListView提供数据的第三种方式使用SimpleExpandableListAdapter*/
public class MainActivity extends ExpandableListActivity {
private static final String NAME = "name";
private static final String IS_EVEN = "is_even";
private ExpandableListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 定义group的数据集合,Map集合中存放了需要在group当中显示的数据的名称和值
List<Map<String, String>> groupDatas = new ArrayList<Map<String, String>>();
// 定义child的数据集合,第一个list表示分属于哪一个group,第二个list表示哪个child
List<List<Map<String, String>>> childDatas = new ArrayList<List<Map<String, String>>>();
// 为数据集填充数据
for (int i = 0; i < 20; i++) {
Map<String, String> curGroupData = new HashMap<String, String>();
curGroupData.put(NAME, "Group " + i);
curGroupData.put(IS_EVEN, (i % 2 == 0) ? "This group is even"
: "This group is odd");
groupDatas.add(curGroupData);
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 15; j++) {
Map<String, String> curChildData = new HashMap<String, String>();
curChildData.put(NAME, "Child " + j);
curChildData.put(IS_EVEN, (j % 2 == 0) ? "This child is even"
: "This child is odd");
children.add(curChildData);
}
childDatas.add(children);
}
mAdapter = new SimpleExpandableListAdapter(
this,
groupDatas,
// 伸展状态android.R.layout.simple_expandable_list_item_1中只有一个textView只能显示一项
android.R.layout.simple_expandable_list_item_2,
// 收缩状态使用android.R.layout.simple_expandable_list_item_2,这个布局中包含两个textView
android.R.layout.simple_expandable_list_item_1,
new String[]{NAME,IS_EVEN},
new int[]{android.R.id.text1,android.R.id.text2},
childDatas,
android.R.layout.simple_expandable_list_item_2,
new String[]{NAME,IS_EVEN},
new int[]{android.R.id.text1,android.R.id.text2});
setListAdapter(mAdapter);
}
}
Android Api Demos登顶之路(109)View-->ExpanableLists-->Simple Adapter
最新推荐文章于 2022-04-04 13:01:20 发布