Use ExpandableListView to implement timeline Effects

Source: Internet
Author: User

Use ExpandableListView to implement timeline Effects

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
 
  
OneList; private ExpandableListView expandlistView; private StatusExpandAdapter statusAdapter; private Context 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 a doll, buy 100 get 1000, grab the doll, buy 100 get 100, grab the doll, buy 100 get 500 get 100, grab the doll, buy 2000 get }; string [] str2 = new String [] {catch a doll, buy 100 get 100 get 100, catch a doll, buy 1000 get 100 get 1000, catch a doll, buy get, catch a doll, buy 100 get 500}; String [] str3 = new String [] {catch a doll, buy 100 get 1000 get 100, catch a doll, buy 1000 get 100 get 100, catch a doll, buy get, grab the doll, buy 100 get 1000}; String [] timeStr1 = new String [] {12 AM, 8 AM, 13 AM, 29 AM }; string [] timeStr2 = new String [] {AM on the 4 th, AM on the 8 th, AM on the 13 th, and AM on the 29 th }; string [] timeStr3 = new String [] {AM on the 4 th, AM on the 8 th, AM on the 13 th, and AM on the 29 th; oneList = new ArrayList
  
   
(); For (int I = 0; I
   
    
TwoList = new ArrayList
    
     
(); 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
     
      
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 BaseExpandableListAdapter {// private static final String TAG = StatusExpandAdapter; private LayoutInflater inflater = null; private List
       
        
OneList; private Context context; public StatusExpandAdapter (Context context, List
        
         
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 ). getTwo List (). 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-generate D 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 GroupViewHo Lder (); 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, in T childPosition, boolean isLastChild, View convertView, ViewGroup parent) {ChildViewHolder viewHolder = null; TwoStatusEntity entity = getChild (groupPosition, childPosition); 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.

 

 

 

 

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.