自定义GridItemDecoration边框

/**
 * @author: hsw
 * @date: 2021/8/23
 * @desc:
 */
class GridDividerDecoration constructor(
    @Px dividerSize: Float,
    @ColorInt colorInt: Int,
    private val spanCount: Int
) : RecyclerView.ItemDecoration() {
    private val dividerPaint: Paint = Paint()
    private val bounds = Rect()

    init {
        dividerPaint.color = colorInt
        dividerPaint.strokeWidth = dividerSize
        dividerPaint.style = Paint.Style.STROKE
        dividerPaint.isAntiAlias = true
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        drawHorizontal(c, parent)
        drawVertical(c, parent)
    }

    private fun drawHorizontal(canvas: Canvas, recyclerView: RecyclerView) {
        val itemCount = recyclerView.adapter?.itemCount ?: 0
        val childCount = recyclerView.childCount
        val lastRowChildCount = getLastRowChildCount(itemCount)
        for (i in 0 until childCount) {
            val childView = recyclerView.getChildAt(i)
            if (isChildInLastRow(recyclerView, childView, itemCount, lastRowChildCount)) {
                continue
            }
            recyclerView.getDecoratedBoundsWithMargins(childView, bounds)
            canvas.drawLine(
                bounds.left.toFloat(),
                bounds.bottom.toFloat(),
                bounds.right.toFloat(),
                bounds.bottom.toFloat(),
                dividerPaint
            )
        }

    }

    private fun drawVertical(canvas: Canvas, recyclerView: RecyclerView) {
        val childCount = recyclerView.childCount
        val isRTL = ViewCompat.getLayoutDirection(recyclerView) == ViewCompat.LAYOUT_DIRECTION_RTL
        for (i in 0 until childCount) {
            val childView = recyclerView.getChildAt(i)
            if (isChildInLastColumn(recyclerView, childView)) {
                continue
            }
            recyclerView.getDecoratedBoundsWithMargins(childView, bounds)
            val x = if (isRTL) bounds.left.toFloat() else bounds.right.toFloat()
            canvas.drawLine(
                x,
                bounds.top.toFloat(),
                x,
                bounds.bottom.toFloat(),
                dividerPaint
            )
        }
    }

    private fun getLastRowChildCount(itemCount: Int): Int {
        val remainder = itemCount % spanCount
        return if (remainder == 0) spanCount else remainder
    }

    private fun isChildInLastRow(
        recyclerView: RecyclerView,
        view: View,
        itemCount: Int,
        lastRowChildCount: Int
    ): Boolean {
        return recyclerView.getChildAdapterPosition(view) >= itemCount - lastRowChildCount
    }

    private fun isChildInLastColumn(recyclerView: RecyclerView, view: View): Boolean {
        return recyclerView.getChildAdapterPosition(view) % spanCount == spanCount - 1
    }
}

这个是通过绘制的方式实现的边框。当然也可通过设置margin结合背景色的的方式进行实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值