很久之前接触过RecyclerView,前段时间闲来无事用了下,结果发觉又被分割线伤了下。于是找了分代码,理解和完善,形成了适合我的一个个万能分割线工具类。

主要方法:

    /*** 看图说话:get Item Offsets,获得item的偏移量。此方法用来控制item的偏移* @param outRect* @param view* @param parent* @param state*/@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);}
    /*** 绘制分割线* @param c* @param parent* @param state*/@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
}

多的原理不说了,去其他网站搜下吧。

不难,多花不说,分享代码。

全部代码,宁包入住。

package com.duke.test;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;/*** author: duke* version: 2.0* dateTime: 2020-03-11 19:30* description:*/
public class TopicRecycleViewDivider extends RecyclerView.ItemDecoration {// 绘制分割线的画笔private Paint paint;// 如果是画笔绘制,记录分割线宽度或高度 pxprivate int paintWidthPX = 5;// 如果需要绘制给定的 drawableprivate Drawable drawableDivider;// 用画笔绘制颜色,还是绘制特定的drawableprivate DrawType drawType;// 注意:列表的方向,非分割线的方向// LinearLayoutManager.HORIZONTAL 或 LinearLayoutManager.VERTICALprivate int orientation = LinearLayoutManager.VERTICAL;// 是否需要忽略第一个 item 后面的分割线,有些需求需要private boolean isSkipFirstItemBelowLine = false;// 是否需要忽略最后一个 item 后面的分割线,大多数需求都需要private boolean isSkipLaseItemBelowLine = true;/*** 忽略 第一个 item 下方或右侧的分割线。个别需要需要** @param skipFirstItemBelowLine 是否忽略* @return this*/public TopicRecycleViewDivider skipFirstItemBelowLine(boolean skipFirstItemBelowLine) {isSkipFirstItemBelowLine = skipFirstItemBelowLine;return this;}/*** 忽略 最后一个 item 下方或右侧的分割线。一般都需要** @param skipLaseItemBelowLine 是否忽略* @return this*/public TopicRecycleViewDivider skipLaseItemBelowLine(boolean skipLaseItemBelowLine) {isSkipLaseItemBelowLine = skipLaseItemBelowLine;return this;}/*** 设置列表方向,非分割线的** @param listOrientation 列表方向,<br/>*                        LinearLayoutManager.HORIZONTAL or LinearLayoutManager.VERTICAL <br/>* @return this*/public TopicRecycleViewDivider setListOrientation(int listOrientation) {if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {throw new IllegalArgumentException("Parameter of orientation is error. Please see LinearLayoutManager ...");}orientation = listOrientation;return this;}/*** 构造函数** @param context    context* @param drawableId 分割线图片*/public TopicRecycleViewDivider(Context context, int drawableId) {drawType = DrawType.USE_DRAWABLE;drawableDivider = ContextCompat.getDrawable(context, drawableId);}/*** 自定义分割线** @param dividerHeightPX 分割线高度 px* @param dividerColorInt 分割线颜色*/public TopicRecycleViewDivider(int dividerHeightPX, int dividerColorInt) {drawType = DrawType.USE_PAINT;paintWidthPX = dividerHeightPX;// 绘制纯颜色 (之一:可以绘制纯颜色)paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setColor(dividerColorInt);paint.setStyle(Paint.Style.FILL);}/*** 看图说话:get Item Offsets,获得item的偏移量。此方法用来控制item的偏移** @param outRect outRect 表示在 item 的 上、下、左、右 四周撑开的距离,默认值为 0* @param view    当前的 holder view* @param parent  recyclerView* @param state   state*/@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);int index = parent.getChildAdapterPosition(view);// 忽略第一个 item 的分割线if (isSkipFirstItemBelowLine && index == 0) {return;}int totalSize = state.getItemCount();// 忽略最后一个 item 的分割线if (isSkipLaseItemBelowLine && index == totalSize - 1) {return;}/*** 列表的方向为横向,画分割线就是纵向的,需要确定的是 child 的右边偏移值* 留出空间画分割线*/if (orientation == LinearLayoutManager.HORIZONTAL) {int x = 0;switch (drawType) {case USE_PAINT:x = paintWidthPX;break;case USE_DRAWABLE:x = drawableDivider.getIntrinsicWidth();break;}outRect.set(0, 0, x, 0);}/*** 列表的方向为纵向,画分割线就是横向的,需要确定的是 child 的下边偏移值* 留出空间画分割线*/else if (this.orientation == LinearLayoutManager.VERTICAL) {int x = 0;switch (drawType) {case USE_PAINT:x = paintWidthPX;break;case USE_DRAWABLE:x = drawableDivider.getIntrinsicHeight();break;}outRect.set(0, 0, 0, x);}}/*** 绘制分割线** @param c      canvas* @param parent paint* @param state  state*/@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDraw(c, parent, state);if (orientation == LinearLayoutManager.VERTICAL) {// 列表是纵向的,需要绘制横向的分割线drawHorizontalLine(c, parent);} else {// 列表是横向的,需要绘制纵向的分割线drawVerticalLine(c, parent);}}/*** 绘制横向 item 分割线。左、上、右都是可计算的,下需要获取给定的高度值** @param canvas canvas* @param parent paint*/private void drawHorizontalLine(Canvas canvas, RecyclerView parent) {// 左边:到父容器的 left 内间距位置值final int left = parent.getPaddingLeft();// 右边:到父容器的 right 内间距位置值final int right = parent.getMeasuredWidth() - parent.getPaddingRight();final int childSize = parent.getChildCount();// 循环绘制每条分割线for (int i = 0; i < childSize; i++) {if (isSkipFirstItemBelowLine && i == 0) {continue;}if (isSkipLaseItemBelowLine && i == childSize - 1) {continue;}final View child = parent.getChildAt(i);if (!(child.getLayoutParams() instanceof RecyclerView.LayoutParams)) {continue;}RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();// 上边:具体的某条分割线的上边以 child 的 (bottom + bottomMargin) 位置值final int top = child.getBottom() + layoutParams.bottomMargin;// 下边:根据类型判断int bottom;switch (drawType) {case USE_PAINT:// 构造方法声明使用画笔绘制// 下边:top 加上指定的高度bottom = top + paintWidthPX;canvas.drawRect(left, top, right, bottom, paint);break;case USE_DRAWABLE:// 构造方法声明使用 drawable// 下边:top 加上指定的高度bottom = top + drawableDivider.getIntrinsicHeight();drawableDivider.setBounds(left, top, right, bottom);drawableDivider.draw(canvas);break;}}}/*** 绘制纵向 item 分割线。上、下、左都是可计算的,右侧需要获取给定的宽度值** @param canvas canvas* @param parent parent*/private void drawVerticalLine(Canvas canvas, RecyclerView parent) {// 上边:到父容器的 top 内间距位置值final int top = parent.getPaddingTop();// 下边:到父容器的 bottom 内间距位置值final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();final int childSize = parent.getChildCount();// 循环绘制每条分割线for (int i = 0; i < childSize; i++) {if (isSkipFirstItemBelowLine && i == 0) {continue;}if (isSkipLaseItemBelowLine && i == childSize - 1) {continue;}final View child = parent.getChildAt(i);if (!(child.getLayoutParams() instanceof RecyclerView.LayoutParams)) {continue;}RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();// 左边:具体的某条分割线的左边以 child 的 (right + rightMargin) 位置值final int left = child.getRight() + layoutParams.rightMargin;// 右边:根据类型判断int right;switch (drawType) {case USE_PAINT:// 构造方法声明使用画笔绘制// 右边:left 加上指定的宽度right = left + paintWidthPX;canvas.drawRect(left, top, right, bottom, paint);break;case USE_DRAWABLE:// 构造方法声明使用 drawable// 右边:left 加上指定的宽度right = left + drawableDivider.getIntrinsicWidth();drawableDivider.setBounds(left, top, right, bottom);drawableDivider.draw(canvas);break;}}}public enum DrawType {USE_PAINT(1),       // 用画笔绘制纯颜色USE_DRAWABLE(2);    // 绘制特定的 drawableprivate final int type;DrawType(int type) {this.type = type;}public int getType() {return type;}}
}

Android RecyclerView万能分割线相关推荐

  1. RecyclerView万能分割线

    工具 public class RecycleViewDivider extends RecyclerView.ItemDecoration {private Paint mPaint;private ...

  2. Android RecyclerView ItemDecoration 分割线

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/111302734 本文出自[赵彦军的博客] 文章目录 如何设置分割线 Divider ...

  3. Android RecyclerView设置分割线

    1.设置分割线代码 public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {private static ...

  4. Android RecyclerView设置分割线 / 下拉加载 / 选项菜单进行增加删除动画

    首先看一下我的整个程序: 主类: package com.example.day02rk;import android.os.Bundle; import android.support.v4.con ...

  5. RecyclerView的万能分割线

    效果图: 使用方法: 添加默认分割线:高度为2px,颜色为灰色 mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, Lin ...

  6. android 分割线布局,Android RecyclerView网格布局(支持多种分割线)详解(2)

    记录了下RecyclerView的使用方法,并且讲述了线性布局列表的使用方法,在此基础上加上了万能分割线,支持颜色分割线和图片分割线,同时支持对分割线设置线宽. 这篇是总结一下网格布局的使用,同样也支 ...

  7. 第7天 Recyclerview万能的适配器(基本使用、分割线、增加删除动画)

    第7天 Recyclerview万能的适配器(基本使用.分割线.增加删除动画) Recyclerview的使用 思路 代码 Recyclerview的使用 用了很长一段时间的RecyclerView, ...

  8. android 水平方向瀑布流,Android RecyclerView(瀑布流)水平/垂直方向分割线

     Android RecyclerView(瀑布流)水平/垂直方向分割线 Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现Recycle ...

  9. Android RecyclerView 基本使用

    Android RecyclerView 基本使用 概述 RecyclerView出现已经有一段时间了,相信大家肯定不陌生了,大家可以通过导入support-v7对其进行使用. 据官方的介绍,该控件用 ...

最新文章

  1. 安装yaml报错:ERROR: Cannot uninstall 'PyYAML'.
  2. mysql 各种恢复_Mysql数据库备份和还原常用的命令
  3. g2o求解BA 第10章
  4. html表单传值,如何将用户输入的表单值从html传递给javascript?
  5. c语言程序设计课件数组,数组(C语言程序设计)课件
  6. ubuntu下源码安装Python
  7. Java项目课程04:需求分析
  8. 计算机专业教研成绩,2018学年第一学期计算机组教研组工作计划
  9. cocos2dx基础篇(24)——基本动画CCAnimation/CCAnimate
  10. MySQL多字节字符集造成主从数据不一致问题
  11. cesium 实现指南针及比例尺效果
  12. 转-国内移动广告平台的现状对比(2010年8月-10月)
  13. mysql 1033_MySQL ERROR 1033 (HY000): Incorrect information in file. 处理一例
  14. C语言fscanf函数的理解
  15. 量化数值评估,查准率和召回率
  16. luoguP3799 妖梦拼木棒
  17. python判断素数_小白学Python | 你还在说你入不了门吗
  18. Lifecycle 使用与源码分析——彻底搞懂Lifecycle原理
  19. MSP430单片机各种寄存器总结(1)——CPU 寄存器
  20. 中国移动M2M业务支撑基地网站转换为物联网

热门文章

  1. 【实践】电商知识图谱构建及搜索推荐场景下的应用.pdf(附下载链接)
  2. Goland 1.15运行报错:该版本的 %1 与你运行的 Windows 版本不兼容
  3. 基于API的ArrayList集合之学习记录
  4. SMB CIFS DOMIAN
  5. 骁龙660和骁龙835之间的差距到底有多大?
  6. 天气预报小程序的设计与实现
  7. 从苹果2015年春季发布会看移动互联网的发展
  8. FindWithTag用法
  9. 商业模拟游戏:柠檬汁杰克项目
  10. 点云txt文件—pcd文件