Android recyclerview實(shí)現(xiàn)縱向虛線(xiàn)時(shí)間軸的示例代碼
效果圖

代碼
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自定義虛線(xiàn)
* @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);//第一條數(shù)據(jù)隱藏頭部線(xiàn)
holder.line_down.setVisibility(position == 4 ? View.INVISIBLE : View.VISIBLE);//最后一條數(shù)據(jù)隱藏底部線(xiàn)
}
@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="標(biāo)題 標(biāo)題 標(biāo)題"
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="內(nèi)容 內(nèi)容 "
android:textColor="#2f3856"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<!-- 垂直方向的虛線(xiàn) -->
<declare-styleable name="DividerView">
<!-- 虛線(xiàn)顏色 -->
<attr name="divider_line_color" format="color"/>
<!-- 虛線(xiàn)寬度 -->
<attr name="dashThickness" format="dimension"/>
<!-- 虛線(xiàn)dash寬度 -->
<attr name="dashLength" format="dimension"/>
<!-- 虛線(xiàn)dash間隔 -->
<attr name="dashGap" format="dimension"/>
<!-- 虛線(xiàn)朝向 -->
<attr name="divider_orientation" format="enum">
<enum name="horizontal" value="0"/>
<enum name="vertical" value="1"/>
</attr>
</declare-styleable>
到此這篇關(guān)于Android recyclerview實(shí)現(xiàn)縱向虛線(xiàn)時(shí)間軸的示例代碼的文章就介紹到這了,更多相關(guān)Android recyclerview縱向虛線(xiàn)時(shí)間軸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(2)
- Android自定義控件實(shí)現(xiàn)時(shí)間軸
- Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果
- Android自定義view仿淘寶快遞物流信息時(shí)間軸
- Android實(shí)現(xiàn)快遞物流時(shí)間軸效果
- Android實(shí)現(xiàn)列表時(shí)間軸
- Android自定義指示器時(shí)間軸效果實(shí)例代碼詳解
- 教你3分鐘了解Android 簡(jiǎn)易時(shí)間軸的實(shí)現(xiàn)方法
- Android自定義View實(shí)現(xiàn)垂直時(shí)間軸布局
- android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(1)
相關(guān)文章
Android 中StringBuffer 和StringBuilder常用方法
這篇文章主要介紹了Android 中StringBuffer 和StringBuilder的常用方法及區(qū)別介紹,需要的朋友可以參考下2017-02-02
Android使用CountDownTimer類(lèi)實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘
這篇文章主要為大家詳細(xì)介紹了Android使用CountDownTimer類(lèi)實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android GridView實(shí)現(xiàn)橫向列表水平滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android GridView實(shí)現(xiàn)橫向列表水平滾動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android ApplicationContext接口深入分析
ApplicationContext是Spring應(yīng)用程序中的中央接口,由于繼承了多個(gè)組件,使得ApplicationContext擁有了許多Spring的核心功能,如獲取bean組件,注冊(cè)監(jiān)聽(tīng)事件,加載資源文件等2022-11-11
Android?妙用TextView實(shí)現(xiàn)左邊文字,右邊圖片
這篇文章主要介紹了Android?妙用TextView實(shí)現(xiàn)左邊文字,右邊圖片的相關(guān)資料,需要的朋友可以參考下2023-07-07
Android獲取屏幕方向及鍵盤(pán)狀態(tài)的小例子
很多開(kāi)發(fā)Android的網(wǎng)友可能需要判斷當(dāng)前的屏幕方向或鍵盤(pán)狀態(tài),下面的代碼可以判斷出橫屏landscape和常規(guī)的portrait縱握方式,如果使用的是G1這樣有QWERTY鍵盤(pán)硬件的,還可以判斷屏幕方向以及鍵盤(pán)的拉出狀態(tài)。2013-05-05
輕松實(shí)現(xiàn)Android語(yǔ)音識(shí)別功能
這篇文章主要為初學(xué)者介紹了輕松實(shí)現(xiàn)Android語(yǔ)音識(shí)別功能的代碼,感興趣的小伙伴們可以參考一下2016-07-07
Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)
這篇文章主要給大家介紹了Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
Android實(shí)現(xiàn)自定義標(biāo)題欄的方法
這篇文章主要介紹了Android實(shí)現(xiàn)自定義標(biāo)題欄的方法,需要的朋友可以參考下2015-12-12

