Android APP開發活用範例速查大辭典(Ch0203-ExpandListView顯示階層選單)

Android APP開發活用範例速查大辭典(Ch0203-ExpandListView顯示階層選單)

Android APP開發活用範例速查大辭典(Ch0203-ExpandListView顯示階層選單)

 

GUI-XML片段

<ExpandableListView
android:id=”@+id/ExpandablelistView”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”@color/FlatLightAqua” />

 

程式碼片段

// 父清單
ArrayList<HashMap<String, String>> groupData = new ArrayList<HashMap<String, String>>();
// 子清單
ArrayList<ArrayList<HashMap<String, String>>> childData = new ArrayList<ArrayList<HashMap<String, String>>>();

// 在父清單新增元素
HashMap<String, String> groupA = new HashMap<String, String>();
groupA.put(“group”, “猴”);
HashMap<String, String> groupB = new HashMap<String, String>();
groupB.put(“group”, “鳥”);

groupData.add(groupA);
groupData.add(groupB);

// 在子清單新增元素(1)
ArrayList<HashMap<String, String>> childListA = new ArrayList<HashMap<String, String>>();
HashMap<String, String> childAA = new HashMap<String, String>();
childAA.put(“group”, “猴”);
childAA.put(“name”, “日本猴”);
HashMap<String, String> childAB = new HashMap<String, String>();
childAB.put(“group”, “猴”);
childAB.put(“name”, “長臂猿”);
HashMap<String, String> childAC = new HashMap<String, String>();
childAC.put(“group”, “猴”);
childAC.put(“name”, “眼鏡猴”);

childListA.add(childAA);
childListA.add(childAB);
childListA.add(childAC);

childData.add(childListA);

// 在子清單新增元素(2)
ArrayList<HashMap<String, String>> childListB = new ArrayList<HashMap<String, String>>();
HashMap<String, String> childBA = new HashMap<String, String>();
childBA.put(“group”, “鳥”);
childBA.put(“name”, “雞”);
HashMap<String, String> childBB = new HashMap<String, String>();
childBB.put(“group”, “鳥”);
childBB.put(“name”, “雀鳥”);

childListB.add(childBA);
childListB.add(childBB);

childData.add(childListB);

// 產生含有父清單、子清單的Adapter
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
getApplicationContext(), 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[] { “name”, “group” }, new int[] {
android.R.id.text1, android.R.id.text2 });

// 在ExpandableListView設定Adapter
ExpandableListView listView = (ExpandableListView) findViewById(R.id.ExpandablelistView);
listView.setAdapter(adapter);

 

 

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *