RecyclerDemacration.java 5.39 KB
package com.cnlive.goldenline.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

/**
 * RecyclerView 间隔线  可以传递颜色值
 * Created on 2017/2/23
 * Version V1.0
 */
public class RecyclerDemacration extends RecyclerView.ItemDecoration {

    //布局方向  既有垂直 又有  水平 即 用于 GridLaytoutManager
    protected static final int ORIENTATION_HYBIRD = 0x2;

    /**
     * RecyclerView布局方向 默认垂直
     */
    private int mOrientation = LinearLayoutManager.VERTICAL;


    /**
     * 画笔
     */
    private Paint mPaint;

    /**
     * 间隔线高度
     */
    private int mDividerHeight = 2;

    private Drawable mDrawable;

    /**
     * @param mOrientation
     * @param color        Color.BLACK 或者 getResource().getColor(R.color.xxx);
     */
    public RecyclerDemacration(@NonNull Context context, int mOrientation, int color, int mDividerHeight) {
        if (mOrientation != LinearLayoutManager.VERTICAL && mOrientation != LinearLayoutManager.HORIZONTAL && mOrientation != ORIENTATION_HYBIRD)
            throw new IllegalArgumentException("orientation is Illegal , Must be LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL or ORIENTATION_HYBIRD");
        this.mOrientation = mOrientation;
        mPaint = new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        this.mDividerHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mDividerHeight, context.getResources().getDisplayMetrics());
    }

    public RecyclerDemacration(Context context, int mOrientation, Drawable mDrawable) {
        if (mOrientation != LinearLayoutManager.VERTICAL && mOrientation != LinearLayoutManager.HORIZONTAL && mOrientation != ORIENTATION_HYBIRD)
            throw new IllegalArgumentException("orientation is Illegal , Must be LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL or ORIENTATION_HYBIRD");
        this.mOrientation = mOrientation;
        this.mDrawable = mDrawable;
    }

    // 子Item绘制之前
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }

    // 子item 绘制之后
    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        switch (mOrientation) {
            case LinearLayoutManager.VERTICAL:
                drawVertical(c, parent);
                break;
            case LinearLayoutManager.HORIZONTAL:
                drawHorizontal(c, parent);
                break;
            case ORIENTATION_HYBIRD:
                drawVertical(c, parent);
                drawHorizontal(c, parent);
                break;
            default:
                drawVertical(c, parent);
                break;
        }
    }

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

    private void drawVertical(@NonNull Canvas c, @NonNull RecyclerView parent) {
        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 top = child.getBottom() + layoutParams.bottomMargin;
            int left = child.getLeft() - layoutParams.leftMargin;
            int right = child.getRight() + layoutParams.rightMargin;
            if (mDrawable != null) { // 优先绘制Drawable
                right += mDrawable.getIntrinsicWidth();
                final int bottom = top + mDrawable.getIntrinsicHeight();
                mDrawable.setBounds(left, top, right, bottom);
                mDrawable.draw(c);
            } else {//其次绘制纯色
                right += mDividerHeight;
                final int bottom = top + mDividerHeight;
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }


    private void drawHorizontal(@NonNull Canvas c, @NonNull RecyclerView parent) {

        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.getTop() - layoutParams.topMargin;
            int bottom = child.getBottom() + layoutParams.bottomMargin;
            int left = child.getRight() + layoutParams.rightMargin;
            if (mDrawable != null) { // 优先绘制Drawable
                final int right = left + mDrawable.getIntrinsicWidth();
                mDrawable.setBounds(left, top, right, bottom);
                mDrawable.draw(c);
            } else {//其次绘制纯色
                final int right = left + mDividerHeight;
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

}