1. 程式人生 > >Kotlin超簡單實現StepView

Kotlin超簡單實現StepView

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" /> 複製程式碼
  • 在程式碼中呼叫
//OrientationShowType對應三種佈局方式
//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)//設定佈局顯示的樣式左邊:LayoutType.LEFT,右邊:LayoutType.RIGHT,左右:LayoutType.ALL //設定stepview進度啟用的mark圖示 .setMarkActive(ContextCompat.getDrawable(context,R.drawable.shape_dot_orange)!!) //設定stepview進度沒啟用的mark圖示 .setMarkInActive(ContextCompat.getDrawable(context,R.drawable.shape_dot_gray)!!) //設定stepview當前進度點的mark圖示 .setMarkCurrent(ContextCompat.getDrawable(context,R.drawable.shape_current)!!) //設定stepview第一個mark的圖示 .setMarkStart(ContextCompat.getDrawable(context,R.drawable.shape_circle_orange)!!) //設定stepview最後一個mark的圖示 .setMarkEnd(ContextCompat.getDrawable(context,R.drawable.shape_circle_orange)!!) //設定stepview線的寬度 .setLineWidth(context.dipc(2)) //設定stepview進度啟用時線的顏色 .setLineActiveColor(ContextCompat.getColor(context,R.color.c_main_orange)) //設定stepview進度沒有啟用時線的顏色 .setLineInActiveColor(ContextCompat.getColor(context,R.color.c_main_gray)) //設定是否需要自定義佈局(此時將createCustomView中的註釋開啟將自定義佈局傳入) .setIsCustom(true
) 複製程式碼

listContent的取值為 mutableListOf(),當存在自定義佈局的時候,listContent中新增的實體需要繼承BaseBean這個實體,如果不需要自定義佈局,可以直接新增實體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 = "倉庫已經接單", rightTime = "倉庫已經接單描述", 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 = "已發貨", rightTime = "已發貨描述", 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 = "運輸中", rightTime = "運輸中描述", timeLineState = TimeLineState.INACTIVE))
複製程式碼

BaseBean的五個引數前四個為控制元件的文字,前四個引數不傳的話該控制元件就不會顯示,最後一個TimeLineState對應進度的三種狀態TimeLineState.ACTIVE,TimeLineState.INACTIVE,TimeLineState.CURRENT,根據狀態在onBindDataViewHolder方法中設定markdrawable,linecolor等,在設定markSize的時候,如果大小超過30dp,需要在createCustomView方法或者onBindDataViewHolder方法中呼叫holder.llLine.layoutParams.width設定為大於等於markSize的大小或者設定為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)
複製程式碼

如果不喜歡在程式碼中設定控制元件屬性的話可以選擇佈局檔案中增加屬性

   <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方法中新增自己定義的佈局檔案,此時呼叫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)
複製程式碼

自定義佈局的一個截圖如下

使用Maven

<dependency>
  <groupId>com.joketng</groupId>
  <artifactId>TimeLineStepView</artifactId>
  <version>1.0.1</version>
  <type>pom</type>
</dependency>
複製程式碼

聯絡方式

如果有什麼問題,我沒有及時回覆的話,可以加我qq542490039,或者發郵件到[email protected],我看到之後會回覆的。