Android recyclerview实现纵向虚线时间轴的示例代码
作者:WXY_126 发布时间:2023-08-23 07:03:39
标签:Android,recyclerview,纵向虚线时间轴
效果图
代码
package com.jh.timelinedemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* @Description: Android自定义虚线
* @Date 2019-07-20 10:07
* @Version
*/
public class DividerView extends View {
static public int ORIENTATION_HORIZONTAL = 0;
static public int ORIENTATION_VERTICAL = 1;
private Paint mPaint;
private int orientation;
public DividerView(Context context) {
this(context, null);
}
public DividerView(Context context, AttributeSet attrs) {
super(context, attrs);
int dashGap, dashLength, dashThickness;
int color;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DividerView, 0, 0);
try {
dashGap = a.getDimensionPixelSize(R.styleable.DividerView_dashGap, 5);
dashLength = a.getDimensionPixelSize(R.styleable.DividerView_dashLength, 5);
dashThickness = a.getDimensionPixelSize(R.styleable.DividerView_dashThickness, 3);
color = a.getColor(R.styleable.DividerView_divider_line_color, 0xff000000);
orientation = a.getInt(R.styleable.DividerView_divider_orientation, ORIENTATION_HORIZONTAL);
} finally {
a.recycle();
}
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(dashThickness);
mPaint.setPathEffect(new DashPathEffect(new float[]{dashGap, dashLength,}, 0));
}
public void setBgColor(int color) {
mPaint.setColor(color);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
if (orientation == ORIENTATION_HORIZONTAL) {
float center = getHeight() * 0.5f;
canvas.drawLine(0, center, getWidth(), center, mPaint);
} else {
float center = getWidth() * 0.5f;
canvas.drawLine(center, 0, center, getHeight(), mPaint);
}
}
}
package com.jh.timelinedemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private RecyclerView rcy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rcy = findViewById(R.id.rcy);
LinearLayoutManager manager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
rcy.setLayoutManager(manager);
TimeLineAdapter adapter = new TimeLineAdapter(this);
rcy.setAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp" />
</LinearLayout>
package com.jh.timelinedemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
/**
*
* @date:on 2021/7/21 17:38
*/
public class TimeLineAdapter extends RecyclerView.Adapter<TimeLineAdapter.ViewHolder> {
private Context context;
public TimeLineAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public TimeLineAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, null);
ViewHolder viewHolder = new ViewHolder(inflate);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull TimeLineAdapter.ViewHolder holder, int position) {
holder.line_up.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);//第一条数据隐藏头部线
holder.line_down.setVisibility(position == 4 ? View.INVISIBLE : View.VISIBLE);//最后一条数据隐藏底部线
}
@Override
public int getItemCount() {
return 5;
}
class ViewHolder extends RecyclerView.ViewHolder {
private final DividerView line_up, line_down;
public ViewHolder(@NonNull View itemView) {
super(itemView);
line_up = itemView.findViewById(R.id.line_up);
line_down = itemView.findViewById(R.id.line_down);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:id="@+id/rl_history_root">
<LinearLayout
android:layout_width="10dp"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_marginLeft="12dp"
android:orientation="vertical">
<com.jh.timelinedemo.DividerView
android:id="@+id/line_up"
android:layout_width="1dp"
android:layout_height="7dp"
android:layerType="software"
custom:dashGap="2dp"
custom:dashLength="2dp"
custom:dashThickness="1dp"
custom:divider_line_color="#A3A9BD"
custom:divider_orientation="vertical" />
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:id="@+id/iv_history_rhombus"
android:src="@mipmap/ic_rhombus_green" />
<com.jh.timelinedemo.DividerView
android:id="@+id/line_down"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layerType="software"
custom:dashGap="2dp"
custom:dashLength="2dp"
custom:dashThickness="1dp"
custom:divider_line_color="#A3A9BD"
custom:divider_orientation="vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="19dp"
android:orientation="vertical"
android:paddingBottom="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="标题 标题 标题"
android:textColor="#2f3856"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="6dp"
android:text="内容 内容 "
android:textColor="#2f3856"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<!-- 垂直方向的虚线 -->
<declare-styleable name="DividerView">
<!-- 虚线颜色 -->
<attr name="divider_line_color" format="color"/>
<!-- 虚线宽度 -->
<attr name="dashThickness" format="dimension"/>
<!-- 虚线dash宽度 -->
<attr name="dashLength" format="dimension"/>
<!-- 虚线dash间隔 -->
<attr name="dashGap" format="dimension"/>
<!-- 虚线朝向 -->
<attr name="divider_orientation" format="enum">
<enum name="horizontal" value="0"/>
<enum name="vertical" value="1"/>
</attr>
</declare-styleable>
来源:https://blog.csdn.net/h1047445540/article/details/118973203


猜你喜欢
- 1、设置ssh安装ssh相关软件包:sudo apt-get install openssh-client openssh-server然后
- 本文实例为大家分享了Android实现拍照或者选取本地图片的具体代码,供大家参考,具体内容如下总体流程从selectPhotoActivit
- 本文实例为大家分享了Swing拆分窗格控件JSplitPane的使用方法,供大家参考,具体内容如下package day1115;impor
- 简介Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特
- MyBatis插入Insert、InsertSelective的区别逆向自动生成的mybatis对应配置Mapper文件里面,有两个方法,分
- 本文实例为大家分享了Unity动画混合树实例代码,供大家参考,具体内容如下先看效果游戏动画中的一项常见任务是在两个或更多相似运动之间进行混合
- 一,栈1,概念在我们软件应用 ,栈这种后进先出数据结构的应用是非常普遍的。比如你用浏 览器上网时不管什么浏览器都有 个"后退&qu
- 文章主要涉及到以下几个问题:怎么实现Java的序列化为什么实现了java.io.Serializable接口才能被序列化transient的
- android手机有自带的照相机和图库,我们做的项目中有时用到上传图片到服务器,今天做了一个项目用到这个功能,所以把我的代码记录下来和大家分
- 会话会话:用户打开浏览器进行的一系列操作直至关闭浏览器的过程看作是一次会话HTTP协议是无状态的,不能实现跟踪对话。比如进入一个网站,每次操
- 本文实例为大家分享了java实现文件夹解压和压缩的具体代码,供大家参考,具体内容如下效果实现多个文件以及文件夹的压缩和解压代码分析impor
- 本文实例讲述了Android中TelephonyManager类的方法。分享给大家供大家参考。具体如下:TelephonyManager类主
- 蓝牙设置相关界面,以下是通过C#方式打开的几个方式,记录一下蓝牙设置界面1.控制面板命令bthprops.cpl可以用控制面板 contro
- 兄dei,耐心把我的写的看完,我写的不繁琐,很好理解.IDEA插件之Mybatis Log plugin
- 之前使用springMVC+spring+mybatis,总是被一些繁琐的xml配置,有时候如果配置出错,还要检查各种xml配置,偶然接触到
- 原理简介Java中提供了Calendar这个专门用于对日历进行操作的类,那么这个类有什么特殊的地方呢,首先我们来看Calendar的声明:p
- 本文实例为大家分享了unity通过Mesh网格绘制球体的具体代码,供大家参考,具体内容如下接着上一篇文章说:球体public class 球
- 由于 * 一般都比较难理解,程序设计者会设计一个 * 接口供开发者使用,开发者只要知道 * 接口的方法、含义和作用即可,无须知道 * 是
- 本文实例讲述了C#非矩形窗体实现方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Colle
- 在平时的工作中,估计大多数都做过轮询调度的任务,比如定时轮询数据库同步,定时邮件通知等等。大家通过windows计划任务,windows服务