当前位置: 首页 > 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);
                }
            }
        }
    }

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

http://www.lryc.cn/news/290485.html

相关文章:

  • MySQL前百分之N问题--percent_rank()函数
  • 【高效开发工具系列】Wolfram Alpha
  • 分享7种SQL的进阶用法
  • protobuf-go pragma.go 文件介绍
  • C#设置程序开机启动
  • 爱可声助听器参与南湖区价值百万公益助残捐赠活动成功举行
  • SpringBoot 实现定时任务
  • 将Vue2中的console.log调试信息移除
  • EMC设计检查建议,让PCB layout达到最佳性能
  • 常用抓包软件集合(Fiddler、Charles)
  • C++入门(一)— 使用VScode开发简介
  • PeakCAN连接到WSL2 Debian
  • Spring Boot导出EXCEL 文件
  • 编程笔记 html5cssjs 060 css响应式布局
  • 建筑行业如何应用3D开发工具HOOPS提升实时设计体验?
  • 【grafana】使用教程
  • seata 分布式
  • 前端面试题-说说你了解的js数据结构?(2024.1.29)
  • 音视频数字化(数字与模拟-录音机)
  • 鸿蒙开发-UI-组件3
  • 安全测试几种:代码静态扫描、模糊测试、黑盒测试、白盒测试、渗透测试
  • Mac安装及配置MySql及图形化工具MySQLworkbench安装
  • 【Vue】为什么Vue3使用Proxy代替defineProperty?
  • 3、css设置样式总结、节点、节点之间关系、创建元素的方式、BOM
  • 计算机网络-物理层传输介质(导向传输介质-双绞线 同轴电缆 光纤和非导向性传输介质-无线波 微波 红外线 激光)
  • springboot3+vue3支付宝在线支付案例-渲染产品列表页面
  • 数字美妆技术:美颜SDK和动态贴纸技术的崭新时代
  • 使用OpenCV实现一个简单的实时人脸跟踪
  • 关于监控的那些事,你有必要了解一下
  • C#学习笔记_数组