Android expandablelist extension usage (based on baseexpandablelistadapter)

Source: Internet
Author: User

1. Introduction

Based on the Expandablelist usage based on the baseexpandablelistadapter extension, there are two main types of popular online now: The first is to pass in two arrays to Baseexpandablelistadapter, The first is a one-dimensional array that represents the group (directory header) information, the second is an array of two-dimensional arrays that represent the child (the Directory subkey), and the second is to build two classes, one for the GroupInfo class that represents the directory information, and the other for the ChildInfo class that represents the child information. Then pass in the Baseexpandablelistadapter. By comparison, it is found that the first method because the array is fixed, and the actual project often requires dynamic changes in the directory and children, it is not useful, the second method file too many, to achieve complexity. Here is a way to pass two dynamic two-dimensional arrays to implement the directory structure.

2. Case studies

 PackageCom.devin;Importjava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.graphics.Color;Importandroid.graphics.drawable.ColorDrawable;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseExpandableListAdapter;ImportAndroid.widget.ExpandableListAdapter;ImportAndroid.widget.ExpandableListView;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classPadtestactivityextendsActivity {protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); ArrayList<String> grouplist =NewArraylist<string>();  for(inti = 0; I < 3; i++) {Grouplist.add ("Title"); } ArrayList<String> ItemList1 =NewArraylist<string>(); Itemlist1.add ("Item1"); Itemlist1.add ("Item2"); ArrayList<String> ItemList2 =NewArraylist<string>(); Itemlist2.add ("Item1"); Itemlist2.add ("Item21"); Itemlist2.add ("Item3"); ArrayList<String> ItemList3 =NewArraylist<string>(); Itemlist3.add ("Item1"); Itemlist3.add ("Item2"); Itemlist3.add ("Item3"); Itemlist3.add ("Item4"); ArrayList<ArrayList<String>> childlist =NewArraylist<arraylist<string>>();        Childlist.add (ITEMLIST1);        Childlist.add (ITEMLIST2);        Childlist.add (ITEMLIST3); Expandablelistview List=NewExpandablelistview ( This); Expandablelistadapter Madapter=NewMyexpandablelistadapter (grouplist, childlist);        List.setadapter (Madapter); List.setcachecolorhint (0x00000000); List.setselector (Newcolordrawable (color.transparent)); List.setgroupindicator (NULL);  for(inti = 0; I < Madapter.getgroupcount (); i++) {list.expandgroup (i);    } setcontentview (list); }    Private classMyexpandablelistadapterextendsBaseexpandablelistadapter {PrivateArraylist<string>grouplist; PrivateArraylist<arraylist<string>>childlist; Myexpandablelistadapter (ArrayList<String> Grouplist, arraylist<arraylist<string>>childlist) {             This. grouplist =grouplist;  This. childlist =childlist; }         PublicObject Getchild (intGroupposition,intchildposition) {            returnChildlist.get (groupposition). get (Childposition); }        Private intSelectedgroupposition =-1; Private intSelectedchildposition =-1;  Public voidSetselectedposition (intSelectedgroupposition,intselectedchildposition) {             This. selectedgroupposition =selectedgroupposition;  This. selectedchildposition =selectedchildposition; }         Public LongGetchildid (intGroupposition,intchildposition) {            returnchildposition; }         Public intGetchildrencount (intgroupposition) {            returnChildlist.get (groupposition). Size (); }         PublicView Getchildview (Final intGroupposition,Final intChildposition,BooleanIslastchild, View Convertview, ViewGroup parent) {TextView TextView=NULL; if(Convertview = =NULL) {TextView=NewTextView (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 (NewOnclicklistener () { Public voidOnClick (View v) {setselectedposition (groupposition, childposition);                Notifydatasetchanged ();            }            }); returnTextView; }         PublicObject Getgroup (intgroupposition) {            returnGrouplist.get (groupposition); }         Public intGetGroupCount () {returngrouplist.size (); }         Public LongGetgroupid (intgroupposition) {            returngroupposition; }         PublicView Getgroupview (intGroupposition,Booleanisexpanded, View Convertview, ViewGroup parent) {LinearLayout Cotain=NewLinearLayout (padtestactivity. This); Cotain.setpadding (0, 10, 0, 10);            Cotain.setgravity (gravity.center_vertical); ImageView Imgindicator=NewImageView (padtestactivity. This); TextView TextView=NewTextView (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); returnCotain; }         Public BooleanHasstableids () {return true; }         Public BooleanIschildselectable (intGroupposition,intchildposition) {            return true; }    }}

In the above code, the Baseexpandablelistadapter passed two dynamic array grouplist (representing the directory header information) and Childlist (representing the subkey information) to build the directory, on the one hand can be dynamically added data, on the other hand simplifies the implementation, Double Benefit In addition, the rewritten baseexpandablelistadapter, if applied in the actual project, requires a build cache of the Getgroupview () and Getchildview () methods (as with the ListView build). To optimize performance and prevent memory leaks. A friend you need can build it yourself.

Android expandablelist extension usage (based on baseexpandablelistadapter)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.