万能RecyclerView分割线
作者:无心下棋 时间:2023-04-19 16:10:04
就不多叙述了,直接上代码
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
/*
* RecyclerView的布局方向,默认先赋值
* 为纵向布局
* RecyclerView 布局可横向,也可纵向
* 横向和纵向对应的分割想画法不一样
* */
private int mOrientation = LinearLayoutManager.VERTICAL;
/**
* item之间分割线的size,1---5
*/
private int mSize;
/**
* 绘制item分割线的画笔,和设置其属性
* 来绘制个性分割线
*/
private Paint mPaint;
/**
* 构造方法传入布局方向,不可不传
*
* @param context context
* @param orientation 布局方向
* @param color 颜色
* @param mItemSize item之间分割线的size
*/
public DividerItemDecoration(Context context, int orientation, int color, int mItemSize) {
this.mOrientation = orientation;
/*
item之间分割线的颜色
*/
this.mSize= mItemSize;
if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
throw new IllegalArgumentException("LinearLayoutManager error");
}
mSize = (int) TypedValue.applyDimension(mItemSize, TypedValue.COMPLEX_UNIT_DIP, context.getResources().getDisplayMetrics());
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(color);
/*设置填充*/
mPaint.setStyle(Paint.Style.FILL);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == LinearLayoutManager.VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
/**
* 绘制纵向 item 分割线
*
* @param canvas canvas
* @param parent parent
*/
private void drawVertical(Canvas canvas, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + layoutParams.bottomMargin;
final int bottom = top + mSize;
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
/**
* 绘制横向 item 分割线
*
* @param canvas canvas
* @param parent parent
*/
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + layoutParams.rightMargin;
final int right = left + mSize;
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
/**
* 设置item分割线的size
*
* @param outRect outRect
* @param view view
* @param parent parent
* @param state state
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == LinearLayoutManager.VERTICAL) {
outRect.set(0, 0, 0, mSize);
} else {
outRect.set(0, 0, mSize, 0);
}
}
}
调用的时候这样写:
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL, Color.RED,5));
标签:RecyclerView,分割线
0
投稿
猜你喜欢
SpringMVC基于配置的异常处理器
2023-01-19 15:34:33
JavaSE static final及abstract修饰符实例解析
2021-07-01 10:54:54
SpringBoot入坑笔记之spring-boot-starter-web 配置文件的使用
2021-12-15 13:11:46
Android实现点击缩略图放大效果
2022-11-10 21:47:23
Android Studio多渠道打包的配置方法
2023-06-15 23:19:48
浅析C# 状态机Stateless
2022-03-08 16:39:07
基于C#实现端口扫描器(单线程和多线程)
2021-10-16 20:32:23
C# websocket及时通信协议的实现方法示例
2023-11-16 05:14:48
SpringBoot SSO轻松实现(附demo)
2022-04-05 02:24:33
Unity使用ScrollRect制作翻页
2023-05-22 09:10:44
unity实现贪吃蛇游戏
2022-05-28 07:07:51
Android DialogUtils弹出窗工具类详解
2022-05-13 17:34:56
c# 使用线程对串口serialPort进行收发数据(四种)
2023-06-20 13:05:48
如何随机选取一个机器的ip
2022-03-01 12:20:43
Java比较问题详细分析
2023-11-20 14:30:48
详解Java利用深度优先遍历解决迷宫问题
2022-08-20 02:46:54
Java必备知识之位运算及常见进制解读
2022-02-10 00:42:19
Android 仿今日头条简单的刷新效果实例代码
2022-06-16 12:19:18
Android自定义View实现波浪动画
2022-05-27 23:04:46
23种设计模式(19)java责任链模式
2021-10-19 15:04:00