Android scalable list listview (expandablelistview)

Source: Internet
Author: User

QQ chat is used every day. Today, I suddenly think, how does Android implement list grouping? I looked at the API and found that its implementation process is also very simple. Let's take a look at the final result first!

 

1. First, create our activity and inherit from Android. App. expandablelistactivity. Read the Code directly.

Package COM. ideasandroid. sample; import android. app. expandablelistactivity; import android. OS. bundle; import COM. ideasandroid. sample. adapter. ideasexpandablelistadapter;/*** @ author ideasandroid * example of a scalable list */public class extends expandablelistactivity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // sets the list adapter ideasexpandablelistadapter setlistadapter (New ideasexpandablelistadapter (this ));}}

2. Create an adapter and inherit from Android. widget. baseexpandablelistadapter.

Package COM. ideasandroid. sample. adapter; import Java. util. arraylist; import Java. util. list; import android. content. context; import android. view. gravity; import android. view. view; import android. view. viewgroup; import android. widget. abslistview; import android. widget. baseexpandablelistadapter; import android. widget. textview;/*** @ author ideasandroid * example of a expanded (shrunk) list */public class ideasexpandablelistadapter extends baseexpandablelistadapter {private context mcontext = NULL; // test data, the development may come from the database, network .... private string [] groups = {"family", "friends", "colleagues"}; private string [] familis = {"dad", "Mom", "sister "}; private string [] Friends = {"Xiao Li", "Zhang San", "Li Si"}; private string [] colleagues = {"Chen Zong", "Li Gong ", "Li client"}; private list <string> grouplist = NULL; private list <string> itemlist = NULL; Public ideasexpandablelistadapter (context) {This. mcontext = context; grouplist = new arraylist <string> (); itemlist = new arraylist <list <string> (); initdata ();}/*** initialize data, put the relevant data in the list to facilitate processing */private void initdata () {for (INT I = 0; I <groups. length; I ++) {grouplist. add (groups [I]) ;}list <string> Item1 = new arraylist <string> (); For (INT I = 0; I <familis. length; I ++) {item1.add (familis [I]) ;}list <string> item2 = new arraylist <string> (); For (INT I = 0; I <friends. length; I ++) {item2.add (Friends [I]) ;}list <string> item3 = new arraylist <string> (); For (INT I = 0; I <colleagues. length; I ++) {item3.add (colleagues [I]);} itemlist. add (Item1); itemlist. add (item2); itemlist. add (item3);} public Boolean areallitemsenabled () {return false;}/** sets the subnode object, which is returned during event processing, some data can be stored */public object getchild (INT groupposition, int childposition) {return itemlist. get (groupposition ). get (childposition);} public long getchildid (INT groupposition, int childposition) {return childposition;}/** byte point view, here we display a Text object */public view getchildview (INT groupposition, int childposition, Boolean islastchild, view convertview, viewgroup parent) {textview text = NULL; If (convertview = NULL) {text = new textview (mcontext);} else {text = (textview) convertview;} // obtain the name string name = (string) itemlist to be displayed on the subnode. get (groupposition ). get (childposition); // sets the relevant attributes of the text view, abslistview. layoutparams Lp = new abslistview. layoutparams (viewgroup. layoutparams. fill_parent, 40); text. setlayoutparams (LP); text. settextsize (18); text. setgravity (gravity. center_vertical | gravity. left); text. setpadding (45, 0, 0, 0); text. settext (name); return text;}/** returns the number of vertices in the current group */Public int getchildrencount (INT groupposition) {return itemlist. get (groupposition ). size ();}/** return group object for data transmission. data related to the group can be directly obtained during event processing */public object getgroup (INT groupposition) {return grouplist. get (groupposition);}/** number of groups */Public int getgroupcount () {return grouplist. size () ;}public long getgroupid (INT groupposition) {return groupposition ;}/ ** group view, which is also a text view */public view getgroupview (INT groupposition, Boolean isexpanded, view convertview, viewgroup parent) {textview text = NULL; If (convertview = NULL) {text = new textview (mcontext);} else {text = (textview) convertview ;} string name = (string) grouplist. get (groupposition); abslistview. layoutparams Lp = new abslistview. layoutparams (viewgroup. layoutparams. fill_parent, 40); text. setlayoutparams (LP); text. settextsize (18); text. setgravity (gravity. center_vertical | gravity. left); text. setpadding (36, 0, 0, 0); text. settext (name); return text;}/** determines whether the group is empty. In this example, the data is fixed, so it is not empty. We return false * If the data comes from the database, when the network is used, you can write the judgment logic to this method. If it is null *, true */Public Boolean isempty () {return false;} is returned ;} /** place all the things to be processed during list shrinking */Public void ongroupcollapsed (INT groupposition) {}/** put all the items to be processed during list expansion */Public void ongroupexpanded (INT groupposition) {}/** indicates whether the child and group IDs are stable when SS changes to * the underlying data. */Public Boolean hasstableids () {return false;}/** whether the child at the specified position is selectable. */Public Boolean ischildselectable (INT groupposition, int childposition) {return true ;}}

Ideasexpandablelistview.rar

Turn: http://www.apkbus.com/android-54271-1-1.html

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.