This article uses a Demo to show how to use the ExpandableListView control in Android, such as how to bind a data source to a group or sublistview. The Code is as follows:
Program structure:
The source code of the main. xml file in the layout directory 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 will define the display mode of the listview (in another layout file) by ourselves without the default mode.
If you customize the display mode of listview, this android: id = "@ id/android: list" must be written like this -->
<! -- Android: drawSelectOnTop = "false" This attribute is used to set whether the background color on the listview is
Block (overwrite) content. If this parameter is set to false, the content will not be overwritten. -->
<ExpandableListView
Android: id = "@ id/android: list"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: drawselectid Top = "false"/>
</LinearLayout>
The source code of ContactsActivity. java in 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 <String> child; // sublist
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 it to Untitled.
SetContentView (R. layout. main );
GetExpandableListView (). setBackgroundResource (R. drawable. default_bg );
InitializeData ();
GetExpandableListView (). setAdapter (new ContactsInfoAdapter ());
GetExpandableListView (). setCacheColorHint (0); // sets the black background when dragging the list.
}
/**
* Initialize group and sublist 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 "});
}
/**
* Simulate adding data to a group or sublist
* @ 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 groupPosition, 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). size ();
}
@ Override
Public View getChildView (int groupPosition, int childPosition,
Boolean isLastChild, View 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, ViewGroup parent ){
String string = group. get (groupPosition );
Return getGenericView (string );
}
// Create a group/subview
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;
}
@ Override
Public boolean hasStableIds (){
// TODO Auto-generated method stub
Return false;
}
@ Override
Public boolean isChildSelectable (int groupPosition, int childPosition ){
// TODO Auto-generated method stub
Return true;
}
}
}
Finally, the program runs as follows:
From: Android-Idea