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

标签:Android,recyclerview,纵向虚线时间轴
0
投稿

猜你喜欢

  • Android编程实现ListView滚动提示等待框功能示例

    2022-11-15 13:50:15
  • Spring Security 中细化权限粒度的方法

    2022-08-30 14:26:40
  • Android Studio提示inotify大小不足的解决办法

    2022-01-06 23:48:07
  • 使用Android Studio创建OpenCV4.1.0 项目的步骤

    2022-05-03 23:16:42
  • Java8深入学习之熟透Optional

    2023-08-24 21:27:54
  • C#之Android手机App开发

    2023-10-19 05:18:04
  • C语言形参和实参的区别详解

    2023-08-27 19:41:53
  • 基于SpringMVC入门案例及讲解

    2023-04-06 17:34:43
  • java 类加载机制和反射详解及实例代码

    2023-11-30 06:42:20
  • mybatis根据表逆向自动化生成代码的实现

    2023-03-12 15:39:02
  • 基于WPF实现控件轮廓跑马灯动画效果

    2022-05-03 20:10:08
  • Java线程休眠的5种方法

    2022-02-21 04:49:48
  • 详解三种C#实现数组反转方式

    2023-10-09 20:29:23
  • Android开发之关于项目

    2023-05-08 23:38:24
  • C#数据结构之队列(Quene)实例详解

    2021-12-03 09:06:26
  • C#微信开发之微信公众号标签管理功能

    2023-04-17 20:20:44
  • Android 实现左滑出现删除选项

    2021-05-28 12:05:59
  • Java中的Struts2框架拦截 器之实例代码

    2023-06-21 19:04:03
  • 基于Unity制作一个简易的计算器

    2023-02-18 10:02:39
  • C#多线程系列之任务基础(一)

    2022-12-09 04:48:22
  • asp之家 软件编程 m.aspxhome.com