Android: implements a foldable list using baseexpandablelistadapter

Source: Internet
Author: User

Baseexpandablelistadapter can be used to implement a so-called foldable list, such as the function of grouping friends in QQ.

The basic principles of baseexpandablelistadapter and baseadapter are the same, except that two groups are required when the list is imported. One group is grouparray and the other is childarray, the former is used for group names (like QQ friends, classmates, and friends), and each element of the latter is a set of sub-data (similar to the set of Zhang San and Li Si of QQ students) to implement Adapter

     Public   Class Expandableadapter Extends  Baseexpandablelistadapter {  Private Shortlist <teachersumgroupsmodel> Grouparray;  Private List <shortlist <teachersumgroupdetailmodel> Childarray; Private  Context context;  Private  Layoutinflater Inflater;  Public Expandableadapter (context, inclulist <teachersumgroupsmodel> Coursegrouplist, list <Shortlist <teachersumgroupdetailmodel> Childarray) {Inflater = (Activity) Context). getlayoutinflater ();  This . Grouparray = Coursegrouplist; This . Childarray = Childarray ;}  Public   Int  Getgroupcount (){  //  Todo auto-generated method stub              Return  Grouparray. Size ();}  Public   Int Getchildrencount ( Int  Groupposition ){  // Todo auto-generated method stub              Return  Childarray. Get (groupposition). Size ();}  Public Object getgroup ( Int  Groupposition ){  //  Todo auto-generated method stub              Return  Grouparray. Get (groupposition );}  Public Object getchild ( Int Groupposition, Int Childposition ){  //  Todo auto-generated method stub              Return  Childarray. Get (groupposition). Get (childposition );}  Public   Long Getgroupid ( Int  Groupposition ){  //  Todo auto-generated method stub              Return  Groupposition ;}  Public  Long Getchildid ( Int Groupposition, Int  Childposition ){  //  Todo auto-generated method stub              Return  Childposition ;}  Public   Boolean  Hasstableids (){  //  Todo auto-generated method stub              Return  False  ;}  Public View getgroupview ( Int Groupposition, Boolean  Isexpanded, view convertview, viewgroup parent ){  //  Todo auto-generated method stub  Textview title;  If (Convertview = Null  ) {Convertview = Inflater. Inflate (R. layout. simple_listview_item, parent, False  );} Title = (Textview) convertview. findviewbyid (R. Id. simple_listview_textview); string t = "" + Grouparray. Get (groupposition). getname (); title. settext (t );  Return  Convertview ;}  Public View getchildview ( Int Groupposition, Int  Childposition, Boolean  Islastchild, view convertview, viewgroup parent ){  //  Todo auto-generated method stub  Textview title;  If (Convertview = Null  ) {Convertview = Inflater. Inflate (R. layout. simple_listview_item, parent, False  );} Title =(Textview) convertview. findviewbyid (R. Id. simple_listview_textview); string account = Childarray. Get (groupposition). Get (childposition). getaccount (); string name = Childarray. Get (groupposition). Get (childposition). getname (); string t = Account + "" + Name; log. E ( "Sumlist", "info is" + T); title. settext (t );  Return  Convertview ;}  Public   Boolean Ischildselectable (Int Groupposition, Int  Childposition ){  //  Todo auto-generated method stub              Return   True  ;}} 

CodeFrom a current project.

Similar to the key getview method of baseadapter, The getgroupview and getchildview methods are important here. In fact, the internal implementation is similar.

Related Article

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.