1.main.xml
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= "Com.example.s02_e03_expandablelistactivity. Mainactivity ">Ten One<Expandablelistview AAndroid:layout_width= "Match_parent" -android:layout_height= "Match_parent" -Android:id= "@id/android:list" theAndroid:background= "#00FF00" -android:layout_weight= "1" -Android:drawselectorontop= "true"/> -<TextView +Android:id= "@id/android:empty" -Android:layout_width= "Match_parent" +android:layout_height= "Match_parent" AAndroid:background= "#FF0000" atandroid:text= "No data"/> - -</RelativeLayout>
2.group.xml
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= "Com.example.s02_e03_expandablelistactivity. Mainactivity ">Ten One<textview android:id= "@+id/groupto" AAndroid:layout_width= "Fill_parent" -android:layout_height= "Fill_parent" -android:paddingleft= "60px" theandroid:paddingtop= "10px" -Android:paddingbottom= "10px" -Android:textsize= "26SP" -Android:background= "#cccccc" +android:text= "No Group data"/> - +</RelativeLayout>
3.child.xml
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= "Com.example.s02_e03_expandablelistactivity. Mainactivity ">Ten One<textview android:id= "@+id/childto" AAndroid:layout_width= "Fill_parent" -android:layout_height= "Fill_parent" -android:paddingleft= "50px" theandroid:paddingtop= "5px" -Android:paddingbottom= "5px" -Android:textsize= "20SP" -android:text= "No child data"/> + -</RelativeLayout>
4.java
1 Packagecom.example.s02_e03_expandablelistactivity;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 Importjava.util.List;6 ImportJava.util.Map;7 8 Importandroid.app.ExpandableListActivity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.widget.SimpleExpandableListAdapter; One A /* - * Create an activity, inherit expandablelistacitivty - */ the Public classMainactivityextendsexpandablelistactivity { - /**Called when the activity is first created.*/ - @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + A //defines a list that provides data for a first-level entry atlist<map<string, string>> groups =NewArraylist<map<string, string>>(); -map<string,string> group1 =NewHashmap<string,string>(); -Group1.put ("group", "Group1"); -map<string, string> group2 =NewHashmap<string, string>(); -Group2.put ("group", "Group2"); - Groups.add (group1); in Groups.add (group2); - to //defines a list that provides data for level two entries for the first level entry +List<map<string,string>> child1 =NewArraylist<map<string,string>>(); -map<string, string> child1data1 =NewHashmap<string, string>(); theChild1data1.put ("Child", "Child1data1"); * Child1.add (child1data1); $Map<string,string> Child1data2 =NewHashmap<string,string>();Panax NotoginsengChild1data2.put ("Child", "Child1data2"); - Child1.add (CHILD1DATA2); the + //defines a list that provides data for level two entries for the second first level entry Alist<map<string, string>> child2 =NewArraylist<map<string, string>>(); themap<string, string> child2data =NewHashmap<string, string>(); +Child2data.put ("Child", "Child2data"); - Child2.add (child2data); $ $ //defines a list that is used to store data for all level two entries -list<list<map<string, string>>> childs =NewArraylist<list<map<string, string>>>(); - Childs.add (child1); the Childs.add (child2); - Wuyi //generate a Simpleexpandablelistadapter object the //1.context - //2. Data for first-level entries Wu //3. Layout file used to set the first-level entry style - //4. Specify key for the first level entry data About //5. Specify the ID of the first-level entry data display control $ //6. Specifying data for level two entries - //7. layout file for setting level two entry style - //8. Specify key for level two entry data - //9. Specify the ID of the two-level entry data display control ASimpleexpandablelistadapter sela =NewSimpleexpandablelistadapter ( This, groups, R.layout.group,NewString[] {"Group"},New int[] {R.id.groupto}, Childs, R.layout.child,NewString[] {"Child"},New int[] {r.id.childto}); + //sets the Simpleexpandablelistadapter object to the current expandablelistactivity the Setlistadapter (SELA); - } $ the}
Android_mars Study Notes _s02_004_expandablelistactivity