Use expandablelistview to implement timeline Effects

Source: Internet
Author: User

No nonsense. First, check the effect.


This is to use expandablelistview to implement the timeline effect. The principle is relatively simple, with the month as the first level and the day as the second level.

Package COM. hj. main; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. content. context; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. expandablelistview; import android. widget. expandablelistview. ongroupclicklistener; import COM. hj. adapter. statusexpandadapter; import COM. hj. entity. onestatusentity; import COM. hj. entity. twostatusentity; import COM. sanrenx. main. r;/*** timeline * @ author shuiji **/public class mainactivity extends activity {Private Static final string tag = "haijiang"; private list <onestatusentity> onelist; private expandablelistview expandlistview; private statusexpandadapter statusadapter; private context; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Context = This; expandlistview = (expandablelistview) findviewbyid (R. id. expandlist); putinitdata (); statusadapter = new statusexpandadapter (context, onelist); expandlistview. setadapter (statusadapter); expandlistview. setgroupindicator (null); // remove the default arrow // traverse all groups and set all items to expand int groupcount = expandlistview by default. getcount (); For (INT I = 0; I <groupcount; I ++) {expandlistview. expandgroup (I);} expandlistview. listener (New ongroupclicklistener () {@ overridepublic Boolean ongroupclick (expandablelistview parent, view V, int groupposition, long ID) {// todo auto-generated method stubreturn true ;}});} private void putinitdata () {string [] strarray = new string [] {"June", "July", "August "}; string [] str1 = new string [] {"catch dolls, buy 100 get 1000", "catch dolls, buy 100 get 100 get 100", "catch dolls, buy 500 get ", "Catch dolls, buy 100 get 2000"}; string [] str2 = new string [] {"catch dolls, buy 100 get 100", "catch dolls, buy 100 get 1000 "," catch dolls, buy 100 get 1000 "," catch dolls, buy 100 get 500 "}; string [] str3 = new string [] {"catch dolls, buy 100 get 1000", "catch dolls, buy 100 get 1000 get 100", "catch dolls, buy 100 get ", "grab the doll, buy 100 get 1000"}; string [] timestr1 = new string [] {"12 \ n09: 00 AM", "8 \ N13: 00 am "," 13 \ n09: 00 AM "," 29 \ n22: 00 AM "}; string [] timestr2 = new string [] {"4 \ n09: 00 AM", "8 \ n09: 00 AM", "13 \ n09: 00 AM ", "29th \ n09: 00 AM"}; string [] timestr3 = new string [] {"4th \ N13: 00 AM", "8th \ n09: 00 am "," 13 \ n09: 00 AM "," 29 \ n09: 00 AM "}; onelist = new arraylist <onestatusentity> (); for (INT I = 0; I <strarray. length; I ++) {onestatusentity one = new onestatusentity (); one. setstatusname (strarray [I]); List <twostatusentity> twolist = new arraylist <twostatusentity> (); string [] Order = str1; string [] time = timestr1; Switch (I) {Case 0: Order = str1; time = timestr1; log. I (TAG, "str1"); break; Case 1: Order = str2; time = timestr2; log. I (TAG, "str2"); break; Case 2: Order = str3; time = timestr3; log. I (TAG, "str3"); break;} For (Int J = 0; j <order. length; j ++) {twostatusentity two = new twostatusentity (); Two. setactionname (Order [J]); Two. setactiontime (time [J]); twolist. add (two);} One. settwolist (twolist); onelist. add (one );}}}

The following is the adapter code.

Package COM. hj. adapter; import Java. util. list; import android. content. context; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. baseexpandablelistadapter; import android. widget. textview; import COM. hj. entity. onestatusentity; import COM. hj. entity. twostatusentity; import COM. sanrenx. main. r; public class statusexpandadapter extends baseexpandablel Istadapter {// Private Static final string tag = "statusexpandadapter"; private layoutinflater Inflater = NULL; private list <onestatusentity> onelist; private context; Public statusexpandadapter (context, list <onestatusentity> onelist) {This. onelist = onelist; Inflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); this. context = context;} @ overridepublic int Getgroupcount () {// todo auto-generated method stubreturn onelist. size () ;}@ overridepublic int getchildrencount (INT groupposition) {If (onelist. get (groupposition ). gettwolist () = NULL) {return 0;} else {return onelist. get (groupposition ). gettwolist (). size () ;}@ overridepublic onestatusentity getgroup (INT groupposition) {// todo auto-generated method stubreturn onelist. get (groupposition) ;}@ overridepublic Twostatusentity getchild (INT groupposition, int childposition) {// todo auto-generated method stubreturn onelist. get (groupposition ). gettwolist (). get (childposition) ;}@ overridepublic long getgroupid (INT groupposition) {// todo auto-generated method stubreturn groupposition ;}@ overridepublic long getchildid (INT groupposition, int childposition) {// todo auto-generated method stubreturn childposition ;} @ Overridepublic Boolean hasstableids () {// todo auto-generated method stubreturn false;} @ overridepublic view getgroupview (INT groupposition, Boolean isexpanded, view convertview, viewgroup parent) {groupviewholder holder = new groupviewholder (); If (convertview = NULL) {convertview = Inflater. inflate (R. layout. one_status_item, null);} // sets the first-level month holder. groupname = (textview) convertview. findviewbyid (R. Id. one_status_time); holder. group_tiao = (textview) convertview. findviewbyid (R. id. group_tiao); holder. groupname. settext (onelist. get (groupposition ). getstatusname (); Return convertview;} @ overridepublic view getchildview (INT groupposition, int childposition, Boolean islastchild, view convertview, viewgroup parent) {childviewholder viewholder = NULL; twostatusentity entity = getchild (groupposition, child Position); If (convertview! = NULL) {viewholder = (childviewholder) convertview. gettag () ;}else {viewholder = new childviewholder (); convertview = Inflater. inflate (R. layout. two_status_item, null); viewholder. actionname = (textview) convertview. findviewbyid (R. id. action_name); viewholder. child_tiao = (textview) convertview. findviewbyid (R. id. tiao); viewholder. actiontime = (textview) convertview. findviewbyid (R. id. action_time);} // sets the second-level time and event name viewholder. actionname. settext (entity. getactionname (); viewholder. actiontime. settext (entity. getactiontime (); convertview. settag (viewholder); Return convertview;} @ overridepublic Boolean ischildselectable (INT groupposition, int childposition) {// todo auto-generated method stubreturn false;} private class groupviewholder {textview groupname; public textview group_tiao;} private class childviewholder {public textview actionname; Public textview actiontime; Public textview child_tiao ;}}

The code is relatively simple, and comments are made to the important part. You can download the code and modify it to the desired effect. Since I have not downloaded the code and now I have 0 points, I want to collect one point, no! Thank you.

: Click to open the link



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.