Android StepView實(shí)現(xiàn)物流進(jìn)度效果
本文實(shí)例為大家分享了Android StepView物流進(jìn)度的具體代碼,供大家參考,具體內(nèi)容如下

之前看了一個(gè)別人寫的物流進(jìn)度的demo,自定義View用的挺好的,但是感覺太麻煩了,就自己寫了一個(gè)簡單的,思路很簡單,上面是效果圖。
思路
思路:主要是進(jìn)行了動(dòng)態(tài)添加,根據(jù)上面的效果展示,創(chuàng)建一個(gè)子布局,如下圖所示(代碼里面的布局圖一個(gè)ImageView一個(gè)View一個(gè)TextView),然后自定義一個(gè)MyVerticalView繼承LinearLayout(注意設(shè)置orientation),在MyVerticalView中根據(jù)數(shù)據(jù)來addview()就可以了
代碼
Model
mode的具體變量是根據(jù)上面item的布局,我們需要知道當(dāng)前的狀態(tài)跟具體過程描述。狀態(tài)分為下面三種情況:
STATE_PROCESSING:正在進(jìn)行中(圖標(biāo)如下)
STATE_COMPLETED:已經(jīng)完成(圖標(biāo)如下)
STATE_DEFAULT:最后默認(rèn)步驟(圖標(biāo)如下)

根據(jù)上面分析需要兩個(gè)變量,currentState是為了根據(jù)狀態(tài)設(shè)置不同圖標(biāo)的
private String description;//當(dāng)前狀態(tài)描述 private String currentState;//當(dāng)前狀態(tài)(上面三個(gè)狀態(tài)中的一個(gè))
完整
public class StepModel {
public static final String STATE_PROCESSING="PROCESSING";//正在進(jìn)行的狀態(tài)
public static final String STATE_COMPLETED="COMPLETED";//已經(jīng)完成的狀態(tài)
public static final String STATE_DEFAULT="DEFAULT";//結(jié)尾的默認(rèn)狀態(tài)
private String description;//當(dāng)前狀態(tài)描述
private String currentState;//當(dāng)前狀態(tài)(上面三個(gè)狀態(tài)中的一個(gè))
public StepModel(String description, String currentState) {
this.description = description;
this.currentState = currentState;
}
public String getCurrentState() {
return currentState;
}
public void setCurrentState(String currentState) {
this.currentState = currentState;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
StepView
public class MyVerticalStepView extends LinearLayout {
private List<StepModel> mDatas = new ArrayList<>();//下面給出了它的set跟get方法
private Context mContext;
public MyVerticalStepView(Context context) {
this(context, null);
}
public MyVerticalStepView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyVerticalStepView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
private void init() {
setOrientation(VERTICAL);
mDatas = getmDatas();//獲取數(shù)據(jù)
for (int i = 0; i < mDatas.size(); i++) {
//獲取布局,注意第二個(gè)參數(shù)一定是ViewGroup,否則margin padding之類的屬性將不能使用
View itemview = LayoutInflater.from(mContext).inflate(R.layout.stepview_item, this, false);
TextView description = (TextView) itemview.findViewById(R.id.description_tv);
View line = itemview.findViewById(R.id.line_v);
ImageView icon = (ImageView) itemview.findViewById(R.id.stepicon_iv);
description.setText(mDatas.get(i).getDescription());
//根據(jù)不同狀態(tài)設(shè)置不同圖標(biāo)
switch (mDatas.get(i).getCurrentState()) {
case StepModel.STATE_COMPLETED:
icon.setImageResource(R.drawable.complted);
break;
case StepModel.STATE_DEFAULT:
//結(jié)尾圖標(biāo)隱藏豎線
line.setVisibility(GONE);
icon.setImageResource(R.drawable.default_icon);
break;
case StepModel.STATE_PROCESSING:
icon.setImageResource(R.drawable.attention);
break;
}
this.addView(itemview);
}
requestLayout();//重新繪制布局
invalidate();//刷新當(dāng)前界面
}
public List<StepModel> getmDatas() {
return mDatas;
}
public void setmDatas(List<StepModel> mDatas) {
this.mDatas = mDatas;
init();
}
}
Activity調(diào)用
public class StepViewDemoActivity extends AppCompatActivity {
private MyVerticalStepView mStepView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stepviewlayout);
mStepView= (MyVerticalStepView) findViewById(R.id.stepview);
init();
}
private void init() {
List<StepModel> datas=new ArrayList<>();
StepModel step1=new StepModel("您已提交訂單,等待系統(tǒng)確認(rèn)",StepModel.STATE_COMPLETED);
StepModel step2=new StepModel("訂單已確認(rèn)并打包,預(yù)計(jì)12月16日送達(dá)",StepModel.STATE_COMPLETED);
StepModel step3=new StepModel("包裹正在路上",StepModel.STATE_COMPLETED);
StepModel step4=new StepModel("包裹正在派送",StepModel.STATE_PROCESSING);
StepModel step5=new StepModel("感謝光臨涂涂女裝(店鋪號(hào)85833577),淘寶店鋪,關(guān)注店鋪更多動(dòng)態(tài)盡在微淘動(dòng)態(tài)!",StepModel.STATE_DEFAULT);
datas.add(step1);
datas.add(step2);
datas.add(step3);
datas.add(step4);
datas.add(step5);
mStepView.setmDatas(datas);
}
}
布局
itemview布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:background="@color/stepviewbg" > <LinearLayout android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:paddingRight="10dp" android:paddingLeft="10dp" > <ImageView android:id="@+id/stepicon_iv" android:layout_width="15dp" android:layout_height="15dp" android:src="@drawable/attention" /> <View android:id="@+id/line_v" android:layout_width="2dp" android:layout_height="30dp" android:background="@color/uncompleted_text_color" android:layout_gravity="center_horizontal" android:visibility="visible" ></View> </LinearLayout> <LinearLayout android:layout_toRightOf="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" android:paddingRight="10dp" android:paddingLeft="10dp" > <TextView android:id="@+id/description_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@color/uncompleted_text_color" android:text="訂單正在派送中"/> </LinearLayout> </RelativeLayout>
stepview布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.demo.demo.networkdemo.stepview.MyVerticalStepView android:id="@+id/stepview" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.demo.demo.networkdemo.stepview.MyVerticalStepView> </LinearLayout>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中ConstraintLayout約束布局的最全詳細(xì)解析
ConstraintLayout是Google在Google?I/O?2016大會(huì)上發(fā)布的一種新的布局容器(ViewGroup),它支持以靈活的方式來放置子控件和調(diào)整子控件的大小,下面這篇文章主要給大家介紹了關(guān)于Android中ConstraintLayout約束布局詳細(xì)解析的相關(guān)資料,需要的朋友可以參考下2022-08-08
Android圖像切換器imageSwitcher的實(shí)例應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android圖像切換器imageSwitcher的實(shí)例應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Android使用Canvas對(duì)象實(shí)現(xiàn)刮刮樂效果
這篇文章主要介紹了Android使用Canvas對(duì)象實(shí)現(xiàn)刮刮樂效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android開發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
Android開發(fā)文檔上專門有一小節(jié)解釋這個(gè)問題。簡單來說,Activity是負(fù)責(zé)與用戶交互的最主要機(jī)制,接下來為您詳細(xì)介紹2012-11-11
詳解Android如何獲取進(jìn)程總數(shù),內(nèi)存與任務(wù)列表
在Android開發(fā)中,有時(shí)我們需要監(jiān)控設(shè)備的性能,比如查看當(dāng)前運(yùn)行的進(jìn)程總數(shù),本文將介紹如何通過Android?API獲取這些信息,希望對(duì)大家有所幫助2025-02-02
Flutter app頁面路由以及路由攔截的實(shí)現(xiàn)
本篇介紹了介紹了Flutter如何使用路由來實(shí)現(xiàn)頁面的跳轉(zhuǎn),從而簡化頁面之間的耦合,并可以實(shí)現(xiàn)路由攔截。2021-06-06
Android ViewPager實(shí)現(xiàn)輪播圖效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)輪播圖效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
kotlin android extensions 插件實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了kotlin android extensions 插件實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
AndroidStudio插件GsonFormat之Json快速轉(zhuǎn)換JavaBean教程
這篇文章主要介紹了AndroidStudio插件GsonFormat之Json快速轉(zhuǎn)換JavaBean教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04

