效果图:

用法:

加入默认切割线:高度为2px。颜色为灰色

mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL));

加入自己定义切割线:可自己定义切割线drawable

mRecyclerView.addItemDecoration(new RecycleViewDivider(
mContext, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));

加入自己定义切割线:可自己定义切割线高度和颜色

mRecyclerView.addItemDecoration(new RecycleViewDivider(
mContext, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.divide_gray_color)));

万能切割线登场:

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);
} //绘制切割线
@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);
}
}
}
}

附:自定的drawable文件一份

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="20dp" />
<solid android:color="#ff992900" />
</shape>

最新文章

  1. Win10 驱动装不上,提示:Windows 无法验证此设备所需的驱动程序的数字签名。该值受安全引导策略保护,无法进行修改或删除。
  2. [转载]GMT地形数据总结
  3. 基于css3新属性transform及原生js实现鼠标拖动3d立方体旋转
  4. vs调试 本地IIS
  5. 各种jee服务器的比较,tomcat, jboss, glassfish, websphere, weblogic
  6. padding and margin.
  7. Nodejs in Visual Studio Code 09.企业网与CNPM
  8. 在sublimetext上打造一个兼容virtualenv的web&amp;python开发环境
  9. 【读书笔记】《未来闪影》罗伯特&#183;J&#183;索耶
  10. Oracle Client: TNS: Connect timeout ocurred.
  11. Windows 多用户远程访问 Ubuntu 14.04桌面
  12. BZOJ 1337: 最小圆覆盖1336: [Balkan2002]Alien最小圆覆盖(随机增量法)
  13. 2017年总结的前端文章——border属性的多方位应用和实现自适应三角形
  14. maven deploy 指定-DaltDeploymentRepository
  15. Velocity 快捷键
  16. Python标准库12 数学与随机数
  17. space.php
  18. Houdini技术体系 基础管线(三) :UE4以选择区域的方式对地形做生成和更新 上篇
  19. Luogu4768 NOI2018 归程 最短路、Kruskal重构树
  20. java框架篇---hibernate(一对多)映射关系

热门文章

  1. tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04
  2. python面向对象(C3算法)(六)
  3. shell-code-5-函数
  4. shell中的$(( )) 的用途:主要用在整数的运算$(( a+b*c ))
  5. BZOJ 2725: [Violet 6]故乡的梦
  6. python基础学习笔记——os模块
  7. C++中的临时变量
  8. windows下在指定目录下打开命令行
  9. 九度oj 题目1340:小A的计算器
  10. (绝对有用)iOS获取UUID,并使用keychain存储