一、什么是ExpandableListActivity
二、如何创建?
2.1 在布局文件当中声明ExpandableListActivity控件:main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false"/> <TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="No data"/> </LinearLayout> |
2.2 在布局文件当中声明group的样式:group.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/groupTo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="60px" android:paddingTop="10px" android:paddingBottom="10px" android:textSize="26sp" android:text="No data" /> </LinearLayout> |
2.3 在布局文件当中声明子项的样式:child.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/childTo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="50px" android:paddingTop="5px" android:paddingBottom="5px" android:textSize="20sp" android:text="No data" /> </LinearLayout> |
2.4 创建一个Activity,继承ExpandableListActivity:
1 | public class MainActivity extends ExpandableListActivity {} |
2.5 为group创建数据:
1 2 3 4 5 6 7 8 | //定义一个List,该List对象为一级条目提供数据 List<Map<String, String>> groups = new ArrayList<Map<String, String>>(); 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); |
2.6 为child创建数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //定义一个List,该List对象为第一个一级条目提供二级条目的数据 List<Map<String, String>> child1 = new ArrayList<Map<String, String>>(); Map<String, String> child1Data1 = new HashMap<String, String>(); child1Data1.put("child", "child1Data1"); child1.add(child1Data1); Map<String,String> child1Data2 = new HashMap<String,String>(); child1Data2.put("child", "child1Data2"); child1.add(child1Data2); //定义一个List,该List对象为第二个一级条目提供二级条目的数据 List<Map<String, String>> child2 = new ArrayList<Map<String, String>>(); Map<String, String> child2Data = new HashMap<String, String>(); child2Data.put("child", "child2Data"); child2.add(child2Data); |
2.7 完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package lpq.expandable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ExpandableListActivity; import android.os.Bundle; import android.widget.SimpleExpandableListAdapter; /* * 创建一个Activity,继承ExpandableListAcitivty */ public class MainActivity extends ExpandableListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //定义一个List,该List对象为一级条目提供数据 List<Map<String, String>> groups = new ArrayList<Map<String, String>>(); 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); //定义一个List,该List对象为第一个一级条目提供二级条目的数据 List<Map<String, String>> child1 = new ArrayList<Map<String, String>>(); Map<String, String> child1Data1 = new HashMap<String, String>(); child1Data1.put("child", "child1Data1"); child1.add(child1Data1); Map<String,String> child1Data2 = new HashMap<String,String>(); child1Data2.put("child", "child1Data2"); child1.add(child1Data2); //定义一个List,该List对象为第二个一级条目提供二级条目的数据 List<Map<String, String>> child2 = new ArrayList<Map<String, String>>(); Map<String, String> child2Data = new HashMap<String, String>(); child2Data.put("child", "child2Data"); child2.add(child2Data); //定义一个List,该List对象用来存储所有的二级条目的数据 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>(); childs.add(child1); childs.add(child2); //生成一个SimpleExpandableListAdapter对象 //1.context //2.一级条目的数据 //3.用来设置一级条目样式的布局文件 //4.指定一级条目数据的key //5.指定一级条目数据显示控件的id //6.指定二级条目的数据 //7.用来设置二级条目样式的布局文件 //8.指定二级条目数据的key //9.指定二级条目数据显示控件的id SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter( this, groups, R.layout.group, new String[] { "group" }, new int[] { R.id.groupTo }, childs, R.layout.child, new String[] { "child" }, new int[] { R.id.childTo }); //将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity setListAdapter(sela); } } |
除非注明,饮水思源博客文章均为原创,转载请以链接形式标明本文地址
本文地址:http://www.alonemonkey.com/android-expandablelistactivity.html
本文链接:http://www.alonemonkey.com/android-expandablelistactivity.html