使用ExpandableListView时间轴效果达到
不废话,首先在地图上,查看结果
这是用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分钟。所以,我收到一分。莫怪!谢谢
下载链接:点击打开链接