首页 > 代码库 > android项目解刨之时间轴
android项目解刨之时间轴
最近开发的app中要用到时间轴这东西,需要实现的效果如下:
想想这个东西应该可以用listview实现吧。然后最近就模拟着去写了:
首先写 listview的item的布局:
listview_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" android:paddingRight="20dp" > <View android:id="@+id/view_0" android:layout_width="1dp" android:layout_height="25dp" android:layout_below="@+id/layout_1" android:layout_marginLeft="71dp" android:background="#A6A6A6" /> <View android:id="@+id/view_1" android:layout_width="1dp" android:layout_height="25dp" android:layout_below="@+id/layout_2" android:layout_marginLeft="71dp" android:background="#A6A6A6" /> <TextView android:id="@+id/show_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/view_1" android:layout_marginLeft="30dp" android:text="测试数据" android:textSize="12dp" /> <ImageView android:id="@+id/image" android:layout_width="15dp" android:layout_height="15dp" android:layout_below="@+id/view_1" android:layout_marginLeft="65dp" android:src=http://www.mamicode.com/"@drawable/timeline_green" />>
接下来就是写listview的adapter了:TimelineAdapter.java
package com.example.timelinetext.test; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class TimelineAdapter extends BaseAdapter { private Context context; private List<Map<String, Object>> list; private LayoutInflater inflater; public TimelineAdapter(Context context, List<Map<String, Object>> list) { super(); this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { inflater = LayoutInflater.from(parent.getContext()); convertView = inflater.inflate(R.layout.listview_item, null); viewHolder = new ViewHolder(); viewHolder.title = (TextView) convertView.findViewById(R.id.title); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } String titleStr = list.get(position).get("title").toString(); viewHolder.title.setText(titleStr); return convertView; } static class ViewHolder { public TextView year; public TextView month; public TextView title; } }最后使用:
MainActivity.java
package com.example.timelinetext.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class MainActivity extends Activity { private ListView listView; List<String> data ; private TimelineAdapter timelineAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); listView = (ListView) this.findViewById(R.id.listview); listView.setDividerHeight(0); timelineAdapter = new TimelineAdapter(this, getData()); listView.setAdapter(timelineAdapter); } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "这是第1行测试数据"); list.add(map); map = new HashMap<String, Object>(); map.put("title", "这是第2行测试数据"); list.add(map); map = new HashMap<String, Object>(); map.put("title", "这是第3行测试数据"); list.add(map); map = new HashMap<String, Object>(); map.put("title", "这是第4行测试数据"); list.add(map); return list; } }
运行效果如图:
所以模拟着去写一个时间轴,并不是什么复杂的事情,不要被UI设计的图片吓到,其实他就是一个普通的listview而已。
源码下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。