您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Kotlin中實(shí)現(xiàn)一個StepView方法,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
TimeLineStepView
支持時間軸和StepView,三種布局,支持水平布局,垂直布局和自定義布局
添加依賴
implementation 'com.joketng:TimeLineStepView:1.0.1'
使用方法
在布局文件中添加TimeLineStepView
<com.joketng.timelinestepview.view.TimeLineStepView android:id="@+id/rvVertical" android:layout_width="match_parent" android:layout_height="wrap_content" app:lineWidth="3dp" app:markSize="10dp" android:paddingStart="20dp" app:markStart="@drawable/shape_circle_orange" app:layoutType="right" />
在代碼中調(diào)用
//OrientationShowType對應(yīng)三種布局方式 //OrientationShowType.TIMELINE(時間軸方式) //OrientationShowType.CENTER_VERTICAL(垂直方式) //OrientationShowType.CENTER_HORIZONTAL(水平方式,支持左右滑動) rvVertical.initData(listContent, OrientationShowType.CENTER_VERTICAL, object : TimeLineStepView.OnInitDataCallBack{ override fun onBindDataViewHolder(holder: TimeLineStepAdapter.CustomViewHolder, position: Int) { } override fun createCustomView(leftLayout: ViewGroup, rightLayout: ViewGroup, holder: TimeLineStepAdapter.CustomViewHolder) { //LayoutInflater.from(context).inflate(R.layout.item_add_left_view, leftLayout, true) //LayoutInflater.from(context).inflate(R.layout.item_add_right_view, rightLayout, true) } }) .setLayoutType(type)//設(shè)置布局顯示的樣式左邊:LayoutType.LEFT,右邊:LayoutType.RIGHT,左右:LayoutType.ALL //設(shè)置stepview進(jìn)度激活的mark圖標(biāo) .setMarkActive(ContextCompat.getDrawable(context,R.drawable.shape_dot_orange)!!) //設(shè)置stepview進(jìn)度沒激活的mark圖標(biāo) .setMarkInActive(ContextCompat.getDrawable(context,R.drawable.shape_dot_gray)!!) //設(shè)置stepview當(dāng)前進(jìn)度點(diǎn)的mark圖標(biāo) .setMarkCurrent(ContextCompat.getDrawable(context,R.drawable.shape_current)!!) //設(shè)置stepview第一個mark的圖標(biāo) .setMarkStart(ContextCompat.getDrawable(context,R.drawable.shape_circle_orange)!!) //設(shè)置stepview最后一個mark的圖標(biāo) .setMarkEnd(ContextCompat.getDrawable(context,R.drawable.shape_circle_orange)!!) //設(shè)置stepview線的寬度 .setLineWidth(context.dipc(2)) //設(shè)置stepview進(jìn)度激活時線的顏色 .setLineActiveColor(ContextCompat.getColor(context,R.color.c_main_orange)) //設(shè)置stepview進(jìn)度沒有激活時線的顏色 .setLineInActiveColor(ContextCompat.getColor(context,R.color.c_main_gray)) //設(shè)置是否需要自定義布局(此時將createCustomView中的注釋打開將自定義布局傳入) .setIsCustom(true)
listContent的取值為 mutableListOf(),當(dāng)存在自定義布局的時候,listContent中添加的實(shí)體需要繼承BaseBean這個實(shí)體,如果不需要自定義布局,可以直接添加實(shí)體BaseBean
listContent.add(BaseBean(leftTitle = "11-11", leftTime = "08:30", rightTitle = "訂單提交成功", rightTime = "訂單提交成功描述", timeLineState = TimeLineState.ACTIVE)) listContent.add(BaseBean(leftTitle = "11-11", leftTime = "08:31", rightTitle = "訂單付款成功", rightTime = "訂單付款成功描述", timeLineState = TimeLineState.ACTIVE)) listContent.add(BaseBean(leftTitle = "11-11", leftTime = "10:00", rightTitle = "倉庫已經(jīng)接單", rightTime = "倉庫已經(jīng)接單描述", timeLineState = TimeLineState.ACTIVE)) listContent.add(BaseBean(leftTitle = "11-11", leftTime = "10:30", rightTitle = "倉庫處理中", rightTime = "倉庫處理中描述", timeLineState = TimeLineState.ACTIVE)) listContent.add(BaseBean(leftTitle = "11-11", leftTime = "11:00", rightTitle = "已出庫", rightTime = "已出庫描述", timeLineState = TimeLineState.ACTIVE)) listContent.add(BaseBean(leftTitle = "11-11", leftTime = "11:30", rightTitle = "已發(fā)貨", rightTime = "已發(fā)貨描述", timeLineState = TimeLineState.CURRENT)) listContent.add(BaseBean(leftTitle = "11-11", leftTime = "16:00", rightTitle = "已攬件", rightTime = "已攬件描述", timeLineState = TimeLineState.INACTIVE)) listContent.add(BaseBean(leftTitle = "11-11", leftTime = "16:30", rightTitle = "運(yùn)輸中", rightTime = "運(yùn)輸中描述", timeLineState = TimeLineState.INACTIVE))
BaseBean的五個參數(shù)前四個為控件的文本,前四個參數(shù)不傳的話該控件就不會顯示,最后一個TimeLineState對應(yīng)進(jìn)度的三種狀態(tài)TimeLineState.ACTIVE,TimeLineState.INACTIVE,TimeLineState.CURRENT,根據(jù)狀態(tài)在onBindDataViewHolder方法中設(shè)置markdrawable,linecolor等,在設(shè)置markSize的時候,如果大小超過30dp,需要在createCustomView方法或者onBindDataViewHolder方法中調(diào)用holder.llLine.layoutParams.width設(shè)置為大于等于markSize的大小或者設(shè)置為WrapContent,如下
holder.llLine.layoutParams.width = context.dip(35) holder.llLine.layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT
對于布局的顯示位置有要求的話可以在createCustomView方法中通過layoutParams來控制
val rightLayoutParams = rightLayout.layoutParams as LinearLayout.LayoutParams rightLayoutParams.rightMargin = context.dip(30)
如果不喜歡在代碼中設(shè)置控件屬性的話可以選擇布局文件中增加屬性
<com.joketng.timelinestepview.view.TimeLineStepView android:id="@+id/rvVertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingStart="20dp" app:markSize="10dp" app:markStart="@drawable/shape_circle_orange" app:markEnd="@drawable/shape_circle_orange" app:markActive="@drawable/shape_dot_orange" app:markInActive="@drawable/shape_dot_gray" app:markCurrent="@drawable/shape_circle_orange" app:lineWidth="3dp" app:lineActiveColor="@color/c_main_orange" app:lineInActiveColor="@color/c_main_gray" app:isCustom="false" app:layoutType="right" />
如果需要可以在onBindDataViewHolder方法中通過holder獲取控件改變控件的樣式,如果想要添加自定義的UI,可以在createCustomView方法中添加自己定義的布局文件,此時調(diào)用setIsCustom(true)即可
rvVertical.initData(listContent, OrientationShowType.CENTER_VERTICAL, object : TimeLineStepView.OnInitDataCallBack{ override fun onBindDataViewHolder(holder: TimeLineStepAdapter.CustomViewHolder, position: Int) { holder.tvRightTitle.setTextColor(ContextCompat.getColor(context, R.color.c_main_black)) holder.tvLeftTitle.setTextColor(ContextCompat.getColor(context, R.color.c_main_black)) holder.tvRightTime.textSize = 12f holder.tvLeftTime.textSize = 12f holder.tvRightTime.setTextColor(ContextCompat.getColor(context, R.color.c_main_gray)) holder.tvLeftTime.setTextColor(ContextCompat.getColor(context, R.color.c_main_gray)) } override fun createCustomView(leftLayout: ViewGroup, rightLayout: ViewGroup, holder: TimeLineStepAdapter.CustomViewHolder) { LayoutInflater.from(context).inflate(布局id, leftLayout, true)//添加左邊自定義布局 LayoutInflater.from(context).inflate(布局id, rightLayout, true)//添加右邊自定義布局 } }).setLayoutType(type).setIsCustom(true)
自定義布局的一個截圖如下
傳送門Github Demo
使用Maven
<dependency> <groupId>com.joketng</groupId> <artifactId>TimeLineStepView</artifactId> <version>1.0.1</version> <type>pom</type> </dependency>
上述內(nèi)容就是怎么在Kotlin中實(shí)現(xiàn)一個StepView方法,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。