Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 ListView 做時間軸:快遞物流軌跡頁的完整實作

編輯:關於 Android 程式設計

實現思路

整個畫面以 ListView 為骨架,每個 ListItem 由兩個欄位構成:內容(content)與時間(time)。

時間軸的視覺部分並不是一張圖片,而是用零件拼出來的:上方一段 20dp 的線、中間一個 15dp 的圓點、下方一段 40dp 的線,三段垂直堆疊即可。只要每一列的間距一致,串起來看就是一條連續的時間軸。

另外,ListView 需要關掉分割線,並取消點擊回饋效果。

完成後的效果:

步驟一:用 XML 畫一個灰色圓點(time_cycle.xml)

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#CBCBCB" /> <size android:width="15dp" android:height="15dp" /> </shape> 

步驟二:建立資料模型

 public class KuaiDi { private String content; private String time; public KuaiDi(String time, String content) { this.content = content; this.time = time; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }

步驟三:編寫列表項佈局(time_item.xml)

左半部負責畫軸線與圓點,右半部放文字。整列高度固定 75dp,讓上下兩段線條剛好能與相鄰列接上。

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal"> <!--線條部分--> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingLeft="30dp"> <View android:layout_width="3dp" android:layout_height="20dp" android:background="#CBCBCB" /> <ImageView android:layout_width="15dp" android:layout_height="15dp" android:background="@drawable/time_cycle" /> <View android:layout_width="3dp" android:layout_height="40dp" android:background="#CBCBCB" /> </LinearLayout> <!--文字部分--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="20dp"> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="【廣東省中國郵政集團公司深圳市龍華函件中心】已收寄" android:textColor="#ABABAB" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_content" android:text="2016-05-03 00:22:36" android:textColor="#ABABAB" /> </LinearLayout> </LinearLayout> 

單列的效果如下:

步驟四:編寫父佈局(activity_main.xml)

divider 設為 @null 去掉分割線,listSelector 設為透明色取消點擊高亮。

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="@null" android:listSelector="@android:color/transparent" /> </RelativeLayout> 

步驟五:編寫 Adapter(KuaiDiAdapter.java)

這裡用 ViewHolder 模式把 findViewById 的結果快取下來,避免每次 getView 都重複查找,是列表順滑的關鍵之一。

 public class KuaiDiAdapter extends BaseAdapter { //印章數據 private List<KuaiDi> list; private LayoutInflater mInflater; public KuaiDiAdapter(Context context, List<KuaiDi> list) { this.list = list; mInflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.time_item, null); } ViewHolder holder = getViewHolder(convertView); KuaiDi kd = list.get(position); holder.tv_content.setText(kd.getContent()); holder.tv_time.setText(kd.getTime()); return convertView; } /** * 獲得控件管理對象 * * @param view * @return */ private ViewHolder getViewHolder(View view) { ViewHolder holder = (ViewHolder) view.getTag(); if (holder == null) { holder = new ViewHolder(view); view.setTag(holder); } return holder; } /** * 控件管理類 */ private class ViewHolder { private TextView tv_content, tv_time; ViewHolder(View view) { tv_content = (TextView) view.findViewById(R.id.tv_content); tv_time = (TextView) view.findViewById(R.id.tv_time); } } }

步驟六:在 Activity 中掛上 Adapter

 public class MainActivity extends AppCompatActivity { private ListView lv; private KuaiDiAdapter adapter; private List<KuaiDi> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); list =new ArrayList<>(); list.add(new KuaiDi("2016-09-18 08:33:50","您的訂單開始處理")); list.add(new KuaiDi("2016-09-18 08:40:23","您的訂單待配貨")); list.add(new KuaiDi("2016-09-18 08:51:33","您的包裹已出庫")); list.add(new KuaiDi("2016-09-18 21:12:53","【深圳市龍華函件中心】已收寄")); list.add(new KuaiDi("2016-09-18 17:44:20","到達【深圳】")); list.add(new KuaiDi("2016-09-18 21:26:51","離開【深圳市龍華函件中心】,下一站【深圳市】")); list.add(new KuaiDi("2016-09-18 23:18:21","到達【深圳市處理中心】")); list.add(new KuaiDi("2016-09-19 01:14:30","離開【深圳市處理中心】,下一站【廣州市】")); list.add(new KuaiDi("2016-09-19 04:42:11","到達【廣東省廣州郵件處理中心】")); adapter = new KuaiDiAdapter(this,list); lv.setAdapter(adapter); } }

幾個可以進一步打磨的地方

  • 首項與末項的軸線需要特殊處理:第一筆不該有上半段線,最後一筆不該有下半段線。可在 Adapter 中依 position 控制兩個 View 的顯示與否。
  • 若想讓最新狀態視覺上更突出,可為第一筆的圓點改用不同顏色的 shape,並加上文字顏色差異。
  • 資料量很大時,考慮換成 RecyclerView 並搭配 DiffUtil,滾動效能與資源回收都會更好。
  • 把顏色值抽進 colors.xml、尺寸抽進 dimens.xml,後續換主題或改版會輕memberName許多。

以上就是用 ListView 實作時間軸效果的完整流程,希望對你有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved