1. Introduction
Based on the baseexpandablelistadapter extension's expandablelist usage, there are currently two popular online methods: the first is to input two arrays to the baseexpandablelistadapter, and the first is a one-dimensional array that represents the group (directory header) information, the second is a two-dimensional array that represents the child (directory sub-item); the second is to construct two classes, one is the groupinfo class that represents the directory information, and the other is the ChildInfo class that represents the Sub-item information, input baseexpandablelistadapter. Through comparison, it is found that the first method is not useful because the array is fixed, while the actual project usually needs dynamically changed directories and subitems. The second method has too many files and is complicated to implement. Here we provide a method to transfer two dynamic two-dimensional arrays to implement directory structure.
2. Case studies
Package Com. Devin; Import Java. util. arraylist; Import Android. App. activity; Import Android. Graphics. color; Import Android. Graphics. drawable. colordrawable; Import Android. OS. Bundle; Import Android. View. gravity; Import Android. View. view; Import Android. View. View. onclicklistener; Import Android. View. viewgroup; Import Android. widget. baseexpandablelistadapter; Import Android. widget. expandablelistadapter; Import Android. widget. expandablelistview; Import Android. widget. imageview; Import Android. widget. linearlayout; Import Android. widget. textview; Public Class Padtestactivity Extends Activity { Protected Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); arraylist <String> grouplist = New Arraylist <string> (); For ( Int I = 0; I <3; I ++ ) {Grouplist. Add ( "Title" );} Arraylist <String> itemlist1 = New Arraylist <string>(); Itemlist1.add ( "Item1" ); Itemlist1.add ( "Item2" ); Arraylist <String> itemlist2 = New Arraylist <string> (); Itemlist2.add ( "Item1" ); Itemlist2.add ( "Item21" ); Itemlist2.add ( "Item3" ); Arraylist <String> itemlist3 = New Arraylist <string> (); Itemlist3.add ( "Item1"); Itemlist3.add ( "Item2" ); Itemlist3.add ( "Item3" ); Itemlist3.add ( "Item4" ); Arraylist <Arraylist <string> childlist = New Arraylist <string> (); Childlist. Add (itemlist1); childlist. Add (itemlist2); childlist. Add (itemlist3); expandablelistview list = New Expandablelistview ( This ); Expandablelistadapter madapter =New Myexpandablelistadapter (grouplist, childlist); list. setadapter (madapter); list. setcachecolorhint ( Zero X 00000000 ); List. setselector ( New Colordrawable (color. Transparent); list. setgroupindicator ( Null ); For ( Int I = 0; I <madapter. getgroupcount (); I ++ ) {List. expandgroup (I);} setcontentview (list );} Private Class Myexpandablelistadapter Extends Baseexpandablelistadapter { Private Arraylist <string> Grouplist; Private Arraylist <string> Childlist; myexpandablelistadapter (arraylist <String> grouplist, arraylist <string> Childlist ){ This . Grouplist = Grouplist; This . Childlist = Childlist ;} Public Object getchild ( Int Groupposition, Int Childposition ){ Return Childlist. Get (groupposition). Get (childposition );} Private Int Selectedgroupposition =-1 ; Private Int Selectedchildposition =-1; Public Void Setselectedposition ( Int Selectedgroupposition, Int Selectedchildposition ){ This . Selectedgroupposition = Selectedgroupposition; This . Selectedchildposition = Selectedchildposition ;} Public Long Getchildid ( Int Groupposition, Int Childposition ){ Return Childposition ;} Public Int Getchildrencount ( Int Groupposition ){ Return Childlist. Get (groupposition). Size ();} Public View getchildview ( Final Int Groupposition,Final Int Childposition, Boolean Islastchild, view convertview, viewgroup parent) {textview = Null ; If (Convertview = Null ) {Textview = New Textview (padtestactivity. This ); Textview. setpadding ( 32, 10, 0, 10); Convertview = Textview ;} Else {Textview = (Textview) convertview;} textview. settext (getchild (groupposition, childposition). tostring ()); If (Groupposition = Selectedgroupposition ){ If (Childposition = Selectedchildposition) {textview. setbackgroundcolor ( 0xffb6ddee );} Else {Textview. setbackgroundcolor (color. Transparent) ;}} textview. setonclicklistener ( New Onclicklistener (){ Public Void Onclick (view v) {setselectedposition (groupposition, childposition); yydatasetchanged ();}}); Return Textview ;} Public Object getgroup ( Int Groupposition ){ Return Grouplist. Get (groupposition );} Public Int Getgroupcount (){ Return Grouplist. Size ();} Public Long Getgroupid ( Int Groupposition ){ Return Groupposition ;} Public View getgroupview ( Int Groupposition, Boolean Isexpanded, view convertview, viewgroup parent) {linearlayout cotain = New Linearlayout (padtestactivity. This ); Cotain. setpadding ( 0, 10, 0, 10 ); Cotain. setgravity (gravity. center_vertical); imageview imgindicator = New Imageview (padtestactivity. This ); Textview = New Textview (padtestactivity. This ); Textview. settext (getgroup (groupposition). tostring (); textview. setpadding ( 5, 0, 0, 0 ); If (Isexpanded) {imgindicator. setbackgroundresource (R. drawable. macro_minus );} Else {Imgindicator. setbackgroundresource (R. drawable. macro_plus);} cotain. addview (imgindicator); cotain. addview (textview ); Return Cotain ;} Public Boolean Hasstableids (){ Return True ;} Public Boolean Ischildselectable ( Int Groupposition, Int Childposition ){ Return True ;}}}
AboveCodeTo baseexpandablelistadapter, two dynamic arrays grouplist (indicating the directory header information) and childlist (representing the Sub-item information) are passed to build a directory. On the one hand, the directory can be dynamically added, while on the other hand, the implementation is simplified, two birds in one fell swoop. In addition, if the baseexpandablelistadapter is used in the actual project, you need to cache the getgroupview () and getchildview () methods (the same as the listview build ), to optimize performance and prevent memory leakage. You can build it on your own.