Modifying RecyclerView.ItemDecoration

After adding drawDividerIndex and relativeLayoutId, the original file looks like this:

public class CustomDivider extends RecyclerView.ItemDecoration {

    private Paint mPaint;
    private int mDividerHeight = 1;//Split line height, default 1 PX
    private int marginLeft;
    private int marginRight;
    private int drawDividerIndex = -1;//Start drawing lines after the first item
    private static int relativeLayoutId = R.id.rlay_index;

    /**
     * Default splitting line: 1 PX height, grey color
     *
     * @param context
     */
    public CustomDivider(Context context) {
        this(context, 0);

    }

    public CustomDivider(Context context, int marginLeftDp) {
        this(context, marginLeftDp, 0);
    }

    public CustomDivider(Context context, int marginLeftDp, String dividerIndex, int relativeLayoutId) {
        this(context, marginLeftDp, 0);
        this.drawDividerIndex = Integer.parseInt(dividerIndex);
        this.relativeLayoutId = relativeLayoutId;
    }

    //The last pass string is to distinguish it from the following constructions
    public CustomDivider(Context context, int marginLeftDp, String dividerIndex) {
        this(context, marginLeftDp, 0);
        this.drawDividerIndex = Integer.parseInt(dividerIndex);
    }

    public CustomDivider(Context context, int marginLeftDp, int marginRightDp) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(context.getResources().getColor(R.color.app_mid_gray));
        mPaint.setStyle(Paint.Style.FILL);

        this.marginLeft = DimenUtils.dp2px(context, marginLeftDp);
        this.marginRight = DimenUtils.dp2px(context, marginRightDp);
    }

    //Obtaining the Dimension of the Split Line
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        View classifyView = view.findViewById(relativeLayoutId);
        if (classifyView != null) {
            if (classifyView.getVisibility() == View.GONE) {
                outRect.set(0, mDividerHeight, 0, 0);
            }
            int childCount = parent.getChildCount();
            int position = parent.indexOfChild(view);
            boolean isLastView = (position - 1 == childCount);
            if (isLastView) {
                outRect.bottom = mDividerHeight;
            }
        } else {
            super.getItemOffsets(outRect, view, parent, state);
        }
    }

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        int childSize = parent.getChildCount();
        View lastView = parent.getChildAt(childSize - 1);
        if (lastView != null) {
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) lastView.getLayoutParams();
            int top = lastView.getBottom() + layoutParams.bottomMargin;
            int bottom = top + mDividerHeight;
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }

    //Drawing partition lines
    @Override
    public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        int left = parent.getPaddingLeft() + marginLeft;
        int right = parent.getMeasuredWidth() - parent.getPaddingRight() - marginRight;
        int childSize = parent.getChildCount();
        int rows = childSize;
        for (int i = 0; i < rows; i++) {
            if (i > this.drawDividerIndex) {
                View child = parent.getChildAt(i);
                View classifyView = child.findViewById(relativeLayoutId);
                if (classifyView != null && classifyView.getVisibility() == View.GONE) {
                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
                    int top = child.getTop() + layoutParams.topMargin;
                    int bottom = top + mDividerHeight;
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }
    }
}

The default search is R.id.rlay_index, a Relative Layout that can be passed into the constructor to specify the Relative Layout to be looked for

Because some recyclerView s set the headView, and if the top partition is cut, the top of the headView is also drawn, so add a drawDividerIndex, starting with the hidden item of the specified position and classifyView.

The actual test of getItemOffsets and onDraw method does not show the bottom line of lastView on the actual page. The logic code processed in it has problems and does not meet the needs of the last bottom section of the project. The method is really redundant. You can directly paste a partition line at the bottom of lastview in onDraw Over method.

Now that the project is running normally, all recycler Views that use this partition line have no lines at the bottom of the last item, so the bottom line drawing method is removed first.

Get rid of the getItemOffsets method. It's useless to set the top padding to 1. Draw a line directly at the top of onDrawOver.

public class CustomDivider extends RecyclerView.ItemDecoration {

    private Paint mPaint;
    private int mDividerHeight = 1;//Split line height, default 1 PX
    private int marginLeft;
    private int marginRight;
    private int drawDividerIndex = -1;//Start drawing lines after the first item
    private static int relativeLayoutId = R.id.rlay_index;

    /**
     * Default splitting line: 1 PX height, grey color
     *
     * @param context
     */
    public CustomDivider(Context context) {
        this(context, 0);

    }

    public CustomDivider(Context context, int marginLeftDp) {
        this(context, marginLeftDp, 0);
    }

    public CustomDivider(Context context, int marginLeftDp, String dividerIndex, int relativeLayoutId) {
        this(context, marginLeftDp, 0);
        this.drawDividerIndex = Integer.parseInt(dividerIndex);
        this.relativeLayoutId = relativeLayoutId;
    }

    //The last pass string is to distinguish it from the following constructions
    public CustomDivider(Context context, int marginLeftDp, String dividerIndex) {
        this(context, marginLeftDp, 0);
        this.drawDividerIndex = Integer.parseInt(dividerIndex);
    }

    public CustomDivider(Context context, int marginLeftDp, int marginRightDp) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(context.getResources().getColor(R.color.app_mid_gray));
        mPaint.setStyle(Paint.Style.FILL);

        this.marginLeft = DimenUtils.dp2px(context, marginLeftDp);
        this.marginRight = DimenUtils.dp2px(context, marginRightDp);
    }

    //Draw the partition line at the top level
    @Override
    public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        int left = parent.getPaddingLeft() + marginLeft;
        int right = parent.getMeasuredWidth() - parent.getPaddingRight() - marginRight;
        int childSize = parent.getChildCount();
        int rows = childSize;
        for (int i = 0; i < rows; i++) {
            if (i > this.drawDividerIndex) {
                View child = parent.getChildAt(i);
                View classifyView = child.findViewById(relativeLayoutId);
                if (classifyView != null && classifyView.getVisibility() == View.GONE) {
                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
                    int top = child.getTop() + layoutParams.topMargin;
                    int bottom = top + mDividerHeight;
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }
    }
}

The code is refreshing.

 

 

 

 

 

 

 

Posted on Wed, 09 Oct 2019 19:54:47 -0700 by jtron85