Expandablelistview1Activity.java
package com.wangzhu.demoexpandablelistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; public class Expandablelistview1Activity extends Activity { private ExpandableListView expandableListView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.expandablelistview1); init(); } private void init() { expandableListView1 = (ExpandableListView) findViewById(R.id.expandableListView1); getDatas(); } private void getDatas() { // 建立兩個一級條目標題 Map<String, String> title_1 = new HashMap<String, String>(); title_1.put("group", "移動開發"); Map<String, String> title_2 = new HashMap<String, String>(); title_2.put("group", "男人的需求"); // 建立一級條目容器 List<Map<String, String>> groups = new ArrayList<Map<String, String>>(); groups.add(title_1); groups.add(title_2); // 建立二級條目內容 // 內容一 Map<String, String> content_1 = new HashMap<String, String>(); content_1.put("child", "Android"); Map<String, String> content_2 = new HashMap<String, String>(); content_2.put("child", "Ios"); List<Map<String, String>> childs_1 = new ArrayList<Map<String, String>>(); childs_1.add(content_1); childs_1.add(content_2); // 內容二 Map<String, String> content_3 = new HashMap<String, String>(); content_3.put("child", "金錢"); Map<String, String> content_4 = new HashMap<String, String>(); content_4.put("child", "權利"); Map<String, String> content_5 = new HashMap<String, String>(); content_5.put("child", "女人"); List<Map<String, String>> childs_2 = new ArrayList<Map<String, String>>(); childs_2.add(content_3); childs_2.add(content_4); childs_2.add(content_5); // 存放兩個內容,以便顯示在列表中 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>(); childs.add(childs_1); childs.add(childs_2); /** * 引數1:上下文物件context 引數2:一級條目目錄集合 引數3:一級條目對應的佈局檔案 * 引數4:fromto,就是map中的key,指定要顯示的物件 引數5:與引數4對應,指定要顯示在groups中的id * 引數6:二級條目目錄集合 引數7:二級條目對應的佈局檔案 引數8:fromto,就是map中的key,指定要顯示的物件 * 引數9:與引數8對應,指定要顯示在childs中的id */ SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groups, R.layout.expandablelistview1_groups, new String[] { "group" }, new int[] { R.id.textGroup }, childs, R.layout.expandablelistview1_child, new String[] { "child" }, new int[] { R.id.textChild }); expandableListView1.setAdapter(adapter); } }
expandablelistview1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ExpandableListView android:id="@+id/expandableListView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ExpandableListView> </LinearLayout>
expandablelistview1_groups.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textGroup" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="6dp" android:paddingLeft="40dp" android:paddingTop="6dp" android:text="No data" android:textSize="15sp" /> </LinearLayout>
expandablelistview1_child.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textChild" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="10dp" android:paddingLeft="40dp" android:paddingTop="10dp" android:text="No data" android:textSize="20sp" /> </LinearLayout>
備註:
簡單的應用,網上匯出都可見,理論就不寫了,直接原始碼吧!