Activity_main.xml
<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:paddingbottom= "@ Dimen/activity_vertical_margin " android:paddingleft=" @dimen/activity_horizontal_margin " android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.example.expandablelistview.MainActivity " > <ExpandableListView android:id= "@+id/expandablelistview1_1" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" &nbsP; android:layout_alignparentleft= "true" Android:layout_alignparenttop= "true" android:groupindicator= "@null" > </ExpandableListView></RelativeLayout><!-- android:groupindicator= "@null" Get rid of your own arrow icons -->
Group_item.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= "wrap_content" android:orientation= "Horizontal" > <ImageView android:id= "@+id/imageviewgroup_1" android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/ic_launcher" /> <textview android:id= "@+id/ Textviewgroup_1 " android:layout_width=" Wrap_content " android: layout_height= "Wrap_content" /></linearlayout>
Child_item.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= "wrap_content" android:orientation= "Horizontal" > <ImageView android:id= "@+id/imageviewchild_1" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:src= "@drawable/ic_launcher " android:padding=" 10DP " /> <textview android:id= "@+id/ Textviewchild_1 " androiD:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Sdfds" android:padding= "10DP" /></LinearLayout>
Mainactivity
package com.example.expandablelistview;import android.app.activity;import android.os.bundle; import android.view.menu;import android.view.menuitem;import android.view.view;import android.view.viewgroup;import android.widget.baseexpandablelistadapter;import android.widget.expandablelistview;import android.widget.imageview;import android.widget.textview; public class mainactivity extends activity {private expandablelistview Expandablelistview, @Overrideprotected void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); expandablelistview= ( Expandablelistview) findviewbyid (r.id.expandablelistview1_1); Expandablelistview.setadapter (new Myexpandablelistadapter ());} @Overridepublic boolean oncreateoptionsmenu (Menu menu) {// inflate the menu ; this adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); Return true;} @Overridepublic boolean onoptionsitemselected (Menuitem item) {// handle action bar item clicks here. The action bar will// automatically handle clicks on the home/up button, so long// as you specify a parent activity in androidmanifest.xml.int id = Item.getitemid ();if (id == r.id.action_settings) {return true;} return super.onoptionsitemselected (item);} class myexpandablelistadapter extends baseexpandablelistadapter{private string[] skills = new string[]{ " WORD ", " EXCEL ", " EMAIL ", " PPT "&NBSP;&NBSP;&NBSP;&NBSP;};p Rivate string[][] groups = new string[][]{ {" Document editing ", " Document Layout ", " Document Processing ", " Document Printing "}, {"Table edit", "table layout", "Table Processing", "Table printing"}, {"Send and receive mail", "Manage Mailbox", "Login Logout", "register binding"}, {"Demo Edit", "presentation Layout", "demo Process", "Demo print"}, }; @Overridepublic int getgroupcount () {// TODO Auto-generated method Stubreturn skills.length;} Number of Level two lists @overridepublic int getchildrencount (int groupposition) {// TODO Auto-generated method stubreturn groups[groupposition].length;} Returns the object for each group @overridepublic object getgroup (int groupposition) {// TODO auto-generated method stubreturn&Nbsp;skills[groupposition];} Returns the list items in each group @overridepublic object getchild (int groupposition, int childposition) {// todo auto-generated method stubreturn groups[groupposition][childposition];} @Overridepublic long getgroupid (int groupposition) {// TODO Auto-generated Method stubreturn groupposition;} @Overridepublic long getchildid (int groupposition, int childposition) {// Todo auto-generated method stubreturn childposition;} @Overridepublic boolean hasstableids () {// TODO Auto-generated method Stubreturn true;} @Overridepublic view getgroupview (int groupposition, boolean isexpanded,view Convertview, viewgroup parent) {// todo auto-generated method stubif ( Convertview==null) {convertview=getlayoutinflater (). Inflate (R.layout.group_item,&nbsP;null);} imageview imageview= (ImageView) convertview.findviewbyid (r.id.imageviewgroup_1); textview textview= (TextView) convertview.findviewbyid (r.id.textviewgroup_1); Imageview.setimageresource (R.drawable.ic_launcher); Textview.settext (Skills[groupposition]);return Convertview;} @Overridepublic view getchildview (int groupposition, int childposition,boolean Islastchild, view convertview, viewgroup parent) {// todo auto-generated &NBSP;METHOD&NBSP;STUBIF (convertview==null) {convertview=getlayoutinflater (). Inflate (R.layout.child_item, NULL); imageview imageview= (ImageView) convertview.findviewbyid (r.id.imageviewchild_1); textview textview= (TextView) convertview.findviewbyid (r.id.textviewchild_1); Imageview.setimageresource (R.drawable.ic_launcher); Textview.settext (Groups[groupposition][childposition]);} Return convertview;} @Overridepublic boolean ischildselectable (Int&nbsP;groupposition, int childposition) {// TODO Auto-generated method Stubreturn true;}}}
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/8B/8C/wKioL1hQ_y3CFvIhAACn1k8Opfw784.png-wh_500x0-wm_3 -wmp_4-s_3195450789.png "title=" _D6RSG) 9w%l35h[w8o (_9c.png "alt=" Wkiol1hq_y3cfvihaacn1k8opfw784.png-wh_50 "/>
This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1882709
Android's Expandablelistview