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);
                }
            }
        }
    }

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

  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值