Like a ListView, a custom Expandablelistview only needs to write an adapter class (inherited from Baseexpandablelistadapter):
Import Java.util.list;import java.util.zip.inflater;import android.app.activity;import android.content.Context; Import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseexpandablelistadapter;import Android.widget.expandablelistadapter;import Android.widget.expandablelistview;import Android.widget.TextView; Import Android.widget.expandablelistview.onchildclicklistener;import Android.widget.expandablelistview.ongroupclicklistener;import Android.widget.linearlayout;public Class Mexpandablelistviewadapter extends Baseexpandablelistadapter{private Activity mactivity;private List<String> Listgroups;private list<list<string>> listchilds;public mexpandablelistviewadapter (Activity mActivity, List<string> listgroups,list<list<string>> listchilds) {this.mactivity = mActivity; this.listgroups = Listgroups;this.listchilds = Listchilds;} @Overridepublic Object getchild (int groupposition, int childposition) {return listchilds.get (groupposition). get (childposition);} @Overridepublic long Getchildid (int groupposition, int childposition) {return childposition;} @Overridepublic view Getchildview (int groupposition, int childposition, Boolean islastchild, view Convertview,viewgroup Parent) {Childviewholder childviewholder; View view = convertview;if (view ==null) {Childviewholder = new Childviewholder (); view = Layoutinflater.from (mactivity). I Nflate (r.layout.list_item_detail, null);} Else{childviewholder = (Childviewholder) View.gettag ();} TextView TView = (TextView) View.findviewbyid (r.id.tv_content_helper_item_list_detail); Tview.settext ( Listchilds.get (groupposition). Get (0)); View.settag (childviewholder); return view;} Class Childviewholder{linearlayout llytcontent; TextView Tvanswer;} @Overridepublic int getchildrencount (int arg0) {return listchilds.get (arg0). Size (); @Overridepublic Object getgroup (int arg0) {return listgroups.get (arg0);} @Overridepublic int GetGroupCount () {if (listgroups!=null) {return listgroups.size ();} return 0;} @Overridepublic long getgroupid (int arg0) {return arg0;} @Overridepublic view Getgroupview (int groupposition, Boolean isexpanded, View Convertview, ViewGroup parent) { Groupviewholder groupviewholder;if (convertview==null) {groupviewholder = new Groupviewholder (); ConvertView = Layoutinflater.from (mactivity). Inflate (R.layout.list_item, null); Convertview.settag (Groupviewholder);} Else{groupviewholder = (Groupviewholder) Convertview.gettag ();} TextView TView = (TextView) Convertview.findviewbyid (r.id.tv_title_helper_item_list); Tview.settext (Listgroups.get ( groupposition)); View Viewarrow = Convertview.findviewbyid (R.id.viewarrow); if (isexpanded) {viewarrow.setbackgrounddrawable (Mactivity.getresources (). Getdrawable (R.drawable.bg_arrow_round_ d));} Else{viewarrow.setbackgrounddrawable (Mactivity.getresources (). getdrawable (R.drawable.bg_arrow_round));} return Convertview;} Class Groupviewholder{textview tvquestion;} @Overridepublic Boolean HasSTableIDs () {return false;} @Overridepublic boolean ischildselectable (int groupposition, int childposition) {return true;} @Override public void ongroupexpanded (int groupposition) {super.ongroupexpanded (groupposition); }}
Where the contents of the List_item.xml layout file are:
<?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:background=" @color/whit E "android:orientation=" horizontal "android:paddingtop=" 2DP "> <textview android:id=" @+id/tv _title_helper_item_list "android:layout_width=" 0DP "android:layout_height=" Wrap_content " Android:layout_marginbottom= "15DP" android:layout_marginleft= "15DP" android:layout_margintop= "15DP" android:layout_weight= "1" android:textcolor= "@color/color_bg_index" Android:textsize= "16s P "android:textstyle=" bold "/> <view android:id=" @+id/viewarrow "Android:lay Out_width= "30DP" android:layout_height= "30DP" android:layout_gravity= "Center_vertical" an droid:layout_marginright= "15DP" android:background= "@drawable/bg_arrow_round"/></linearlayout>
Where the contents of the List_item_detail.xml layout file are:
<?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:background= "@color/color_bg" android:orientation= "vertical" android:paddingright= "10DP" > <view Android:layout_width= "Match_parent" android:layout_height= "1DP" android:background= "@drawabl E/bg_border "/> <textview android:id=" @+id/tv_content_helper_item_list_detail "Android: Layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_marginbottom= "10DP" android:layout_marginleft= "15DP" android:layout_margintop= "10DP" android:textcolor= "@colo R/color_text "android:textsize=" 14SP "/></linearlayout>
If you do not want to display the indicator icon (arrow), you need to set Groupindicator to transparent color in the layout file where the main activity is bound. i.e. <color name= "Transparent" > #00000000 </color>.
<expandablelistview android:id= "@+id/list_view_helper" android:layout_width= "Match_parent " android:layout_height= "0DP" android:scrollbars= "None" android:fadingedge= "None" android: Groupindicator= "@color/transparent" android:layout_weight= "1" android:cachecolorhint= "#00000000" android:dividerheight= "1DP" android:footerdividersenabled= "false"/>
Where Bg_arrow_round and bg_arrow_round_d are arrows to the right and down respectively:
android--Custom expandablelistview+ Hide indicator picture + anti-data display clutter