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)
自定义布局的一个截图如下
传送门github demo
使用maven
<dependency> <groupid>com.joketng</groupid> <artifactid>timelinestepview</artifactid> <version>1.0.1</version> <type>pom</type> </dependency>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。