当前位置: 首页 > news >正文

Android开发中自定义View实现RecyclerView下划线

本篇文章主要讲解的是有关RecyclerView下划线的使用,主要有几个方法,具体如下:

第一种方式:网格分割线

    public class GridDivider extends RecyclerView.ItemDecoration {

        private Drawable mDividerDarwable;
        private int mDividerHight = 1;
        private Paint mColorPaint;


        public final int[] ATRRS = new int[]{android.R.attr.listDivider};

        public GridDivider(Context context) {
            final TypedArray ta = context.obtainStyledAttributes(ATRRS);
            this.mDividerDarwable = ta.getDrawable(0);
            ta.recycle();
        }

        /*
         int dividerHight  分割线的线宽
         int dividerColor  分割线的颜色
         */
        public GridDivider(Context context, int dividerHight, int dividerColor) {
            this(context);
            mDividerHight = dividerHight;
            mColorPaint = new Paint();
            mColorPaint.setColor(dividerColor);
        }

        /*
         int dividerHight  分割线的线宽
         Drawable dividerDrawable  图片分割线
         */
        public GridDivider(Context context, int dividerHight, Drawable dividerDrawable) {
            this(context);
            mDividerHight = dividerHight;
            mDividerDarwable = dividerDrawable;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
            //画水平和垂直分割线
            drawHorizontalDivider(c, parent);
            drawVerticalDivider(c, parent);
        }

        public void drawVerticalDivider(Canvas c, RecyclerView parent) {
            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

                final int top = child.getTop() - params.topMargin;
                final int bottom = child.getBottom() + params.bottomMargin;

                int left = 0;
                int right = 0;

                //左边第一列
                if ((i % 3) == 0) {
                    //item左边分割线
                    left = child.getLeft();
                    right = left + mDividerHight;
                    mDividerDarwable.setBounds(left, top, right, bottom);
                    mDividerDarwable.draw(c);
                    if (mColorPaint != null) {
                        c.drawRect(left, top, right, bottom, mColorPaint);
                    }
                    //item右边分割线
                    left = child.getRight() + params.rightMargin - mDividerHight;
                    right = left + mDividerHight;
                } else {
                    //非左边第一列
                    left = child.getRight() + params.rightMargin - mDividerHight;
                    right = left + mDividerHight;
                }
                //画分割线
                mDividerDarwable.setBounds(left, top, right, bottom);
                mDividerDarwable.draw(c);
                if (mColorPaint != null) {
                    c.drawRect(left, top, right, bottom, mColorPaint);
                }

            }
        }

        public void drawHorizontalDivider(Canvas c, RecyclerView parent) {

            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

                final int left = child.getLeft() - params.leftMargin - mDividerHight;
                final int right = child.getRight() + params.rightMargin;
                int top = 0;
                int bottom = 0;

                // 最上面一行
                if ((i / 3) == 0) {
                    //当前item最上面的分割线
                    top = child.getTop();
                    //当前item下面的分割线
                    bottom = top + mDividerHight;
                    mDividerDarwable.setBounds(left, top, right, bottom);
                    mDividerDarwable.draw(c);
                    if (mColorPaint != null) {
                        c.drawRect(left, top, right, bottom, mColorPaint);
                    }
                    top = child.getBottom() + params.bottomMargin;
                    bottom = top + mDividerHight;
                } else {
                    top = child.getBottom() + params.bottomMargin;
                    bottom = top + mDividerHight;
                }
                //画分割线
                mDividerDarwable.setBounds(left, top, right, bottom);
                mDividerDarwable.draw(c);
                if (mColorPaint != null) {
                    c.drawRect(left, top, right, bottom, mColorPaint);
                }
            }
        }
    }

第二种方式,水平下划线

第一种:

    public class GridDivider extends RecyclerView.ItemDecoration {

        private Drawable mDividerDarwable;
        private int mDividerHight = 1;
        private Paint mColorPaint;


        public final int[] ATRRS = new int[]{android.R.attr.listDivider};

        public GridDivider(Context context) {
            final TypedArray ta = context.obtainStyledAttributes(ATRRS);
            this.mDividerDarwable = ta.getDrawable(0);
            ta.recycle();
        }

        /*
         int dividerHight  分割线的线宽
         int dividerColor  分割线的颜色
         */
        public GridDivider(Context context, int dividerHight, int dividerColor) {
            this(context);
            mDividerHight = dividerHight;
            mColorPaint = new Paint();
            mColorPaint.setColor(dividerColor);
        }

        /*
         int dividerHight  分割线的线宽
         Drawable dividerDrawable  图片分割线
         */
        public GridDivider(Context context, int dividerHight, Drawable dividerDrawable) {
            this(context);
            mDividerHight = dividerHight;
            mDividerDarwable = dividerDrawable;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
            //画水平和垂直分割线
            drawHorizontalDivider(c, parent);
            drawVerticalDivider(c, parent);
        }

        public void drawVerticalDivider(Canvas c, RecyclerView parent) {
            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

                final int top = child.getTop() - params.topMargin;
                final int bottom = child.getBottom() + params.bottomMargin;

                int left = 0;
                int right = 0;

                //左边第一列
                if ((i % 3) == 0) {
                    //item左边分割线
                    left = child.getLeft();
                    right = left + mDividerHight;
                    mDividerDarwable.setBounds(left, top, right, bottom);
                    mDividerDarwable.draw(c);
                    if (mColorPaint != null) {
                        c.drawRect(left, top, right, bottom, mColorPaint);
                    }
                    //item右边分割线
                    left = child.getRight() + params.rightMargin - mDividerHight;
                    right = left + mDividerHight;
                } else {
                    //非左边第一列
                    left = child.getRight() + params.rightMargin - mDividerHight;
                    right = left + mDividerHight;
                }
                //画分割线
                mDividerDarwable.setBounds(left, top, right, bottom);
                mDividerDarwable.draw(c);
                if (mColorPaint != null) {
                    c.drawRect(left, top, right, bottom, mColorPaint);
                }

            }
        }

        public void drawHorizontalDivider(Canvas c, RecyclerView parent) {

            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

                final int left = child.getLeft() - params.leftMargin - mDividerHight;
                final int right = child.getRight() + params.rightMargin;
                int top = 0;
                int bottom = 0;

                // 最上面一行
                if ((i / 3) == 0) {
                    //当前item最上面的分割线
                    top = child.getTop();
                    //当前item下面的分割线
                    bottom = top + mDividerHight;
                    mDividerDarwable.setBounds(left, top, right, bottom);
                    mDividerDarwable.draw(c);
                    if (mColorPaint != null) {
                        c.drawRect(left, top, right, bottom, mColorPaint);
                    }
                    top = child.getBottom() + params.bottomMargin;
                    bottom = top + mDividerHight;
                } else {
                    top = child.getBottom() + params.bottomMargin;
                    bottom = top + mDividerHight;
                }
                //画分割线
                mDividerDarwable.setBounds(left, top, right, bottom);
                mDividerDarwable.draw(c);
                if (mColorPaint != null) {
                    c.drawRect(left, top, right, bottom, mColorPaint);
                }
            }
        }
    }

第二种:

    public class GridDivider extends RecyclerView.ItemDecoration {

        private Drawable mDividerDarwable;
        private int mDividerHight = 1;
        private Paint mColorPaint;


        public final int[] ATRRS = new int[]{android.R.attr.listDivider};

        public GridDivider(Context context) {
            final TypedArray ta = context.obtainStyledAttributes(ATRRS);
            this.mDividerDarwable = ta.getDrawable(0);
            ta.recycle();
        }

        /*
         int dividerHight  分割线的线宽
         int dividerColor  分割线的颜色
         */
        public GridDivider(Context context, int dividerHight, int dividerColor) {
            this(context);
            mDividerHight = dividerHight;
            mColorPaint = new Paint();
            mColorPaint.setColor(dividerColor);
        }

        /*
         int dividerHight  分割线的线宽
         Drawable dividerDrawable  图片分割线
         */
        public GridDivider(Context context, int dividerHight, Drawable dividerDrawable) {
            this(context);
            mDividerHight = dividerHight;
            mDividerDarwable = dividerDrawable;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
            //画水平和垂直分割线
            drawHorizontalDivider(c, parent);
            drawVerticalDivider(c, parent);
        }

        public void drawVerticalDivider(Canvas c, RecyclerView parent) {
            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

                final int top = child.getTop() - params.topMargin;
                final int bottom = child.getBottom() + params.bottomMargin;

                int left = 0;
                int right = 0;

                //左边第一列
                if ((i % 3) == 0) {
                    //item左边分割线
                    left = child.getLeft();
                    right = left + mDividerHight;
                    mDividerDarwable.setBounds(left, top, right, bottom);
                    mDividerDarwable.draw(c);
                    if (mColorPaint != null) {
                        c.drawRect(left, top, right, bottom, mColorPaint);
                    }
                    //item右边分割线
                    left = child.getRight() + params.rightMargin - mDividerHight;
                    right = left + mDividerHight;
                } else {
                    //非左边第一列
                    left = child.getRight() + params.rightMargin - mDividerHight;
                    right = left + mDividerHight;
                }
                //画分割线
                mDividerDarwable.setBounds(left, top, right, bottom);
                mDividerDarwable.draw(c);
                if (mColorPaint != null) {
                    c.drawRect(left, top, right, bottom, mColorPaint);
                }

            }
        }

        public void drawHorizontalDivider(Canvas c, RecyclerView parent) {

            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

                final int left = child.getLeft() - params.leftMargin - mDividerHight;
                final int right = child.getRight() + params.rightMargin;
                int top = 0;
                int bottom = 0;

                // 最上面一行
                if ((i / 3) == 0) {
                    //当前item最上面的分割线
                    top = child.getTop();
                    //当前item下面的分割线
                    bottom = top + mDividerHight;
                    mDividerDarwable.setBounds(left, top, right, bottom);
                    mDividerDarwable.draw(c);
                    if (mColorPaint != null) {
                        c.drawRect(left, top, right, bottom, mColorPaint);
                    }
                    top = child.getBottom() + params.bottomMargin;
                    bottom = top + mDividerHight;
                } else {
                    top = child.getBottom() + params.bottomMargin;
                    bottom = top + mDividerHight;
                }
                //画分割线
                mDividerDarwable.setBounds(left, top, right, bottom);
                mDividerDarwable.draw(c);
                if (mColorPaint != null) {
                    c.drawRect(left, top, right, bottom, mColorPaint);
                }
            }
        }
    }

以上就是今天主要分享的内容,希望对广大网友有所帮助。

相关文章:

Android开发中自定义View实现RecyclerView下划线

本篇文章主要讲解的是有关RecyclerView下划线的使用&#xff0c;主要有几个方法&#xff0c;具体如下&#xff1a; 第一种方式&#xff1a;网格分割线 public class GridDivider extends RecyclerView.ItemDecoration { private Drawable mDividerDarwable; private i…...

MySQL前百分之N问题--percent_rank()函数

PERCENT_RANK()函数 PERCENT_RANK()函数用于将每行按照(rank - 1) / (rows - 1)进行计算,用以求MySQL中前百分之N问题。其中&#xff0c;rank为RANK()函数产生的序号&#xff0c;rows为当前窗口的记录总行数 PERCENT_RANK()函数返回介于 0 和 1 之间的小数值 selectstudent_…...

【高效开发工具系列】Wolfram Alpha

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…...

分享7种SQL的进阶用法

推荐一款ChatGPT4.0国内站点,每日有免费使用额度,支持PC、APP、VScode插件同步使用 SQL(Structured Query Language)是一种强大的数据库查询和操作语言,它用于与关系数据库进行交互。随着数据的不断增长和应用需求的日益复杂,掌握SQL的进阶用法对于数据库管理员、数据分析…...

protobuf-go pragma.go 文件介绍

pragma.go 文件 文件位于&#xff1a; https://github.com/protocolbuffers/protobuf-go/blob/master/internal/pragma/pragma.go 该文件核心思想&#xff1a; 利用 Golang 语法机制&#xff0c;扩展 Golang 语言特性 目前&#xff0c;该文件提供以下 4 个功能&#xff1a; …...

C#设置程序开机启动

1&#xff1a;获取当前用户&#xff1a; System.Security.Principal.WindowsIdentity identity System.Security.Principal.WindowsIdentity.GetCurrent();System.Security.Principal.WindowsPrincipal principal new System.Security.Principal.WindowsPrincipal(identity);…...

爱可声助听器参与南湖区价值百万公益助残捐赠活动成功举行

“声音大小合适吗&#xff1f;能听清楚吗&#xff1f;”今天下午&#xff0c;一场助残捐赠活动在南湖区凤桥镇悄然举行&#xff0c;杭州爱听科技有限公司带着验配团队和听力检测设备来到活动现场&#xff0c;为南湖区听障残疾人和老人适配助听器。 家住余新镇的75岁的周奶奶身体…...

SpringBoot 实现定时任务

在项目我们会有很多需要在某一特定时刻自动触发某一时间的需求&#xff0c;例如我们提交订单但未支付的超过一定时间后需要自动取消订单。 定时任务实现的几种方式&#xff1a; Timer&#xff1a;java自带的java.util.Timer类&#xff0c;使用这种方式允许你调度一个java.util…...

将Vue2中的console.log调试信息移除

前端项目构建生产环境下的package时&#xff0c;咱们肯定要去掉development环境下的console.log&#xff0c;如果挨个注释可就太费劲了&#xff0c;本文介绍怎么使用 babel-plugin-transform-remove-console 移除前端项目中所有的console.log. 1. 安装依赖 npm install babel-…...

EMC设计检查建议,让PCB layout达到最佳性能

EMC&#xff1a;Electro Magnetic Compatibility的简称&#xff0c;也称电磁兼容&#xff0c;各种电气或电子设备在电磁环境复杂的共同空间中&#xff0c;以规定的安全系数满足设计要求的正常工作能力。 本章对于 RK3588产品设计中的 ESD/EMI防护设计及EMC的设计检查给出了建议…...

常用抓包软件集合(Fiddler、Charles)

1. Fiddler 介绍&#xff1a;Fiddler是一个免费的HTTP和HTTPS调试工具&#xff0c;支持Windows平台。它可以捕获HTTP和HTTPS流量&#xff0c;并提供了丰富的调试和分析功能。优点&#xff1a;易于安装、易于使用、支持多种扩展、可以提高开发效率。缺点&#xff1a;只支持Wind…...

C++入门(一)— 使用VScode开发简介

文章目录 C 介绍C 擅长领域C 程序是如何开发编译器、链接器和库编译预处理编译阶段汇编阶段链接阶段 安装集成开发环境 &#xff08;IDE&#xff09;配置编译器&#xff1a;构建配置配置编译器&#xff1a;编译器扩展配置编译器&#xff1a;警告和错误级别配置编译器&#xff1…...

PeakCAN连接到WSL2 Debian

操作步骤 按照以下步骤进行操作&#xff1a; 在Windows下安装PeakCAN驱动并安装&#xff0c;地址是https://www.peak-system.com/PCAN-USB.199.0.html?&L1 在Windows下安装usbipd&#xff0c;地址是https://github.com/dorssel/usbipd-win/releases&#xff0c;最新版是…...

Spring Boot导出EXCEL 文件

主要功能:实现java导出excel到本地 JDK版本&#xff1a;openJDK 20.0.1 依赖pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchem…...

编程笔记 html5cssjs 060 css响应式布局

编程笔记 html5&css&js 060 css响应式布局 一、响应式布局二、Bootstrap简介总结 CSS响应式布局是一种可以在不同设备&#xff08;例如桌面电脑、平板电脑、手机等&#xff09;上自动调整页面布局和样式的技术。 一、响应式布局 使用CSS响应式布局的关键是媒体查询&am…...

建筑行业如何应用3D开发工具HOOPS提升实时设计体验?

建筑行业一直在迅速发展&#xff0c;技术的不断创新也为其带来了新的机遇与挑战。在这一领域&#xff0c;三维图形技术的应用变得尤为重要。HOOPS技术&#xff0c;作为一套用于开发三维图形应用程序的工具和库&#xff0c;为建筑行业带来了深刻的变革。本文将探讨HOOPS技术在建…...

【grafana】使用教程

【grafana】使用教程 一、简介二、下载及安装及配置三、基本概念3.1 数据源&#xff08;Data Source&#xff09;3.2 仪表盘&#xff08;Dashboard&#xff09;3.3 Panel&#xff08;面板&#xff09;3.4 ROW&#xff08;行&#xff09;3.5 共享及自定义 四、常用可视化示例4.1…...

seata 分布式

一、下载安装seata 已经下载好的朋友可以跳过这个步骤。这里下载的是seata1.6.1这个版本。 1、进入seata官网 地址&#xff1a; https://seata.io/zh-cn/index.html 2、进入下载 3、点击下载地址 下载地址&#xff1a; https://github.com/seata/seata 二、配置seata 进入c…...

前端面试题-说说你了解的js数据结构?(2024.1.29)

1、数组 (Array) 数组是一组有序的值的集合&#xff0c;可以通过索引访问。JavaScript 数组可以包含不同的数据类型&#xff0c;并且长度是动态的。 let myArray [1, hello, true, [2, 3]];2、对象 (Object) 对象是无序的键值对的集合。每个键都是字符串或符号&#xff0c;…...

音视频数字化(数字与模拟-录音机)

之前我们说了【数字与模拟-照相机】照相机的数字化,今天聊聊录音机。 说录音机之前,必须说说留声机。留声机是爱迪生1877年宣布发明成功的,研发过程相当复杂,但原理是简单的。 声音的本质是“波”,是物体振动产生的。以乐器为例,打击乐就是敲击(鼓、钹、木鱼、木琴、三…...

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?

编辑&#xff1a;陈萍萍的公主一点人工一点智能 未来机器人的大脑&#xff1a;如何用神经网络模拟器实现更智能的决策&#xff1f;RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战&#xff0c;在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…...

从WWDC看苹果产品发展的规律

WWDC 是苹果公司一年一度面向全球开发者的盛会&#xff0c;其主题演讲展现了苹果在产品设计、技术路线、用户体验和生态系统构建上的核心理念与演进脉络。我们借助 ChatGPT Deep Research 工具&#xff0c;对过去十年 WWDC 主题演讲内容进行了系统化分析&#xff0c;形成了这份…...

《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析 (一)

CSI-2 协议详细解析 (一&#xff09; 1. CSI-2层定义&#xff08;CSI-2 Layer Definitions&#xff09; 分层结构 &#xff1a;CSI-2协议分为6层&#xff1a; 物理层&#xff08;PHY Layer&#xff09; &#xff1a; 定义电气特性、时钟机制和传输介质&#xff08;导线&#…...

vue3 字体颜色设置的多种方式

在Vue 3中设置字体颜色可以通过多种方式实现&#xff0c;这取决于你是想在组件内部直接设置&#xff0c;还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法&#xff1a; 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...

SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现

摘要 本论文旨在设计并实现基于 SpringBoot 和 uniapp 的 Champion 俱乐部微信小程序&#xff0c;以满足俱乐部线上活动推广、会员管理、社交互动等需求。通过 SpringBoot 搭建后端服务&#xff0c;提供稳定高效的数据处理与业务逻辑支持&#xff1b;利用 uniapp 实现跨平台前…...

HTML前端开发:JavaScript 常用事件详解

作为前端开发的核心&#xff0c;JavaScript 事件是用户与网页交互的基础。以下是常见事件的详细说明和用法示例&#xff1a; 1. onclick - 点击事件 当元素被单击时触发&#xff08;左键点击&#xff09; button.onclick function() {alert("按钮被点击了&#xff01;&…...

.Net Framework 4/C# 关键字(非常用,持续更新...)

一、is 关键字 is 关键字用于检查对象是否于给定类型兼容,如果兼容将返回 true,如果不兼容则返回 false,在进行类型转换前,可以先使用 is 关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。 例如有:首先创建一个字符串对象,然后将字符串对象隐…...

Typeerror: cannot read properties of undefined (reading ‘XXX‘)

最近需要在离线机器上运行软件&#xff0c;所以得把软件用docker打包起来&#xff0c;大部分功能都没问题&#xff0c;出了一个奇怪的事情。同样的代码&#xff0c;在本机上用vscode可以运行起来&#xff0c;但是打包之后在docker里出现了问题。使用的是dialog组件&#xff0c;…...

基于SpringBoot在线拍卖系统的设计和实现

摘 要 随着社会的发展&#xff0c;社会的各行各业都在利用信息化时代的优势。计算机的优势和普及使得各种信息系统的开发成为必需。 在线拍卖系统&#xff0c;主要的模块包括管理员&#xff1b;首页、个人中心、用户管理、商品类型管理、拍卖商品管理、历史竞拍管理、竞拍订单…...

MySQL 部分重点知识篇

一、数据库对象 1. 主键 定义 &#xff1a;主键是用于唯一标识表中每一行记录的字段或字段组合。它具有唯一性和非空性特点。 作用 &#xff1a;确保数据的完整性&#xff0c;便于数据的查询和管理。 示例 &#xff1a;在学生信息表中&#xff0c;学号可以作为主键&#xff…...