不废话,先上图,看看效果
这是用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<OneStatusEntity> 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日\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", "29日\n09:00 AM"}; String[] timeStr3 = new String[]{"4日\n13:00 AM", "8日\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); } } }
下面是适配器代码
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<OneStatusEntity> oneList; private Context context; public StatusExpandAdapter(Context context, List<OneStatusEntity> 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分,所以收大家一分,莫怪!谢谢
下载地址:点击打开链接
使用ExpandableListView实现时间轴效果
时间: 2024-09-29 22:08:02