This article uses a demo to showcase the use of Expandablelistview controls in Android, such as how to bind a data source in a group/Sub ListView. Directly on the code below:
Program Structure diagram:
Layout directory under the Main.xml file source code 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 define how ListView will be displayed (in another layout file) without the default, if you want to customize the way
ListView is displayed here this Android : id= "@id/android:list" must be written like this-->
<!--android:drawselectontop= "false" This property is used to set the background color on the ListView will
not Block (overwrite) content, if this is false, 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"/>
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; The data adapter/** called when the ' activity is ' is a-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/** * Initialization Group, sub-list data/private void Initializedata () {group = new ARRAYLIST&L T
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"}); /** * Simulate adding data to groups, sub lists * @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----------------//@Override public Object getchild (int grouppositi
ON, int childposition} {return Child.get (groupposition). get (Childposition);
@Override public long Getchildid (int groupposition, int childposition) {return childposition; @Override public int Getchildrencount (int groupposition) {return child.get (groupposition). Si
Ze (); @Override public View getchildview (int groupposition, int childposition, Boolean islastchild, V
Iew Convertview, ViewGroup parent) {String string = Child.get (groupposition). get (Childposition);
return Getgenericview (String);
//----------------Group----------------//@Override public Object getgroup (int groupposition) {
Return Group.get (groupposition); @Override public long getgroupid (int groupposition) {return grouppositIon
@Override public int GetGroupCount () {return group.size (); @Override public View getgroupview (int groupposition, Boolean isexpanded, View Convertview, V
Iewgroup parent) {String string = Group.get (groupposition);
return Getgenericview (String); //Create Group/Child View public TextView Getgenericview (String s) {//Layout parameters for the Expandablelistvie
W 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; @Override public boolean hasstableids () {//TOdo auto-generated method stub return false; @Override public boolean ischildselectable (int groupposition, int childposition) {//TODO auto-g
Enerated method stub return true; }
}
}
Finally, after the program is run the screenshot below:
I hope this article will help you learn about Android software programming.