[Android] Baseexpandablelistadapter Implementing a collapsible list

Source: Internet
Author: User

Using Baseexpandablelistadapter, you can implement so-called collapsible lists, such as the ability to group friends in QQ.

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.


Package Com.example.test;import Java.util.arraylist;import Android.os.bundle;import android.app.activity;import Android.graphics.color;import Android.graphics.drawable.colordrawable;import Android.view.Gravity;import Android.view.menu;import Android.view.view;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;import Android.view.view.onclicklistener;public class Mainactivity extends Activity {@        Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Group data arraylist<string> grouplist = new arraylist<string> ();        Grouplist.add ("My Friends");        Grouplist.add ("My Loved ones");        Grouplist.add ("my Classmates");        Child data arraylist<string> itemList1 = new arraylist<string> (); ItEmlist1.add ("millet");        Itemlist1.add ("Xiao Li");        arraylist<string> itemList2 = new arraylist<string> ();        Itemlist2.add ("Xiao Li");        Itemlist2.add ("small");        Itemlist2.add ("The Few");        arraylist<string> itemList3 = new arraylist<string> ();        Itemlist3.add ("Little South");        Itemlist3.add ("Xiao Fei");        Itemlist3.add ("xiaoding");        Itemlist3.add ("Mei");        arraylist<arraylist<string>> childlist = new arraylist<arraylist<string>> ();        Childlist.add (ITEMLIST1);        Childlist.add (ITEMLIST2);        Childlist.add (ITEMLIST3);        Collapsible list Expandablelistview list = new Expandablelistview (this);        Expandablelistadapter madapter = new Myexpandablelistadapter (grouplist, childlist);        List.setadapter (Madapter);        List.setcachecolorhint (0x00000000);        List.setselector (New colordrawable (color.transparent));        List.setgroupindicator (NULL); for (int i = 0; i < Madapter.getgroupcount ();        i++) {list.expandgroup (i);    } setcontentview (list); }//adapter Private class Myexpandablelistadapter extends Baseexpandablelistadapter {private arraylist<st        Ring> grouplist;        Private arraylist<arraylist<string>> childlist;            Myexpandablelistadapter (arraylist<string> grouplist, arraylist<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.selectedgroupposit            ion = 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 TextView = null;                if (Convertview = = null) {TextView = new TextView (mainactivity.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 oncli                    CK (View v) {setselectedposition (groupposition, childposition);                Notifydatasetchanged ();            }            });        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 (mainactivity.this);            Cotain.setpadding (0, 10, 0, 10);            Cotain.setgravity (gravity.center_vertical);            ImageView imgindicator = new ImageView (mainactivity.this); TextView TextView = new TextView (mainactivIty.this);            Textview.settext (Getgroup (groupposition). toString ());            Textview.setpadding (5, 0, 0, 0);            if (isexpanded) {imgindicator.setbackgroundresource (R.DRAWABLE.BG_ARROW_UP);            } else {Imgindicator.setbackgroundresource (r.drawable.bg_arrow_down);            } cotain.addview (Imgindicator);            Cotain.addview (TextView);        return cotain;        } public boolean Hasstableids () {return true;        } public boolean ischildselectable (int groupposition, int childposition) {return true; }} @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu)        ;    return true; }}

Grouplist for group names (similar to QQ friends, classmates, friends),

Childlist Each element is a set of sub-data (similar to the Zhang San in QQ, John Doe Collection)




[Android] Baseexpandablelistadapter Implementing a collapsible list

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.