最近項(xiàng)目需要用到時間軸的效果,網(wǎng)上看了幾個例子,和想要實(shí)現(xiàn)的效果也不盡相同,下面將我的實(shí)現(xiàn)方法和思路說一下。首先看下效果圖:
數(shù)據(jù)是隨便填的,顯得有點(diǎn)亂,但是不影響效果。實(shí)現(xiàn)方面主要是用ListView來實(shí)現(xiàn),主要是根據(jù)ListView的item位置與上一條數(shù)據(jù)進(jìn)行比較,來控制時間的顯示隱藏效果。思路很簡單,下面看代碼實(shí)現(xiàn):
首先是頁面的整體布局,很簡單,就一個ListView:
res/layout/activity_main.xml - <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas./apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/lv_list"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:cacheColorHint="@null"
- android:divider="@null" >
- </ListView>
- </LinearLayout>
復(fù)制代碼
ListView的item的布局分了三部分,一個需要顯示隱藏的標(biāo)題欄,包括一個小圓點(diǎn)和一個顯示時間的TextView,第二部分是展示的內(nèi)容,最后是時間軸的豎線。 res/layout/item_time_line.xml - <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas./apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <RelativeLayout
- android:id="@+id/rl_title"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingTop="10dp" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="8dp"
- android:background="@drawable/img_line_point" />
- <TextView
- android:id="@+id/txt_date_time"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="15dp"
- android:textColor="#FC6802" />
- </RelativeLayout>
- <TextView
- android:id="@+id/txt_date_content"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/rl_title"
- android:layout_marginLeft="15dp"
- android:paddingBottom="10dp"
- android:textColor="#5296C5" />
- <View
- android:id="@+id/v_line"
- android:layout_width="2dp"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:background="#FC6802" />
- </RelativeLayout>
復(fù)制代碼
下面看主要的實(shí)現(xiàn)部分,是在listview的適配器中: DateAdapter.java - package com.xiaowu.timeline;
- import java.util.List;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.RelativeLayout;
- import android.widget.RelativeLayout.LayoutParams;
- import android.widget.TextView;
- public class DateAdapter extends BaseAdapter {
- private Context context;
- private List<DateText> list;
- public DateAdapter(Context context, List<DateText> list) {
- this.context = context;
- this.list = list;
- }
- @Override
- public int getCount() {
- if (list == null) {
- return 0;
- }
- return list.size();
- }
- @Override
- public Object getItem(int position) {
- if (list == null) {
- return null;
- }
- return list.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder = new ViewHolder();
- convertView = LayoutInflater.from(context).inflate(
- R.layout.item_time_line, parent, false);
- holder.date = (TextView) convertView
- .findViewById(R.id.txt_date_time);
- holder.content = (TextView) convertView
- .findViewById(R.id.txt_date_content);
- holder.line = (View) convertView.findViewById(R.id.v_line);
- holder.title = (RelativeLayout) convertView
- .findViewById(R.id.rl_title);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- //時間軸豎線的layout
- LayoutParams params = (LayoutParams) holder.line.getLayoutParams();
- //第一條數(shù)據(jù),肯定顯示時間標(biāo)題
- if (position == 0) {
- holder.title.setVisibility(View.VISIBLE);
- holder.date.setText(TimeFormat.format("yyyy.MM.dd",
- list.get(position).getDate()));
- params.addRule(RelativeLayout.ALIGN_TOP, R.id.rl_title);
- params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.txt_date_content);
- } else { // 不是第一條數(shù)據(jù)
- // 本條數(shù)據(jù)和上一條數(shù)據(jù)的時間戳相同,時間標(biāo)題不顯示
- if (list.get(position).getDate()
- .equals(list.get(position - 1).getDate())) {
- holder.title.setVisibility(View.GONE);
- params.addRule(RelativeLayout.ALIGN_TOP, R.id.txt_date_content);
- params.addRule(RelativeLayout.ALIGN_BOTTOM,
- R.id.txt_date_content);
- } else {
- //本條數(shù)據(jù)和上一條的數(shù)據(jù)的時間戳不同的時候,顯示數(shù)據(jù)
- holder.title.setVisibility(View.VISIBLE);
- holder.date.setText(TimeFormat.format("yyyy.MM.dd",
- list.get(position).getDate()));
- params.addRule(RelativeLayout.ALIGN_TOP, R.id.rl_title);
- params.addRule(RelativeLayout.ALIGN_BOTTOM,
- R.id.txt_date_content);
- }
- }
- holder.line.setLayoutParams(params);
- holder.content.setText(list.get(position).getText());
- return convertView;
- }
- public static class ViewHolder {
- RelativeLayout title;
- View line;
- TextView date;
- TextView content;
- }
- }
復(fù)制代碼 其中DateText是實(shí)體類,包括標(biāo)題和內(nèi)容:
DateText.java - package com.xiaowu.timeline;
- public class DateText {
- private String date;
- private String text;
- public DateText() {
- }
- public DateText(String date, String text) {
- super();
- this.date = date;
- this.text = text;
- }
- public String getDate() {
- return date;
- }
- public void setDate(String date) {
- this.date = date;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- }
復(fù)制代碼adapter里面用到了一個時間戳轉(zhuǎn)為指定格式的工具類: TimeFormat.java - package com.xiaowu.timeline;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class TimeFormat {
- public static String format(String format, String time) {
- String result = "";
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
- try {
- Date date = df.parse(time);
- SimpleDateFormat df1 = new SimpleDateFormat(format);
- result = df1.format(date);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return result;
- }
- }
復(fù)制代碼 場景類中還用到一個比較時間戳的工具類:
DateComparator.java - package com.xiaowu.timeline;
- import java.util.Comparator;
- public class DateComparator implements Comparator<DateText> {
- @Override
- public int compare(DateText lhs, DateText rhs) {
- return rhs.getDate().compareTo(lhs.getDate());
- }
- }
復(fù)制代碼 下面看下場景類:
MainActivity.java - package com.xiaowu.timeline;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- public class MainActivity extends Activity {
- // 時間軸列表
- private ListView lvList;
- // 數(shù)據(jù)list
- private List<DateText> list;
- // 列表適配器
- private DateAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // findviewbyid
- lvList = (ListView) findViewById(R.id.lv_list);
- list = new ArrayList<DateText>();
- // 添加測試數(shù)據(jù)
- addData();
- // 將數(shù)據(jù)按照時間排序
- DateComparator comparator = new DateComparator();
- Collections.sort(list, comparator);
- // listview綁定適配器
- adapter = new DateAdapter(this, list);
- lvList.setAdapter(adapter);
- }
- private void addData() {
- DateText date1 = new DateText("20140710", "撒大聲地");
- DateText date2 = new DateText("20140709", "撒薩達(dá)");
- DateText date3 = new DateText("20140726", "撒大ADS");
- DateText date4 = new DateText("20140710", "撒達(dá)到對薩達(dá)撒地");
- DateText date5 = new DateText("20140711", "撒大阿瑟的薩達(dá)地");
- DateText date6 = new DateText("20140713", "撒撒大大地");
- DateText date7 = new DateText("20140712", "撒大鼎折覆餗地");
- DateText date8 = new DateText("20140714", "撒大請問阿爾阿斯頓");
- DateText date9 = new DateText("20140709", "撒大親愛額問問喬恩地");
- DateText date10 = new DateText("20140705", "撒 請問請問地");
- DateText date11 = new DateText("20140729", "撒請問額外確認(rèn)聲地");
- DateText date12 = new DateText("20140725", "撒大按時打算");
- DateText date13 = new DateText("20140716", "撒大愛上大聲地");
- DateText date14 = new DateText("20140711", "撒其味無窮地");
- DateText date15 = new DateText("20140710", "撒大請問我期待地");
- DateText date16 = new DateText("20140711", "撒大聲薩達(dá)");
- DateText date17 = new DateText("20140712", "阿斯達(dá)");
- DateText date18 = new DateText("20140711", "撒大聲大聲道");
- DateText date19 = new DateText("20140715", "阿斯頓撒打算23 ");
- DateText date20 = new DateText("20140723", "范德薩發(fā)生");
- DateText date21 = new DateText("20140718", "阿薩德飛灑");
- DateText date22 = new DateText("20140706", "撒打算打算");
- DateText date23 = new DateText("20140714", "撒打算");
- DateText date24 = new DateText("20140726", "輕微的城市的方式");
- DateText date25 = new DateText("20140725", "阿斯達(dá)阿斯達(dá)現(xiàn)在");
- DateText date26 = new DateText("20140723", "代購費(fèi)多少自行車");
- DateText date27 = new DateText("20140721", "多撒阿薩德時打算");
- DateText date28 = new DateText("20140716", "愛上大聲地啊地");
- DateText date29 = new DateText("20140712", "阿斯蒂芬當(dāng)我?guī)煾?);
- DateText date30 = new DateText("20140710", "撒大聲大聲道");
- list.add(date1);
- list.add(date2);
- list.add(date3);
- list.add(date4);
- list.add(date5);
- list.add(date6);
- list.add(date7);
- list.add(date8);
- list.add(date9);
- list.add(date10);
- list.add(date11);
- list.add(date12);
- list.add(date13);
- list.add(date14);
- list.add(date15);
- list.add(date16);
- list.add(date17);
- list.add(date18);
- list.add(date19);
- list.add(date20);
- list.add(date21);
- list.add(date22);
- list.add(date23);
- list.add(date24);
- list.add(date25);
- list.add(date26);
- list.add(date27);
- list.add(date28);
- list.add(date29);
- list.add(date30);
- }
- }
復(fù)制代碼 附上代碼:
|