package com.miya.fastcashier.widget;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 通用的RecyclerDivider
 */
public class Divider extends RecyclerView.ItemDecoration {
    private Paint paint;
    private int divideWidth;

    public Divider(int divideWidth, int color) {
        super();
        this.paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        this.divideWidth = divideWidth;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingStart();
        int right = parent.getMeasuredWidth() - parent.getPaddingEnd();
        View child;
        int childPosition;
        int validChildCount = parent.getChildCount();
        for (int i = 0; i < validChildCount; i++) {
            child = parent.getChildAt(i);
            childPosition = parent.getChildAdapterPosition(child);
            if (childPosition == validChildCount - 1) {
                break;
            }
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = child.getBottom() + layoutParams.bottomMargin;
            int bottom = top + divideWidth;
            c.drawRect(left, top, right, bottom, paint);
        }
    }
}