recycleview添加分割线

package com.hlds.cp.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class RecyclerSpace extends RecyclerView.ItemDecoration {
    private int space;  
    private Drawable mDivider;
    private Paint mPaint;
    private int type;  
  

  
    public RecyclerSpace(int space) {  
        this.space = space;  
    }  
  
    public RecyclerSpace(int space, String color) {
        this.space = space;
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor(color));
        mPaint.setStyle(Paint.Style.FILL);  
        mPaint.setStrokeWidth(space * 2);  
    }  
    public RecyclerSpace(int space, String color,int type) {
        this.space = space;
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor(color));
        mPaint.setStyle(Paint.Style.FILL);  
        mPaint.setStrokeWidth(space * 2);
        this.type=type;  
    }  
  
    public RecyclerSpace(int space, Drawable mDivider) {  
        this.space = space;  
        this.mDivider = mDivider;  
    }  
  
    @Override  
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {  
        if (parent.getLayoutManager() != null) {  
            if (parent.getLayoutManager() instanceof LinearLayoutManager && !(parent.getLayoutManager() instanceof GridLayoutManager)) {
                if (((LinearLayoutManager) parent.getLayoutManager()).getOrientation() == LinearLayoutManager.HORIZONTAL) {  
                    outRect.set(space, 0, space, 0);  
                } else {  
                    outRect.set(0, space, 0, space);  
                }  
            } else {  
                outRect.set(space, space, space, space);  
            }  
        }  
  
    }  
  
    @Override  
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);  
        if (parent.getLayoutManager() != null) {  
            if (parent.getLayoutManager() instanceof LinearLayoutManager && !(parent.getLayoutManager() instanceof GridLayoutManager)) {  
                if (((LinearLayoutManager) parent.getLayoutManager()).getOrientation() == LinearLayoutManager.HORIZONTAL) {  
                    drawHorizontal(c, parent);  
                } else {  
                    drawVertical(c, parent);  
                }  
            } else {  
                if(type==0){  
                    drawGrideview(c, parent);  
                }else{  
                    drawGrideview1(c, parent);  
                }  
            }  
        }  
    }  
  
    //绘制纵向 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 + space;  
            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 drawHorizontal(Canvas canvas, RecyclerView parent) {  
        int left = parent.getPaddingLeft();  
        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();  
            int top = child.getBottom() + layoutParams.bottomMargin;  
            int bottom = top + space;  
            if (mDivider != null) {  
                mDivider.setBounds(left, top, right, bottom);  
                mDivider.draw(canvas);  
            }  
            if (mPaint != null) {  
                canvas.drawRect(left, top, right, bottom, mPaint);  
            }  
  
        }  
    }  
  
    //绘制grideview item 分割线 不是填充满的  
    private void drawGrideview(Canvas canvas, RecyclerView parent) {  
        GridLayoutManager linearLayoutManager = (GridLayoutManager) parent.getLayoutManager();  
        int childSize = parent.getChildCount();  
        int other = parent.getChildCount() / linearLayoutManager.getSpanCount() - 1;  
        if (other < 1) {  
            other = 1;  
        }  
        other = other * linearLayoutManager.getSpanCount();  
        if (parent.getChildCount() < linearLayoutManager.getSpanCount()) {  
            other = parent.getChildCount();  
        }  
        int top, bottom, left, right, spancount;  
        spancount = linearLayoutManager.getSpanCount() - 1;  
        for (int i = 0; i < childSize; i++) {  
            final View child = parent.getChildAt(i);  
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();  
            if (i < other) {  
                top = child.getBottom() + layoutParams.bottomMargin;  
                bottom = top + space;  
                left = (layoutParams.leftMargin + space) * (i + 1);  
                right = child.getMeasuredWidth() * (i + 1) + left + space * i;  
                if (mDivider != null) {  
                    mDivider.setBounds(left, top, right, bottom);  
                    mDivider.draw(canvas);  
                }  
                if (mPaint != null) {  
                    canvas.drawRect(left, top, right, bottom, mPaint);  
                }  
            }  
            if (i != spancount) {  
                top = (layoutParams.topMargin + space) * (i / linearLayoutManager.getSpanCount() + 1);  
                bottom = (child.getMeasuredHeight() + space) * (i / linearLayoutManager.getSpanCount() + 1) + space;  
                left = child.getRight() + layoutParams.rightMargin;  
                right = left + space;  
                if (mDivider != null) {  
                    mDivider.setBounds(left, top, right, bottom);  
                    mDivider.draw(canvas);  
                }  
                if (mPaint != null) {  
                    canvas.drawRect(left, top, right, bottom, mPaint);  
                }  
            } else {  
                spancount += 4;  
            }  
        }  
    }

    /***/  
    private void drawGrideview1(Canvas canvas, RecyclerView parent) {  
        GridLayoutManager linearLayoutManager = (GridLayoutManager) parent.getLayoutManager();  
        int childSize = parent.getChildCount();  
        int top, bottom, left, right,spancount;  
        spancount=linearLayoutManager.getSpanCount();  
        for (int i = 0; i < childSize; i++) {  
            final View child = parent.getChildAt(i);  
            //画横线  
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();  
            top = child.getBottom() + layoutParams.bottomMargin;  
            bottom = top + space;  
            left = layoutParams.leftMargin+child.getPaddingLeft()+space;  
            right = child.getMeasuredWidth() * (i + 1) + left + space * i;  
            if (mDivider != null) {  
                mDivider.setBounds(left, top, right, bottom);  
                mDivider.draw(canvas);  
            }  
            if (mPaint != null) {  
                canvas.drawRect(left, top, right, bottom, mPaint);  
            }  
            //画竖线  
            top = (layoutParams.topMargin + space) * (i / linearLayoutManager.getSpanCount() + 1);  
            bottom = (child.getMeasuredHeight() + space) * (i / linearLayoutManager.getSpanCount() + 1) + space;  
            left = child.getRight() + layoutParams.rightMargin;  
            right = left + space;  
            if (mDivider != null) {  
                mDivider.setBounds(left, top, right, bottom);  
                mDivider.draw(canvas);  
            }  
            if (mPaint != null) {  
                canvas.drawRect(left, top, right, bottom, mPaint);  
            }  
  
            //画上缺失的线框  
            if(i<spancount){
                top = child.getTop() + layoutParams.topMargin;  
                bottom = top + space;  
                left = (layoutParams.leftMargin + space) * (i + 1);  
                right = child.getMeasuredWidth() * (i + 1) + left + space * i;  
                if (mDivider != null) {  
                    mDivider.setBounds(left, top, right, bottom);  
                    mDivider.draw(canvas);  
                }  
                if (mPaint != null) {  
                    canvas.drawRect(left, top, right, bottom, mPaint);  
                }  
            }
            if(i%spancount==0){  
                top = (layoutParams.topMargin + space) * (i / linearLayoutManager.getSpanCount() + 1);  
                bottom = (child.getMeasuredHeight() + space) * (i / linearLayoutManager.getSpanCount() + 1) + space;  
                left = child.getLeft() + layoutParams.leftMargin;  
                right = left + space;  
                if (mDivider != null) {  
                    mDivider.setBounds(left, top, right, bottom);  
                    mDivider.draw(canvas);  
                }  
                if (mPaint != null) {  
                    canvas.drawRect(left, top, right, bottom, mPaint);  
                }  
            }  
        }  
    }
}

 

 

使用方法:

 

recyclerView.addItemDecoration(new RecyclerSpace(10,"#f7f7f7",1));  不同构造方法,改构造为网格布局,第一个参数表示边距,线的颜色,布局类型
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值