Android实现列表时间轴
作者:初心不负 时间:2021-10-19 12:55:04
本文实例为大家分享了Android列表时间轴展示的具体代码,供大家参考,具体内容如下
实现的效果图如下:
实现的方式是利用recycleview的ItemDecoration这个抽象类,就是我们经常用来画分割线的的这个类,
具体如下
public class DividerItemDecoration extends RecyclerView.ItemDecoration{
// 写右边字的画笔(具体信息)
private Paint mPaint;
// 写左边日期字的画笔( 时间 + 日期)
private Paint mPaint1;
private Paint mPaint2;
private Paint mPaint3;
// 左 上偏移长度
private int itemView_leftinterval;
private int itemView_topinterval;
// 轴点半径
private int circle_radius;
// 图标
private Bitmap mIcon;
//月份合集(使用时需要设置)
private List<String> monthList= new ArrayList<>();
//年份合集(使用时需要设置)
private List<String> yearList= new ArrayList<>();
public void setMonthList(List<String> monthList) {
this.monthList = monthList;
}
public void setYearList(List<String> yearList) {
this.yearList = yearList;
}
// 在构造函数里进行绘制的初始化,如画笔属性设置等
public DividerItemDecoration(Context context) {
// 轴点画笔(红色)
mPaint = new Paint();
mPaint.setColor(Color.rgb(58, 154, 239));
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(3);
// 左边时间文本画笔(蓝色)
// 此处设置了两只分别设置 时分 & 年月
mPaint1 = new Paint();
mPaint1.setColor(Color.BLACK);
mPaint1.setTextSize(30);
mPaint2 = new Paint();
mPaint2.setColor(context.getResources().getColor(R.color.divider));
mPaint2.setTextSize(26);
mPaint3 = new Paint();
mPaint3.setColor(Color.rgb(58, 154, 239));
mPaint3.setTextSize(20);
// 赋值ItemView的左偏移长
itemView_leftinterval = 150;
// 赋值ItemView的上偏移长度
itemView_topinterval = 30;
// 赋值轴点圆的半径为10
circle_radius = 8;
}
// 重写getItemOffsets()方法
// 作用:设置ItemView 左 & 上偏移长度
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
// 设置ItemView的左 & 上偏移长度分别为150 px & 30px,即此为onDraw()可绘制的区域
outRect.set(itemView_leftinterval, itemView_topinterval, 0, 0);
}
// 重写onDraw()
// 作用:在间隔区域里绘制时光轴线 & 时间文本
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
// 获取RecyclerView的Child view的个数
int childCount = parent.getChildCount();
// 遍历每个Item,分别获取它们的位置信息,然后再绘制对应的分割线
for (int i = 0; i < childCount; i++) {
// 获取每个Item对象
View child = parent.getChildAt(i);
View lastChild = null;
if (i > 0) {
lastChild = parent.getChildAt(i - 1);
}
/**
* 绘制轴点
*/
// 轴点 = 圆 = 圆心(x,y) 位置可以根据需求来调节
float centerx = child.getLeft() - itemView_leftinterval / 4;
float centery = child.getTop() + itemView_topinterval +10;
// 绘制轴点圆
c.drawCircle(centerx, centery, circle_radius, mPaint);
/**
* 绘制上半轴线(x轴是保持不变的)
*/
// 上端点坐标(x,y)
float upLine_up_x = centerx;
float upLine_up_y = 0;
if (i>0){
upLine_up_y = lastChild.getBottom();
}else {
upLine_up_y = centery - itemView_topinterval;
}
// 下端点坐标(x,y)
float upLine_bottom_x = centerx;
float upLine_bottom_y = centery - circle_radius;
//绘制上半部轴线
c.drawLine(upLine_up_x, upLine_up_y, upLine_bottom_x, upLine_bottom_y, mPaint);
/**
* 绘制下半轴线,最后一个不画下半轴
*/
if (i <childCount-1){
// 上端点坐标(x,y)
float bottomLine_up_x = centerx;
float bottom_up_y = centery + circle_radius;
// 下端点坐标(x,y)
float bottomLine_bottom_x = centerx;
float bottomLine_bottom_y = child.getBottom();
//绘制下半部轴线
c.drawLine(bottomLine_up_x, bottom_up_y, bottomLine_bottom_x, bottomLine_bottom_y, mPaint);
}
/**
* 绘制左边时间文本
*/
// 获取每个Item的位置
int index = parent.getChildAdapterPosition(child);
// 设置文本起始坐标
float Text_x = child.getLeft() - itemView_leftinterval * 5 / 6;
float Text_y = upLine_bottom_y;
// 根据Item位置设置时间文本
switch (index) {
case (0):
// 设置日期绘制位置
c.drawText(monthList.get(0), Text_x, Text_y, mPaint1);
c.drawText(yearList.get(0), Text_x + 8, Text_y + 28, mPaint2);
break;
case (1):
// 设置日期绘制位置
c.drawText(monthList.get(1), Text_x, Text_y, mPaint1);
c.drawText(yearList.get(1), Text_x + 8, Text_y + 28, mPaint2);
break;
case (2):
// 设置日期绘制位置
if (TextUtils.isEmpty(yearList.get(2))){
c.drawText(monthList.get(2), Text_x, Text_y, mPaint3);
}else {
c.drawText(monthList.get(2), Text_x, Text_y, mPaint1);
c.drawText(yearList.get(2), Text_x + 8, Text_y + 28, mPaint2);
}
break;
case (3):
// 设置日期绘制位置
c.drawText(monthList.get(3), Text_x, Text_y, mPaint1);
c.drawText(yearList.get(3), Text_x + 8, Text_y + 28, mPaint2);
break;
case (4):
// 设置日期绘制位置
c.drawText(monthList.get(4), Text_x, Text_y, mPaint1);
c.drawText(yearList.get(4), Text_x + 8, Text_y + 28, mPaint2);
break;
default:c.drawText("结束", Text_x, Text_y, mPaint1);
}
}
}
}
使用比较简单:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this);
dividerItemDecoration.setMonthList(monthList);
dividerItemDecoration.setYearList(yearList);
mRecyclerView.addItemDecoration(dividerItemDecoration);
来源:https://blog.csdn.net/sinat_35670989/article/details/79591728
标签:Android,时间轴
0
投稿
猜你喜欢
C语言使用结构体实现简单通讯录
2023-05-30 02:27:27
200行java代码实现2048小游戏
2023-11-29 12:03:57
Spring Boot中的Properties的使用详解
2021-07-02 07:25:22
C#使用InstallerProjects打包桌面应用程序的完整步骤
2023-12-08 14:38:04
使用Swing绘制动态时钟
2023-11-24 07:05:12
C#二维数组与多维数组的具体使用
2023-07-30 23:53:49
springboot配置https访问的方法
2022-12-11 16:17:37
超详细的Intellij IDEA 看源码必备技能
2021-10-27 09:03:16
详解Glide4.0集成及使用注意事项
2021-12-28 00:09:06
详谈Java中的事件监听机制
2022-08-05 00:29:30
Flutter集成高德地图并添加自定义Maker的实践
2022-07-11 21:32:49
Java内存区域管理详解
2023-11-10 23:44:42
30条Java代码编写经验分享
2022-01-09 06:41:42
SQL语句删除和添加外键、主键的方法
2023-04-16 22:18:35
一口气说出Java 6种延时队列的实现方法(面试官也得服)
2022-12-15 16:40:12
Android使用addView动态添加组件的方法
2023-12-03 03:40:25
基于C#实现的木马程序实例详解
2023-02-25 20:05:03
mybatis 查询返回Map<String,Object>类型
2023-11-14 07:06:09
C#中后台post请求常用的两种方式总结
2023-04-21 17:29:40
浅谈java中的重载和重写的区别
2023-03-31 04:45:57