Android自定義指示器時間軸效果實例代碼詳解
指示器時間軸在外賣、購物類的APP里會經常用到,效果大概就像下面這樣,看了網上很多文章,大都是自己繪制,太麻煩,其實通過ListView就可以實現(xiàn)。
在Activity關聯(lián)的布局文件activity_main.xml中放置一個ListView,代碼如下。由于這個列表只是用于展示信息,并不需要用戶去點擊,所以將其clickable屬性置為false;為了消除ListView點擊產生的波紋效果,我們設置其listSelector屬性的值為透明;我們不需要列表項之間的分割線,所以設置其divider屬性的值為null。
activity_main
<ListView android:id="@+id/lvTrace" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="false" android:divider="@null" android:dividerHeight="0dp" android:listSelector="@android:color/transparent" />
每個列表項的布局stepview_adapter.xml,代碼如下。由于時間軸的點和線都位于item布局中,為了使線是連續(xù)的,所以設置上面ListView的dividerHeight屬性值為0dp,即垂直方向每個列表項都是緊挨著的。在item的布局中,我們先使用LinearLayout將布局分成左右兩個部分,左邊就是時間軸的布局,右邊是內容的布局。
內容的布局,物流信息是一個RelativeLayout,為了不使兩個列表項的文本靠得太近,在RelativeLayout中設置其paddingBottom和paddingTop屬性。
時間軸的布局,時間軸的布局也是一個RelativeLayout,為了使時間軸的圓點和顯示時間的文本對齊,我們需要在圓點之上再放置一條豎線,所以整體的布局就是 線 - 點 - 線。為了讓線可以正好對準圓點的中心,我們讓線和點都水平居中,即android:layout_centerHorizontal="true"
stepview_adapter
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal"> <RelativeLayout android:id="@+id/rlTimeline" android:layout_width="wrap_content" android:layout_marginLeft="15dp" android:layout_height="match_parent"> <TextView android:id="@+id/tvTopLine" android:layout_width="1.2dp" android:layout_height="12dp" android:layout_centerHorizontal="true" android:background="#999" /> <TextView android:id="@+id/tvDot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvTopLine" android:layout_centerHorizontal="true" android:background="@drawable/state_get_huankuan" /> <TextView android:layout_width="1.2dp" android:id="@+id/tvLine" android:layout_height="match_parent" android:layout_below="@id/tvDot" android:layout_centerHorizontal="true" android:background="#999" /> </RelativeLayout> <RelativeLayout android:id="@+id/rlCenter" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="6dp" android:paddingRight="10dp" android:layout_marginLeft="20dp" android:paddingTop="12dp"> <TextView android:id="@+id/step_tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="6dp" android:text="10-20 22:22" android:textColor="#cccccc" android:textSize="12sp" /> <TextView android:id="@+id/step_tv_des" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginRight="15dp" android:textStyle="bold" android:layout_toLeftOf="@+id/step_tv_time" android:text="fffffff" /> <TextView android:id="@+id/step_tv_des_below" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/step_tv_des" android:layout_marginTop="5dp" android:text="" android:textColor="#999999" /> </RelativeLayout> </LinearLayout><br><br><br>
定義一個Adapter,代碼如下。由于第一行的物流信息的顯示形式和其他的不一樣,所以要注意第一行的item的時間軸布局中最上面的線不顯示
public class StepViewAdapter extends BaseAdapter { private Context context; private List<StepViewBean> traceList = new ArrayList<>(); private static final int TYPE_FINISH = 101; private static final int TYPE_UNFINISH = 102; private static final int TYPE_ERROR = 103; public StepViewAdapter(Context context, List<StepViewBean> traceList) { this.context = context; this.traceList = traceList; } @Override public int getCount() { return traceList.size(); } @Override public StepViewBean getItem(int position) { return traceList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; final StepViewBean trace = getItem(position); if (convertView != null) { holder = (ViewHolder) convertView.getTag(); } else { holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.stepview_adapter, parent, false); holder.tvTopLine = (TextView) convertView.findViewById(R.id.tvTopLine); holder.tvDot = (TextView) convertView.findViewById(R.id.tvDot); holder.tvLine = (TextView) convertView.findViewById(R.id.tvLine); holder.tvAcceptStation = (TextView) convertView.findViewById(R.id.step_tv_des); holder.tvAcceptTime = (TextView) convertView.findViewById(R.id.step_tv_time); holder.tvAcceptStationBelow = (TextView) convertView.findViewById(R.id.step_tv_des_below); holder.rlTimeline = (RelativeLayout) convertView.findViewById(rlTimeline); convertView.setTag(holder); } if (position == 0) { holder.tvTopLine.setVisibility(View.INVISIBLE); } if (position == traceList.size() - 1) { holder.tvLine.setVisibility(View.GONE); } else { holder.tvLine.setVisibility(View.VISIBLE); } switch (getItemViewType(position)) { case TYPE_FINISH: holder.tvAcceptStation.setTextColor(context.getResources().getColor(R.color.crt_completed)); holder.tvDot.setBackgroundResource(R.drawable.state_get_huankuan); holder.tvLine.setBackgroundColor(context.getResources().getColor(R.color.crt_completed)); holder.tvTopLine.setBackgroundColor(context.getResources().getColor(R.color.crt_completed)); break; case TYPE_UNFINISH: holder.tvAcceptStation.setTextColor(context.getResources().getColor(R.color.crt_uncompleted_text)); holder.tvDot.setBackgroundResource(R.drawable.state_normal_huankuan); holder.tvLine.setBackgroundColor(context.getResources().getColor(R.color.crt_text_hint_color)); break; case TYPE_ERROR: holder.tvTopLine.setVisibility(View.VISIBLE); holder.tvAcceptStation.setTextColor(context.getResources().getColor(R.color.crt_error_text)); holder.tvDot.setBackgroundResource(R.drawable.state_lose_huankuan); break; } holder.tvAcceptTime.setText(trace.getAcceptTime()); holder.tvAcceptStation.setText(trace.getAcceptStation()); if (!TextUtils.isEmpty(trace.getAcceptStation())) { holder.tvAcceptStationBelow.setText(trace.getAcceptStationBelow()); } return convertView; } @Override public int getItemViewType(int id) { if(id==(traceList.size()-2)){ return TYPE_ERROR; } if(id==(traceList.size()-1)){ return TYPE_UNFINISH; } return TYPE_FINISH; } static class ViewHolder { public TextView tvAcceptTime, tvAcceptStation, tvLine, tvAcceptStationBelow; public TextView tvTopLine, tvDot; public RelativeLayout rlTimeline; } }
為了可以看到布局的效果,在Activity中模擬一些假數(shù)據(jù)。需要定義一個實體類Trace,它有兩個屬性,acceptTime和acceptStation,代碼如下:
StepViewBean
public class StepViewBean { /** 時間 */ private String acceptTime; /** 描述 */ private String acceptStation; /** 描述下方*/ private String acceptStationBelow; public String getAcceptStationBelow() { return acceptStationBelow; } public void setAcceptStationBelow(String acceptStationBelow) { this.acceptStationBelow = acceptStationBelow; } public StepViewBean() { } public StepViewBean(String acceptTime, String acceptStation) { this.acceptTime = acceptTime; this.acceptStation = acceptStation; } public StepViewBean(String acceptTime, String acceptStation, String acceptStationBelow) { this.acceptTime = acceptTime; this.acceptStation = acceptStation; this.acceptStationBelow = acceptStationBelow; } public String getAcceptTime() { return acceptTime; } public void setAcceptTime(String acceptTime) { this.acceptTime = acceptTime; } public String getAcceptStation() { return acceptStation; } public void setAcceptStation(String acceptStation) { this.acceptStation = acceptStation; } }
MainActivity
public class MainActivity extends AppCompatActivity { private List<StepViewBean> traceList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lvTrace= (ListView) findViewById(R.id.lvTrace); traceList.add(new StepViewBean("10-20 22: 22", "您的訂單已打印完畢", "招商銀行(9979) 小明\n支付金額 100000")); traceList.add(new StepViewBean("10-20 22:22", "您已提交定單,等待系統(tǒng)確認")); traceList.add(new StepViewBean("10-20 22:24", "您的訂單已揀貨完成")); traceList.add(new StepViewBean("10-20 22:24", "掃描員已經掃描")); traceList.add(new StepViewBean("10-20 22:24", "您的訂單已揀貨完成")); traceList.add(new StepViewBean("10-20 22:24", "感謝你在京東購物,歡迎你下次光臨!")); StepViewAdapter adapter = new StepViewAdapter(this, traceList); lvTrace.setAdapter(adapter); } }
GitHub地址:https://github.com/peiniwan/StepView
總結
以上所述是小編給大家介紹的Android自定義指示器時間軸效果實例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
Android開發(fā)Compose remember原理解析
這篇文章主要為大家介紹了Android開發(fā)Compose remember原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07Android編程實現(xiàn)圖片背景漸變切換與圖層疊加效果
這篇文章主要介紹了Android編程實現(xiàn)圖片背景漸變切換與圖層疊加效果,涉及Android圖形特效的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2017-01-01Android Activity啟動模式之singleTop實例詳解
這篇文章主要介紹了Android Activity啟動模式之singleTop,結合實例形式較為詳細的分析了singleTop模式的功能、使用方法與相關注意事項,需要的朋友可以參考下2016-01-01Android ViewPager中顯示圖片與播放視頻的填坑記錄
這篇文章主要給介紹了關于Android ViewPager中顯示圖片與播放視頻的一些填坑記錄,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-05-05Android M(6.x)使用OkHttp包解析和發(fā)送JSON請求的教程
Android 6.0采用的SPDY支持HTTP上GZIP壓縮的傳輸,這使得OkHttp包的功能能夠進一步被利用,本文我們來總結一下Android M(6.0)使用OkHttp包解析和發(fā)送JSON請求的教程2016-07-07