This article uses a demo to demonstrate the use of Expandablelistview controls in Android, such as how to bind a data source in a group/child ListView. Directly on the code such as the following:
Program Structure diagram:
The Main.xml file source code under the Layout folder is as follows:
<?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 "> <!--We'll customize how the ListView is displayed (in another layout file) without the default way of assuming that the custom ListView is displayed here this android:id=" @id/ Android:list "must be written like this- <!--android:drawselectontop=" false "This property is used to set whether the background color on the ListView will block (overwrite) the content, Assuming this is false, it means that it will not overwrite-- <expandablelistview android:id= "@id/android:list" android:layout_width = "Fill_parent" android:layout_height= "wrap_content" android:layout_weight= "1" android: Drawselectorontop= "false"/> </LinearLayout>
The Contactsactivity.java source code in the package Com.andyidea.demo is as follows:
Package Com.andyidea.demo;import Java.util.arraylist;import Java.util.list;import Android.app.expandablelistactivity;import Android.os.bundle;import Android.view.gravity;import Android.view.View; Import Android.view.viewgroup;import Android.view.window;import Android.widget.abslistview;import Android.widget.baseexpandablelistadapter;import Android.widget.textview;public class ContactsActivity extends Expandablelistactivity {list<string> Group; Group List list<list<string>> child; Sub-list Contactsinfoadapter adapter; Data adapter/** Called when the activity is first created. */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Set to untitled Setcontentview (R.layout.main); Getexpandablelistview (). Setbackgroundresource (R.DRAWABLE.DEFAULT_BG); Initializedata (); Getexpandablelistview (). Setadapter (New Contactsinfoadapter ()); Getexpandablelistview (). Setcachecolorhint (0); Prevent black background When setting drag list}/** * Initialize group, sub-list data */private void initializedata () {group = new arraylist< String> (); Child = new arraylist<list<string>> (); Addinfo ("Andy", New string[]{"male", "138123***", "GuangZhou"}); Addinfo ("Fairy", new string[]{"female", "138123***", "GuangZhou"}); Addinfo ("Jerry", New string[]{"male", "138123***", "ShenZhen"}); Addinfo ("Tom", New string[]{"female", "138123***", "Shanghai"}); Addinfo ("Bill", New string[]{"male", "138231***", "Zhanjiang"}); }/** * Add data to group, sub-list * @param g-group * @param c-child */private void Addinfo (String g,string[] c) {Group.add (g); list<string> ChildItem = new arraylist<string> (); for (int i=0;i<c.length;i++) {childitem.add (c[i]); } child.add (ChildItem); } class Contactsinfoadapter extends baseexpandablelistadapter{//-----------------child----------------// @Overridepublic Object getchild (int groupposition, int childposition) {return child.get (groupposition). Get (Childposit ION);} @Overridepublic long Getchildid (int groupposition, int childposition) {return childposition;} @Overridepublic int getchildrencount (int groupposition) {return child.get (groupposition). Size (); @Overridepublic view Getchildview (int groupposition, int childposition,boolean islastchild, view Convertview, ViewGroup Parent) {String string = Child.get (groupposition). get (childposition); return Getgenericview (string);} ----------------Group----------------//@Overridepublic Object getgroup (int groupposition) {return Group.get ( groupposition);} @Overridepublic long getgroupid (int groupposition) {return groupposition;} @Overridepublic int GetGroupCount () {return group.size ();} @Overridepublic View getgroupview (int groupposition, Boolean Isexpanded,view Convertview, ViewGroup parent) {String String = Group.get (groupposition); Return GetgeNericview (string);} Create Group/Child View public TextView Getgenericview (String s) {//Layout parameters for the Expandablelistview Abslistview.layoutparams LP = new Abslistview.layoutparams (ViewGroup.LayoutParams.FILL _parent, 40); TextView Text = new TextView (contactsactivity.this); TEXT.SETLAYOUTPARAMS (LP); Center the text vertically text.setgravity (gravity.center_vertical | Gravity.left); Set the text starting position text.setpadding (36, 0, 0, 0); Text.settext (s); return text; } @Overridepublic Boolean hasstableids () {//TODO auto-generated method Stubreturn false;} @Overridepublic boolean ischildselectable (int groupposition, int childposition) {//TODO auto-generated method Stubreturn true;} }}
Finally, the program executes after the following example:
Expandablelistview controls in Android are basically used