Life is a mirror. You laugh at it, it laughs at you, you cry to it, it cries to you.
This content: Expandablelistview multilevel list
Let's take a look at the following example
Here is the Res/layout/activity_main.xml layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:background= "@drawable/default_bg" tools:context= "com.example.expandablelistview.mainactivity$ Placeholderfragment "> <expandablelistview android:id=" @+id/list " android:layout_width=" match _parent " android:layout_height=" match_parent " android:layout_alignparentleft=" true "> </ Expandablelistview></relativelayout>
here is the Res/layout/member_listview.xml layout file:
<?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=" 40DP "android:layout_gravity=" Center_horizont Al "> <linearlayout android:layout_width=" match_parent "android:layout_height=" 40DP "Android : orientation= "Horizontal" > <imageview android:id= "@+id/imageview01" Android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:paddingtop= "10DP" android:src= "@drawable/user_group"/> <relativelayout Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" > <textvi EW android:id= "@+id/tv01" android:layout_width= "Wrap_content" android:layout _height= "Match_parent" Android:textsize= "25SP" android:textcolor= "#fff" android:gravity= "center_vertical" Android:layout_gravity= "center_vertical" android:paddingleft= "10DP"/> &L T;imageview android:id= "@+id/tubiao" android:layout_width= "Wrap_content" and roid:layout_height= "Wrap_content" android:layout_alignparentright= "true"/> </relat Ivelayout> </LinearLayout></LinearLayout>
Here is the Res/layout/member_childitem.xml layout file:
<?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:id=" @+id/childlayout " android:orientation= "Horizontal" > <imageview android:id= "@+id/child_image" Android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/child_image" Android: paddingtop= "7DP" android:layout_marginleft= "40DP"/> <linearlayout android:layout_width= "Wrap_c Ontent "android:layout_height=" wrap_content "android:orientation=" vertical "> <textview Android:id= "@+id/child_text" android:layout_width= "Wrap_content" android:layout_height= " Wrap_content "android:textsize=" 17SP "android:layout_gravity=" center_vertical "android:gr avity= "Center_vertical"/> <textview android:id= "@+id/child_text2" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:textsize= "12SP" android:layout_gravity= "center_vertical" android:gravity= "Center_vertical"/> </LinearLayout></LinearLayout>
here is the Mainactivity.java main interface file:
public class Mainactivity extends Activity {private static final string g_text = "G_text";p rivate static final string c_te XT1 = "C_text1";p rivate static final String c_text2 = "C_text2"; list<map<string, string>> groupdata = new arraylist<map<string, string>> (); list<list<map<string, string>>> childdata = new arraylist<list<map<string, String>> > (); Exadapter adapter; Expandablelistview exlist;/** Called when the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Settitle ("canyoning"); for (int i = 0; i < 3; i++) {map<string, string> groupmap = new hashmap& Lt String, string> (); Groupdata.add (groupmap);//Groupmap.put (G_text, "Group" +i), if (i = = 0) {groupmap.put (G_text, "class 18 ");} else if (i = = 1) {groupmap.put (G_text, "24 Shifts"),} else if (i = = 2) {groupmap.put (G_text, "33 shifts");} list<map<string, string>> children = New arraylist<map<string, string>> (); for (int j = 0; J < 5; J + +) {map<string, string> childmap = new Hashmap<string, string> (); Children.add (Childmap); if (j = = 0) {childmap.put (C_text1, "Dan Mei"); Childmap.put (C_TEXT2 , "Give up and get, stay, cherish." ");} else if (j = = 1) {childmap.put (C_text1, "Four-eyed sister"), Childmap.put (C_TEXT2, "Pacific Insurance, life insurance, Property insurances");} else if (j = = 2) {childmap.put (C _text1, "von Open"); Childmap.put (C_text2, "What a moment, heart ~ stop beating." ");} else if (j = = 3) {childmap.put (C_text1, "Zheng Bodybuilding"); Childmap.put (C_text2, "Love is gambling, who first fell in love, who lost, I only want to lose Once");} else if (j = = 4) {childmap.put (C_text1, "Huang"); Childmap.put (C_text2, "Squeeze into the world, don't squeeze, why bother others, bitter himself.") ");}} Childdata.add (children);} adapter = new Exadapter (mainactivity.this); exlist = (Expandablelistview) Findviewbyid (r.id.list); Exlist.setadapter ( adapter); Exlist.setgroupindicator (null); Exlist.setdivider (null);} Class Exadapter extends Baseexpandablelistadapter {context Context;public Exadapter (context context) {context = This.context;} Public View getgroupview (int GroupPOsition, Boolean Isexpanded,view Convertview, ViewGroup parent) {View view = convertview;if (view = = null) {Layoutinflater Inflater = (layoutinflater) getsystemservice (context.layout_inflater_service); view = Inflater.inflate ( R.layout.member_listview, null);} TextView title = (TextView) View.findviewbyid (R.ID.TV01);//Remember to add view otherwise error Title.settext (Getgroup (groupposition). ToString ()); ImageView image = (ImageView) View.findviewbyid (R.id.tubiao); if (isexpanded) { Image.setbackgroundresource (R.DRAWABLE.BTN_BROWSER2);} else {image.setbackgroundresource (r.drawable.btn_browser);} return view;} public long getgroupid (int groupposition) {return groupposition;} Public Object getgroup (int groupposition) {return groupdata.get (groupposition). Get (G_text). toString (); public int GetGroupCount () {return groupdata.size ();} Public View getchildview (int groupposition, int childposition,boolean islastchild, View Convertview, ViewGroup parent) {V Iew view = convertview;if (view = = null) {Layoutinflater Inflater = (layouTinflater) Getsystemservice (context.layout_inflater_service); view = Inflater.inflate (R.layout.member_childitem, NULL);} Final TextView title = (TextView) View.findviewbyid (R.id.child_text); Title.settext (Childdata.get (groupposition). Get (childposition). Get (C_TEXT1). toString ()), final TextView title2 = (TextView) View.findviewbyid (R.ID.CHILD_TEXT2); Title2.settext (Childdata.get (groupposition). Get (Childposition). Get (C_TEXT2). toString ()); return view;} public long Getchildid (int groupposition, int childposition) {return childposition;} Public Object getchild (int groupposition, int childposition) {return childdata.get (groupposition). Get (Childposition). Get (C_TEXT1). toString ();} public int Getchildrencount (int groupposition) {return childdata.get (groupposition). Size (); public Boolean hasstableids () {return true;} public boolean ischildselectable (int groupposition, int childposition) {return true;}}}
This is where we go, take your time and enjoy it
77th: User interface View Expandablelistview