博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 窗口分割线,android RecyclerView 万能分隔线
阅读量:6622 次
发布时间:2019-06-25

本文共 3609 字,大约阅读时间需要 12 分钟。

在使用recyclerview时,经常会需要使用分隔线的功能,现在记录一下

public class RecycleViewDivider extends RecyclerView.ItemDecoration {

private Paint mPaint;

private Drawable mDivider;

private int mDividerHeight = 2;//分割线高度,默认为1px

private int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL

private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

/**

* 默认分割线:高度为2px,颜色为灰色

*

* @param context

* @param orientation 列表方向

*/

public RecycleViewDivider(Context context, int orientation) {

if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {

throw new IllegalArgumentException("请输入正确的参数!");

}

mOrientation = orientation;

final TypedArray a = context.obtainStyledAttributes(ATTRS);

mDivider = a.getDrawable(0);

a.recycle();

}

/**

* 自定义分割线

*

* @param context

* @param orientation 列表方向

* @param drawableId 分割线图片

*/

public RecycleViewDivider(Context context, int orientation, int drawableId) {

//this(context, orientation);

mDivider = ContextCompat.getDrawable(context, drawableId);

mDividerHeight = mDivider.getIntrinsicHeight();

}

/**

* 自定义分割线

*

* @param context

* @param orientation 列表方向

* @param dividerHeight 分割线高度

* @param dividerColor 分割线颜色

*/

public RecycleViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {

//this(context, orientation);

mDividerHeight = dividerHeight;

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mPaint.setColor(dividerColor);

mPaint.setStyle(Paint.Style.FILL);

}

//获取分割线尺寸

@Override

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

super.getItemOffsets(outRect, view, parent, state);

// outRect.set(0, 0, 0, mDividerHeight);

if (mOrientation == LinearLayoutManager.VERTICAL) {

outRect.set(0, 0, 0, mDividerHeight);

} else {

outRect.set(0, 0, mDividerHeight, 0);

}

}

//绘制分割线

@Override

public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

super.onDraw(c, parent, state);

if (mOrientation == LinearLayoutManager.VERTICAL) {

drawVertical(c, parent);

} else {

drawHorizontal(c, parent);

}

}

//绘制横向 item 分割线

private void drawHorizontal(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 + mDividerHeight;

if (mDivider != null) {

mDivider.setBounds(left, top, right, bottom);

mDivider.draw(canvas);

}

if (mPaint != null) {

canvas.drawRect(left, top, right, bottom, mPaint);

}

}

}

//绘制纵向 item 分割线

private void drawVertical(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 + mDividerHeight;

if (mDivider != null) {

mDivider.setBounds(left, top, right, bottom);

mDivider.draw(canvas);

}

if (mPaint != null) {

canvas.drawRect(left, top, right, bottom, mPaint);

}

}

}

}

使用方法如下:

mRv.addItemDecoration(new RecycleViewDivider(

getActivity(), LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.xxx)));

转载地址:http://fdcpo.baihongyu.com/

你可能感兴趣的文章
初识 Rust 语言的所有权概念
查看>>
Yaafe 安装介绍
查看>>
Python读excel生成数据存入txt文件
查看>>
android初学之自定义控件
查看>>
Android WebView中的JavaScript代码使用
查看>>
认证 (authentication) 和授权 (authorization) 的区别
查看>>
gopherjs 与 echo 配合
查看>>
添加打印机的时候怎样说windows没法连接到打印机毛病为0x00000002
查看>>
手机归属地查询
查看>>
Linux下Nginx的安装
查看>>
Nginx增加模块
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
SCOM 2012 配置篇(一)导入管理包图文教程
查看>>
centos 脚本基础练习 11
查看>>
C-冒泡排序法
查看>>
Hadoop学习--设置配置文件参数列出目录结构--day04
查看>>
我的友情链接
查看>>
笔记 libvirt工具的安装过程
查看>>
Spring错误问题收集
查看>>