Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 使用ExpandableListView實現時間軸效果

使用ExpandableListView實現時間軸效果

編輯:關於Android編程

不廢話,先上圖,看看效果\

 

這是用ExpandableListView來實現時間軸效果,原理比較簡單,以月份為第一級,以天為第二級來實現的。

 

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;
/**
 * 時間軸
 * @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;

	@Override
	public 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); // 去掉默認帶的箭頭

		// 遍歷所有group,將所有項設置成默認展開
		int groupCount = expandlistView.getCount();
		for (int i = 0; i < groupCount; i++) {
			expandlistView.expandGroup(i);
		}
		expandlistView.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		
	}

	private void putInitData() {
		String[] strArray = new String[]{6月, 7月, 8月};
		String[] str1 = new String[]{抓娃娃,買100送1000, 抓娃娃,買100送100, 抓娃娃,買100送500, 抓娃娃,買100送2000};
		String[] str2 = new String[]{抓娃娃,買100送100, 抓娃娃,買100送1000, 抓娃娃,買100送1000, 抓娃娃,買100送500};
		String[] str3 = new String[]{抓娃娃,買100送1000, 抓娃娃,買100送1000, 抓娃娃,買100送100, 抓娃娃,買100送1000};
		
		String[] timeStr1 = new String[]{12日
09:00 AM, 8日
13:00 AM, 13日
09:00 AM, 29日
22:00 AM};
		String[] timeStr2 = new String[]{4日
09:00 AM, 8日
09:00 AM, 13日
09:00 AM, 29日
09:00 AM};
		String[] timeStr3 = new String[]{4日
13:00 AM, 8日
09:00 AM, 13日
09:00 AM, 29日
09:00 AM};
		
		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

下面是適配器代碼

 

 

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;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return oneList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if(oneList.get(groupPosition).getTwoList() == null){
			return 0;
		}else{
			return oneList.get(groupPosition).getTwoList().size();
		}
	}

	@Override
	public OneStatusEntity getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return oneList.get(groupPosition);
	}

	@Override
	public TwoStatusEntity getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return oneList.get(groupPosition).getTwoList().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public 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);
		}
		//設置第一級月份
		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;
	}

	@Override
	public View getChildView(int groupPosition, int 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);
		}
		//設置第二級時間和事件名稱
		viewHolder.actionName.setText(entity.getActionName());
		viewHolder.actionTime.setText(entity.getActionTime());
		convertView.setTag(viewHolder);
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}
	
	private class GroupViewHolder {
		TextView groupName;
		public TextView group_tiao;
	}
	
	private class ChildViewHolder {
		public TextView actionName;
		public TextView actionTime;
		public TextView child_tiao;
	}

}

代碼比較簡單,重要的地方做了注釋,大家可以把代碼下載下來修改成自己想要的效果,由於我下載分沒有了現在0分,所以收大家一分,莫怪!謝謝

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved