RecyclerView通用分割线

可自定义分割线的颜色 宽度   虚线  适用RecyclerView 的  GridLayoutManager  LinearLayoutManager 一行代码搞定

代码拿走就能用   如果好用  别忘了点个赞 


```

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.DashPathEffect;

import android.graphics.Paint;

import android.graphics.Rect;

import android.support.annotation.NonNull;

import android.support.v7.widget.GridLayoutManager;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.util.Log;

import android.view.View;

/**

* hukui  2019/5/21 8:30

**/

public class RcyclerItemDecoration extends RecyclerView.ItemDecoration {

    private int mDividerHeight = 1;//分割线高度,默认为1px

    private int mDividerColor = Color.parseColor("#8b8b8b");//分割线颜色

    private float dashWidth = 0;//虚线长度

    private float dashGap = 0;//虚线间隔长度

    private int spanCount;//每列的个数

    private int mMaxSpanGroupIndex;

    Paint paint;

    public RcyclerItemDecoration() {

        initDrawable();

}

    public RcyclerItemDecoration(int mDividerHeight) {

        this.mDividerHeight = mDividerHeight;

        initDrawable();

}

    public RcyclerItemDecoration(int mDividerHeight, int mDividerColor) {

        this.mDividerHeight = mDividerHeight;

        this.mDividerColor = mDividerColor;

        initDrawable();

}

    public RcyclerItemDecoration(int mDividerHeight, int mDividerColor, float dashWidth, float dashGap) {

        this.mDividerHeight = mDividerHeight;

        this.mDividerColor = mDividerColor;

        this.dashWidth = dashWidth;

        this.dashGap = dashGap;

        initDrawable();

}

    private void initDrawable() {

        paint = new Paint();

        paint.setColor(mDividerColor);

        paint.setStrokeWidth(mDividerHeight);

        paint.setStyle(Paint.Style.STROKE);

        paint.setAntiAlias(true);

        paint.setPathEffect(new DashPathEffect(new float[]{dashWidth, dashGap}, 0));

}

    @Override

    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

        super.getItemOffsets(outRect, view, parent, state);

        //获取当前位置

        int position = parent.getChildAdapterPosition(view);

        view.setTag(position);

        RecyclerView.LayoutManager manager = parent.getLayoutManager();

        if (manager instanceof GridLayoutManager) {

            GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();

            GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

            //获取每列的个数

            spanCount = gridLayoutManager.getSpanCount();

            //获取最大的列数

            mMaxSpanGroupIndex = spanSizeLookup.getSpanGroupIndex(parent.getAdapter().getItemCount() - 1, spanCount);

            //获取当前列数

            int spanGroupIndex = spanSizeLookup.getSpanGroupIndex(position, spanCount);

            //当前列中占用的个数

            int spanSize = spanSizeLookup.getSpanSize(position);

            if (spanGroupIndex == mMaxSpanGroupIndex && spanGroupIndex != 0) {

                if (spanSize < spanCount) {

                    outRect.right = mDividerHeight;

}

            } else {

                //view 没有占据一列 并不是当前列的第一个

                if (spanSize < spanCount && spanSize != 0) {

                    outRect.left = mDividerHeight;

}

}

            if (spanGroupIndex != 0) {

                outRect.top = mDividerHeight;

}

        } else if (manager instanceof LinearLayoutManager) {

            LinearLayoutManager layoutManager = (LinearLayoutManager) manager;

            if (position != 0) {

                if (layoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {

                    outRect.left = mDividerHeight;

                } else {

                    outRect.top = mDividerHeight;

}

}

}

}

    @Override

    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

        super.onDraw(c, parent, state);

        RecyclerView.LayoutManager manager = parent.getLayoutManager();

        if (manager instanceof GridLayoutManager) {

            drawGrid(c, parent);

        } else if (manager instanceof LinearLayoutManager) {

            LinearLayoutManager layoutManager = (LinearLayoutManager) manager;

            if (layoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {

                draHorizontal(c, parent);

            } else {

                draVertical(c, parent);

}

}

}

    private void drawGrid(Canvas c, RecyclerView parent) {

        GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();

        int count = parent.getChildCount();

        GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        for (int i= 0; i< count; i++) {

            View view = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

            int position = (int) view.getTag();

            //获取当前列数

            int spanGroupIndex = spanSizeLookup.getSpanGroupIndex(position, spanCount);

            //当前列中的位置

            int spanSize = spanSizeLookup.getSpanIndex(position, spanCount);

            int left, top, right, bottom;

            if (spanGroupIndex == mMaxSpanGroupIndex && spanGroupIndex != 0) {

                if (spanSize < spanCount) {

                    left= view.getRight();

                    right= left+ mDividerHeight - params.rightMargin;

                    top= view.getTop() - params.topMargin;

                    bottom= view.getBottom() + params.bottomMargin;

                    c.drawLine(left, top, right, bottom, paint);

}

            } else {

                //竖线  左边需要分割线,开始绘制

                if (spanSize < spanCount && spanSize != 0) {

                    left= view.getLeft() - mDividerHeight - params.leftMargin;

                    right= left+ mDividerHeight;

                    top= view.getTop() - params.topMargin;

                    bottom= view.getBottom() + params.bottomMargin;

                    c.drawLine(left, top, right, bottom, paint);

}

}

            //画横线 最后一组不需要绘制

            if (spanGroupIndex < mMaxSpanGroupIndex) {

                left= view.getLeft() + params.leftMargin;

                right= left+ view.getMeasuredWidth() + params.rightMargin;

                top= view.getBottom() + params.bottomMargin;

                bottom= top+ mDividerHeight;

                c.drawLine(left, top, right, bottom, paint);

}

}

}

    //横向分割线

    private void draVertical(Canvas c, RecyclerView parent) {

        int count = parent.getChildCount();

        int left = parent.getPaddingLeft();

        int right = parent.getMeasuredWidth() - parent.getPaddingRight();

        for (int i= 0; i< count; i++) {

            View view = parent.getChildAt(i);

            int position = (int) view.getTag();

            if (position == 0) {

                continue;

}

            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();

            int top = view.getBottom() + layoutParams.bottomMargin;

            int bottom = top + mDividerHeight;

            c.drawLine(left, top, right, bottom, paint);

}

}

    //竖直分割线

    private void draHorizontal(Canvas c, RecyclerView parent) {

        LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();

        int count = parent.getChildCount();

        int top = parent.getPaddingTop();

        int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();

        for (int i= 0; i< count; i++) {

            View view = parent.getChildAt(i);

            int position = (int) view.getTag();

            if (position == layoutManager.getChildCount() - 1) {

                continue;

}

            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();

            int left = view.getRight() + layoutParams.rightMargin;

            int right = left + mDividerHeight;

            c.drawLine(left, top, right, bottom, paint);

}

}

}

```

你可能感兴趣的:(RecyclerView通用分割线)