Click Show expansion, first look at the effect:
Starting with, 1. Start with an XML display main interface:
Main_activity.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 " android:orientation= "vertical" ><expandablelistview android:id = "@+id/expandablelistview" android:layout_width = "Match_parent" android:layout_height = "Wrap_content" /></linearlayout >
2. Enter main_activity to start displaying the work:
Defines two lists used to control string;private list<string> grouparray;//groups in group and child private list< list<string>> childarray;//Child list private expandablelistview Expandablelistview_one;
Initialize data:
Initialize data private void Initdate () { addinfo ("language", New string[]{"Oracle", "Java", "Linux", "Jquery"}); Addinfo ("Man's Needs", new string[]{"money", "career", "Power", "Woman", "house", "car", "Ball"}); Add data to the list to go to private void Addinfo (String group,string []child) { grouparray.add (group);//Add to the outer lease list list< string> ChildItem =new arraylist<string> (); for (int index=0;index<child.length;index++) { childitem.add (Child[index]); } Childarray.add (ChildItem);}
After initialization, adapter adaptation can be performed:
Expandablelistview_one.setadapter (New Expandablelistviewaadapter (Mainactivity.this));
Class Expandablelistviewaadapter extends Baseexpandablelistadapter {activity activity; Public Expandablelistviewaadapter (activity a) {activity = A; }/*-----------------child */@Override public Object getchild (int groupposition, int childposition) {//TODO auto-generated Method Stub return Childarray.get (Groupposition). get (Childposition); } @Override public long getchildid (int groupposition, int childposition) {//TODO Auto-gener Ated method stub return childposition; } @Override public View getchildview (int groupposition, int childposition, Boolean Islastchi LD, View Convertview, ViewGroup parent) {string string =childarray.get (groupposition). Get (Childpo Sition); return Getgenericview (String); } @Override public int GetchildrencounT (int groupposition) {//TODO auto-generated Method Stub return Childarray.get (Groupposition). Size ( ); }/*----------------------------Group */@Override public Object getgroup (int groupposition) { TODO auto-generated Method Stub return Getgroup (Groupposition); } @Override public int getgroupcount () {//TODO auto-generated method Stub return GR Ouparray.size (); } @Override public long getgroupid (int groupposition) {//TODO auto-generated method stub return groupposition; } @Override public View getgroupview (int groupposition, Boolean isexpanded, view Convertview , ViewGroup parent) {String string=grouparray.get (groupposition); return Getgenericview (String); } @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; } Private TextView Getgenericview (string string) {Abslistview.layoutparams layout Params =new abslistview.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup . Layoutparams.wrap_content); TextView TextView =new TextView (activity); Textview.setlayoutparams (Layoutparams); Textview.setgravity (gravity.center_vertical | Gravity.left); Textview.setpadding (40, 0, 0, 0); Textview.settext (string); return textView; } }
Android Expandablelistview Detailed