divier for RecyclerView (you can control whether the boundary is displayed)

Keywords: Android

Design Idea: Because ItemDecoration is a modification of an item, if a split line is used, add a split line to the bottom of the item and there must be one more at the bottom of the last line of item.
1. If you want to delete the edges of all the items, you can only compute them dynamically: for example, 10 items only have 9 dividing lines, and the total height of 9 dividing lines is divided by 10, which is one height and divided into two parts.
2. If you want to add edges of all items, you can only calculate them dynamically: for example, 10 items have 11 dividing lines, and the total height of 11 dividing lines is divided by 10, which is one height and divided into two parts
Drawable cannot be set because Decoration s around each item are not equal.
package com.xin.ui.helper;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.xin.views.R;


@SuppressWarnings("SuspiciousNameCombination")
public class DividerDecoration extends RecyclerView.ItemDecoration {
    private boolean showParentEdge = true;
    private int divideColor = 0xffebe9e9;
    private int divideHeight = 0;
    private Paint paint;
    private Rect rect;

    public DividerDecoration(Context rv) {
        divideHeight = rv.getResources().getDimensionPixelSize(R.dimen.recyclerview_divider);
        init();
    }

    public DividerDecoration(Context rv, boolean showParentEdge) {
        divideHeight = rv.getResources().getDimensionPixelSize(R.dimen.recyclerview_divider);
        this.showParentEdge = showParentEdge;
        init();
    }

    public DividerDecoration(int divideColor, int divideHeight, boolean showParentEdge) {
        this.divideColor = divideColor;
        this.divideHeight = divideHeight;
        this.showParentEdge = showParentEdge;
        init();
    }

    /**
     * rv The edge of is added through padding or margin
     */
    public void init() {
        rect = new Rect();
        paint = new Paint();
    }

    /**
     * Add margins to bottom and right
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        int position = layoutManager.getPosition(view);
        int spanCount;
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        } else {
            spanCount = 1;
        }
        if (showParentEdge) {
            if (spanCount > position) {//first line
                outRect.top = divideHeight;
            }
            outRect.bottom = divideHeight;
            float avgDivider = divideHeight * (spanCount + 1f) / spanCount - divideHeight;
            int spanIndex = position % spanCount;
            outRect.left = (int) (divideHeight - spanIndex * avgDivider);
            outRect.right = (int) ((spanIndex + 1) * avgDivider);
        } else {
            if (spanCount > position) {//first line
                outRect.top = 0;
            } else {
                outRect.top = divideHeight;
            }
            float avgDivider = divideHeight - divideHeight * (spanCount - 1f) / spanCount;
            int spanIndex = position % spanCount;
            outRect.left = (int) (spanIndex * avgDivider);//Since the width of each item+decoration is the same, the total size of the dividing lines for each column needs to be divided into equal sizes
            outRect.right = (int) (divideHeight - (spanIndex + 1) * avgDivider);
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        paint.setColor(divideColor);
        for (int i = 0; i < parent.getChildCount(); i++) {//Lines of right s and bottom s for all view s
            View view = parent.getChildAt(i);
            int left = view.getLeft();
            int top = view.getTop();
            int right = view.getRight();
            int bottom = view.getBottom();
            int dbottom = layoutManager.getBottomDecorationHeight(view);
            int dright = layoutManager.getRightDecorationWidth(view);
            int dtop = layoutManager.getTopDecorationHeight(view);
            int dleft = layoutManager.getLeftDecorationWidth(view);
            rect.bottom = bottom;
            rect.top = top;
            rect.left = left - dleft;
            rect.right = left;
            c.drawRect(rect, paint);//left
            rect.right = right + dright;
            rect.left = right;
            c.drawRect(rect, paint);//Right
            rect.left = left - dleft;
            rect.top = top - dtop;
            rect.bottom = top;
            c.drawRect(rect, paint);//Top
            rect.top = bottom;
            rect.bottom = bottom + dbottom;
            c.drawRect(rect, paint);//Bottom
        }
    }
}
 <dimen name="recyclerview_divider">0.5dp</dimen>

Posted by himnbandit on Mon, 13 Jul 2020 08:59:30 -0700