Reference blog:
http://blog.csdn.net/xyz_lmn/article/details/6906268
Http://www.apkbus.com/android-124715-1-1.html
Sometimes, using a ListView does not satisfy the functionality required by the application. Some applications require more than one set of LISTVIW, and we are going to use a new control expandablelistview--the ListView that can be expanded. Its role is to group the ListView. As if we use QQ, there are "my friends", "Strangers", "Blacklist" like, click will expand, and then click again will shrink back. Expandablelistview is a vertical scrolling view of level two list items, and unlike a ListView, it can have two layers: each layer can be expanded independently and display its children. These subkeys come from the expandablelistadapter associated with the view.
Next to each list item that can be expanded, there is an indicator arrow to indicate the current state of the list item (these states are generally expanded list items, and there are no expanded list items, sub-lists, and last child list items). You can use the method: Setchildindicator (drawable), Setgroupindicator (drawable) (or the corresponding XML file attribute) to set the style of these indicators, or you can use the default indicator.
Like a ListView, Expandablelistview is also a control that needs to be adapter as a bridge to get data. Adapter that are generally applicable to Expandablelistview inherit the Baseexpandablelistadapter class, and the two most important methods of Getgroupview and Getchildview must be overloaded.
Summary :
1. Expandablelistview extensible list needs to be implemented with Baseexpanablelistadapter subclasses, and implements Getgroupview and Getchildview two methods
2, Level list and level two enumeration data set problem
Therefore, the first level of the list to use a collection of data storage, level two list with two-level collection storage data, the code is implemented as follows:
<pre name= "code" class= "Java" >ArrayList<String> arraylist_groupdata; arraylist<arraylist<string>> arraylist_memberdata;arraylist_groupdata = new ArrayList<String> (); Arraylist_memberdata = new arraylist<arraylist<string>> (); for (int i = 0; i < 5; i++) { Arraylist_group Data.add ("Group" +i); arraylist<string> Arraylist_memberdataitem = new arraylist<string> (); for (int j = 0; J < 6; J + +) { Arraylist_memberdataitem.add ("member" +J); } Arraylist_memberdata.add (Arraylist_memberdataitem);}
3. When calling Expandablelistview.setadapter (Exadapter), only the Getgroupview method is called ( Where Expandablelistview is an instantiated object of Expandablelistview, and Exadapter is a class instance object that inherits from Baseexpandablelistadap. When the first-level list subkey is expanded, the Getgroupview method is called more than once, and when it is an expanded list subkey, the Getchildview method is called to display the level two list subkey in turn.
4. Set the distance from the level list to the left of level two list
You only need to set the Android:layout_marginleft property of the first control in the layout that corresponds to the level two list, which is the distance from the parent layout to the left of the control.
5. Set the height of the first level list
Place the layout file for the first level list all controls in a linearlayout linear layout, and then set the number of dips by setting the Android:layout_height property of this linearlayout linear layout. I did not put this layout file in a new LinearLayout linear layout before, because the parent layout is linearlayout linear layout, but also to set the Android:layout_height property value, but can not change the height of the level list, It is only possible to put all the space back into the new LinearLayout linear layout and set the Android:layout_height property value.
androidmanifest.xml--did not make any changes, the default
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "/http Schemas.android.com/apk/res/android "package=" Com.wxl.expandablelistview "android:versioncode=" 1 "android:version Name= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <applica" tion android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@style/apptheme" > <activity android:name= "Com.wxl.expandablelistview.MainA Ctivity "android:label=" @string/app_name "> <intent-filter> <action Andro Id:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/&G T </intent-filter> </activity> </application></manifest>
String.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" > Multilevel list </string > <string name= "action_settings" >Settings</string> <string name= "Hello_world" >hello World!</string></resources>
Grouplayout.xml
<?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:orientation=" Horizontal "> <linearlayout android:id=" @+id/linearlayout "android:layout_width=" Match_parent "Android : layout_height= "40dip" > <imageview android:id= "@+id/grouplayout_imageview_group" android:layou T_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/user_group"/> <R Elativelayout android:id= "@+id/grouplayout_relativelayout" android:layout_width= "Match_parent" Andro id:layout_height= "Match_parent" > <textview android:id= "@+id/grouplayout_textview" Android:layo Ut_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Group" android:textsize= "1 5SP "Android:layout_margintop= "5DP"/> <imageview android:id= "@+id/grouplayout_imageview_tubiao" android:layout_width= "Wrap_co Ntent "android:layout_height=" wrap_content "android:background=" @drawable/btn_browser "android:layout _alignparentright= "true"/> </RelativeLayout> </LinearLayout> </LinearLayout>
Memberlayout.xml
<?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:o rientation= "Horizontal" android:gravity= "center_vertical" > <imageview android:id= "@+id/ Memberlayout_imageview " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android:src= "@drawable/child_image" android:layout_marginleft= "15DP"/> <textview android: Id= "@+id/memberlayout_textview" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:text=" member " android:textsize=" 20SP "/></linearlayout>
Activity_main.xml
<linearlayout 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 " tools:context= ". Mainactivity "> <expandablelistview android:id=" @+id/expandablelistview " android:layout_width = "Wrap_content" android:layout_height= "match_parent" android:background= "@drawable/default_bg" >< /expandablelistview></linearlayout>
Mainactivity.java
Package Com.wxl.expandablelistview;import Java.util.arraylist;import Android.os.bundle;import android.util.Log; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseexpandablelistadapter;import Android.widget.expandablelistview;import Android.widget.expandablelistview.onchildclicklistener;import Android.widget.imageview;import Android.widget.textview;import Android.app.activity;import Android.content.context;public class MainActivity Extends Activity {Expandablelistview expandablelistview; Arraylist<string> Arraylist_groupdata; Arraylist<arraylist<string>> Arraylist_memberdata; Exadapter Exadapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Expandablelistview = (Expandablelistview) This.findviewbyid (R.id.expandablelistview); Arraylist_groupdata = new arraylist<string> (); Arraylist_memberdata = new arraylist<arraylist<string>> (); for (int i = 0; i < 5; i++) {Arraylist_groupdata.add ("Group" +i); arraylist<string> Arraylist_memberdataitem = new arraylist<string> (); for (int j = 0; J < 6; J + +) {Arraylist_memberdataitem.add ("member" +J); } arraylist_memberdata.add (Arraylist_memberdataitem); } exadapter = new Exadapter (this); Expandablelistview.setadapter (Exadapter); Expandablelistview.expandgroup (0);//Set the first set of open//expandablelistview.collapsegroup (0); Remove the group from expandablelistview.setgroupindicator (null);//Remove the arrow that comes with it, with the arrows on the leftmost side of the parent list, without expanding down, expand up expandablelistview. Setdivider (null);//This is to set the split line between each group. , default has split line, set NULL no split line Expandablelistview.setonchildclicklistener (new Onchildclicklistener () {@Overridepublic Boolean Onchildclick (expandablelistview parent, view view, int groupposition,int childposition,Long ID) {//TODO auto-generated method Stubexadapter.setchildselection (groupposition,childposition); Exadapter.notifydatasetchanged (); return true;}); } public class Exadapter extends Baseexpandablelistadapter {context context; int selectparentitem =-1; int selectchilditem =-1; Public Exadapter (Context context) {//TODO auto-generated constructor stub this.context = context;} public void setchildselection (int groupposition, int childposition) {selectparentitem = groupposition; Selectchilditem = childposition; } @Overridepublic Object getchild (int groupposition, int childposition) {//TODO auto-generated method Stubreturn Array List_memberdata.get (groupposition). get (childposition);} @Overridepublic long Getchildid (int groupposition, int childposition) {//TODO auto-generated method Stubreturn Childposi tion;} @Overridepublic view Getchildview (int groupposition, int childposition, Boolean islastchild, view Convertview,viewgroup Parent) {//TODO auto-generated method Stubview view = Convertview; LOG.I ("++++++++++", "groupposition=" +groupposition+ "," + "childposition" +childposition); if (null = = view) {// Get Layoutinflaterlayoutinflater Layoutinflater = (layoutinflater) context.getsystemservice (Context.LAYOUT_INFLATER_ SERVICE);//Get the corresponding layout view = Layoutinflater.inflate (r.layout.memberlayout, null);} TextView TextView = (TextView) View.findviewbyid (R.id.memberlayout_textview); Textview.settext (arraylist_ Memberdata.get (groupposition). Get (Childposition)); if (Selectchilditem = = Childposition && Selectparentitem = = groupposition) {log.i ("++++++++++", "click:" +groupposition+ "," +childposition ");} return view;} @Overridepublic int getchildrencount (int groupposition) {//TODO auto-generated method Stubreturn Arraylist_ Memberdata.get (groupposition). Size ();} @Overridepublic Object getgroup (int groupposition) {//TODO auto-generated method Stubreturn Arraylist_groupdata.get ( groupposition);} @Overridepublic int GetGroupCount () {//TODO Auto-generatEd method Stubreturn Arraylist_groupdata.size ();} @Overridepublic long getgroupid (int groupposition) {//TODO auto-generated method Stubreturn groupposition;} @Overridepublic view Getgroupview (int groupposition, Boolean isexpanded, view Convertview,viewgroup parent) {//TODO Auto -generated method Stubview view = Convertview; LOG.I ("++++++++++", "groupposition=" +groupposition); if (null = = view) {Layoutinflater Layoutinflater = (layoutinflater ) Context.getsystemservice (context.layout_inflater_service); view = Layoutinflater.inflate (R.layout.grouplayout, NULL);} TextView TextView = (TextView) View.findviewbyid (R.id.grouplayout_textview); Textview.settext (arraylist_ Groupdata.get (groupposition)); ImageView image= (ImageView) View.findviewbyid (R.id.grouplayout_imageview_tubiao); if (isexpanded) {image.setbackgroundresource (r.drawable.btn_browser2);} else {image.setbackgroundresource (r.drawable.btn_browser);} return view;} @Overridepublic Boolean hasstableids () {//TODO auto-generated method Stubreturn true;} @Overridepublic boolean ischildselectable (int groupposition, int childposition) {//TODO auto-generated method Stubreturn true;} } }
Android UI Programming (2)--multilevel list (Expandablelistview)