Android ListView物流获取追踪功能实现

Android ListView物流获取追踪功能实现,第1张

概述ListView控件可使用四种不同视图显示项目。通过此控件,可将项目组成带有或不带有列标头的列,并显示伴随的图标和文本。

ListVIEw 控件可使用四种不同视图显示项目。通过此控件,可将项目组成带有或不带有列标头的列,并显示伴随的图标和文本。

最近在网上看到时间轴的布局效果,尝试按照这个原理,实现物流跟踪的效果,目前已经实现了,效果如下图

@H_419_6@

该效果完全是使用ListVIEw来实现了,下面我们来看一下是如何实现的

(一):布局ListVIEw并编写Item布局

首先需要在布局上面编写ListVIEw:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"tools:context="com.bobo.trace.MainActivity" ><ListVIEw androID:ID="@+ID/lv_trace"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:divIDer="@drawable/trace_divIDer"androID:divIDerHeight="1dp"></ListVIEw></relativeLayout>

然后编写ListVIEw的item布局:

<?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" ><relativeLayout androID:ID="@+ID/rl_trace_item"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"><VIEwandroID:ID="@+ID/v_up_line"androID:layout_wIDth="2.5dp"androID:layout_height="10dp"androID:background="@color/mgrey"androID:layout_marginleft="22dp"></VIEw><ImageVIEw androID:ID="@+ID/iv_state"androID:layout_wIDth="16dp"androID:layout_height="16dp"androID:src="@drawable/circle"androID:layout_margintop="10dp"androID:layout_marginleft="15dp"/><TextVIEw androID:ID="@+ID/tv_trace_info"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:layout_margintop="10dp"androID:layout_toRightOf="@ID/iv_state"androID:layout_marginleft="20dp"androID:text="@string/test_trace_info"androID:textcolor="@androID:color/black"androID:textSize="13sp"/><linearLayout androID:ID="@+ID/ll_trace_phone"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:layout_margintop="3dp"androID:layout_toRightOf="@ID/iv_state"androID:layout_marginleft="20dp"androID:orIEntation="horizontal"androID:layout_below="@ID/tv_trace_info"><TextVIEw androID:ID="@+ID/tv_phone_label"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:text="@string/phone_label"androID:textcolor="@androID:color/black"androID:textSize="13sp"/><TextVIEw androID:ID="@+ID/tv_phone"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_marginleft="5dp"androID:text="@string/test_phone"androID:textcolor="@androID:color/black"androID:textSize="13sp"/></linearLayout><TextVIEw androID:ID="@+ID/tv_trace_time"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:layout_margintop="3dp"androID:layout_toRightOf="@ID/iv_state"androID:layout_marginleft="20dp"androID:text="@string/test_trace_info"androID:textcolor="@androID:color/black"androID:textSize="13sp"androID:layout_below="@ID/ll_trace_phone"/><VIEwandroID:ID="@+ID/v_down_line"androID:layout_wIDth="2.5dp"androID:layout_height="45dp"androID:background="@color/mgrey"androID:layout_below="@ID/iv_state"androID:layout_marginleft="22dp"></VIEw></relativeLayout></relativeLayout>

下面我们来看一下item效果:


在上面的效果图中,我们就可以看出,在这个item布局中,左边是”线-图片-线“的布局,显示一个时间轴,右边显示相应的信息,包括物流信息,联系电话和时间;我们知道,在时间轴中,第一个点是不需要上面那个线的,最后一个点是不需要下面那个线的,所以,这个的处理就需要我们在Adapter中进行相应的处理。

(二):自定义Adapter

下面我们就需要自定义Adapter来填充数据和进行VIEw处理。

当然,在编写Adapter之前,我们需要一个javabean来保存相应的信息。

Trace.java:

package com.bobo.beans;public class Trace {private boolean ishead;private String info;private String phone;private String time;public Trace(boolean ishead,String info,String phone,String time) {super();this.ishead = ishead;this.info = info;this.phone = phone;this.time = time;}public boolean ishead() {return ishead;}public voID sethead(boolean ishead) {this.ishead = ishead;}public String getInfo() {return info;}public voID setInfo(String info) {this.info = info;}public String getPhone() {return phone;}public voID setPhone(String phone) {this.phone = phone;}public String getTime() {return time;}public voID setTime(String time) {this.time = time;}}

下面我们就可以愉快的编写Adapter类了:

package com.bobo.adapters;import java.util.ArrayList;import androID.content.Context;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.TextVIEw;import com.bobo.beans.Trace;import com.bobo.trace.R;public class TraceAdapter extends BaseAdapter {private ArrayList<Trace> TradeLists = null;private LayoutInflater inflater;private Context context;public TraceAdapter(ArrayList<Trace> TradeLists,Context context){this.TradeLists = TradeLists;this.context = context;this.inflater = LayoutInflater.from(context);}@OverrIDepublic int getCount() {// Todo auto-generated method stubreturn TradeLists == null ? 0 : TradeLists.size();}@OverrIDepublic Object getItem(int position) {// Todo auto-generated method stubreturn TradeLists.get(position);}@OverrIDepublic long getItemID(int position) {// Todo auto-generated method stubreturn position;}@OverrIDepublic VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {Holder holder;if(convertVIEw == null){convertVIEw = inflater.inflate(R.layout.trace_item,null);holder = new Holder();holder.v_up_line = (VIEw)convertVIEw.findVIEwByID(R.ID.v_up_line);holder.iv_state = (ImageVIEw)convertVIEw.findVIEwByID(R.ID.iv_state);holder.tv_trace_info = (TextVIEw)convertVIEw.findVIEwByID(R.ID.tv_trace_info);holder.ll_trace_phone = (linearLayout)convertVIEw.findVIEwByID(R.ID.ll_trace_phone);holder.tv_phone = (TextVIEw)convertVIEw.findVIEwByID(R.ID.tv_phone);holder.tv_trace_time = (TextVIEw)convertVIEw.findVIEwByID(R.ID.tv_trace_time);holder.v_down_line = (VIEw)convertVIEw.findVIEwByID(R.ID.v_down_line);convertVIEw.setTag(holder);}else{holder = (Holder)convertVIEw.getTag();}if(TradeLists.get(position).ishead()){holder.v_up_line.setVisibility(VIEw.GONE);//holder.iv_state = (ImageVIEw)convertVIEw.findVIEwByID(R.ID.iv_state);holder.tv_trace_info.setText(TradeLists.get(position).getInfo());holder.tv_phone.setText(TradeLists.get(position).getPhone());holder.tv_trace_time.setText(TradeLists.get(position).getTime());holder.v_down_line.setVisibility(VIEw.VISIBLE);}else if(TradeLists.size() == (position+1)){holder.tv_trace_info.setText(TradeLists.get(position).getInfo());holder.ll_trace_phone.setVisibility(VIEw.GONE);holder.tv_trace_time.setText(TradeLists.get(position).getTime());holder.v_down_line.setVisibility(VIEw.GONE);}else{holder.tv_trace_info.setText(TradeLists.get(position).getInfo());holder.ll_trace_phone.setVisibility(VIEw.GONE);holder.tv_trace_time.setText(TradeLists.get(position).getTime());holder.v_down_line.setVisibility(VIEw.VISIBLE);}return convertVIEw;}class Holder{VIEw v_up_line;ImageVIEw iv_state;TextVIEw tv_trace_info;linearLayout ll_trace_phone;TextVIEw tv_phone;TextVIEw tv_trace_time;VIEw v_down_line;}}

这样,我们的Adapter就已经适配完成,下面我们在Activity中实验一下。

(三):Activity实验:

package com.bobo.trace;import java.util.ArrayList;import androID.app.Activity;import androID.content.Context;import androID.os.Bundle;import androID.Widget.ListVIEw;import com.bobo.adapters.TraceAdapter;import com.bobo.beans.Trace;public class MainActivity extends Activity {private ListVIEw lv_trace;private ArrayList<Trace> TradeLists = new ArrayList<Trace>();private TraceAdapter ta;private Context context;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);context = MainActivity.this;initVIEw();}private voID initVIEw(){lv_trace = (ListVIEw)findVIEwByID(R.ID.lv_trace);initData();ta = new TraceAdapter(TradeLists,context);lv_trace.setAdapter(ta);}private voID initData(){TradeLists.add(new Trace(true,"商家已从广东发货","15253157943","2016-03-16 13:30:43"));TradeLists.add(new Trace(false,"货物正在配送","","2016-03-16 18:30:43"));TradeLists.add(new Trace(false,"货物已到达天津转运中心","2016-03-17 13:30:43"));TradeLists.add(new Trace(false,"货品已到济南货运站","2016-03-18 13:30:43"));TradeLists.add(new Trace(false,"货物已送达济南高新区站点","2016-03-19 13:30:43"));}}

这样运行之后,我们就会发现,ListVIEw的selector宽度是占满全屏的,这样,我们就需要编写一个inset来调整ListVIEw的selector。

trace_divIDer.xml:

<?xml version="1.0" enCoding="utf-8"?><inset xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:insetleft="50dp"androID:drawable="@color/mgrey"></inset>

这样,我们的物流追踪界面就算是完成了,很简单。

关于ListVIEw物流获取追踪功能实现就给大家介绍到这里,希望对大家有所帮助!

总结

以上是内存溢出为你收集整理的Android ListView物流获取追踪功能实现全部内容,希望文章能够帮你解决Android ListView物流获取追踪功能实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存