美文网首页
RecyclerView 绘制表格

RecyclerView 绘制表格

作者: 善良的老农 | 来源:发表于2024-11-20 21:38 被阅读0次

调用

addItemDecoration(

TextGridSpacingItemDecoration(

3,//三列

        1f.dp,//表格线 1dp

        R.color.color_f1f1f5.getColor()//线颜色

)

)

分割线类图如下:

package com.shizhuang.poizon.modules.sell.batchSellDialog.view

import android.graphics.*

import android.view.View

import androidx.recyclerview.widget.RecyclerView

import com.shizhuang.poizon.modules.ui.components.dp

/**

*

* @Author: chenfeipeng

* @Email: o_chenfeipeng@shizhuang-inc.com

* @Date: 2024/11/21

* @Description:  文本网格分割线

* ----------------------------------------------

* important modify:

*

*/

class TextGridSpacingItemDecoration(private val spanCount: Int, private val spacing: Int, private val colorPaint: Int) : RecyclerView.ItemDecoration() {

private val paint = Paint().apply {

        color =colorPaint  // 设置分割线颜色

        strokeWidth =1f.dp.toFloat()// 分割线宽度

        style = Paint.Style.STROKE // 设置为描边模式

        isAntiAlias =true // 启用抗锯齿

    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {

val childCount = parent.childCount

        for (indexin 0 until childCount) {

val child = parent.getChildAt(index)

val params = child.layoutParams as RecyclerView.LayoutParams

val currentPosition = index +1

            if (currentPosition <=spanCount) {

if (currentPosition %spanCount ==1) {

// 左边的线

                    drawLeftLine(child, params, c)

// 右边的线

                    drawRightLine(child, params, c)

// 上面的线

                    drawTopLine(child, params, c)

// 下面的线

                    drawBottomLine(child, params, c)

}else if (currentPosition %spanCount ==2) {

// 右边的线

                    drawRightLine(child, params, c)

// 上面的线

                    drawTopLine(child, params, c)

// 下面的线

                    drawBottomLine(child, params, c)

}else if (currentPosition %spanCount ==0) {

// 右边的线

                    drawRightLine(child, params, c)

// 上面的线

                    drawTopLine(child, params, c)

// 下面的线

                    drawBottomLine(child, params, c)

}

}else {

// 每排最左侧和最右侧的左右边距设置成 20 像素, 其余 4 个边距一律设置成5

                if (currentPosition %spanCount ==1) {

// 左边的线

                    drawLeftLine(child, params, c)

// 右边的线

                    drawRightLine(child, params, c)

// 下面的线

                    drawBottomLine(child, params, c)

}else if (currentPosition %spanCount ==2) {

// 右边的线

                    drawRightLine(child, params, c)

// 下面的线

                    drawBottomLine(child, params, c)

}else if (currentPosition %spanCount ==0) {

// 右边的线

                    drawRightLine(child, params, c)

// 下面的线

                    drawBottomLine(child, params, c)

}

}

}

}

private fun drawLeftLine(child: View, params: RecyclerView.LayoutParams, c: Canvas) {

// 左边的线

        val left = child.left + params.leftMargin

        c.drawLine(left.toFloat(), child.top.toFloat() -spacing /3f, left.toFloat(), child.bottom.toFloat() +spacing /3f, paint)

}

private fun drawRightLine(child: View, params: RecyclerView.LayoutParams, c: Canvas) {

// 右边的线

        val right = child.right + params.rightMargin

        c.drawLine(right.toFloat(), child.top.toFloat() -spacing /3f, right.toFloat(), child.bottom.toFloat() +spacing /3f, paint)

}

private fun drawTopLine(child: View, params: RecyclerView.LayoutParams, c: Canvas) {

// 上面的线

        val top = child.top + params.topMargin

        c.drawLine(child.left.toFloat() -spacing /3f, top.toFloat(), child.right.toFloat() +spacing /3f, top.toFloat(), paint)

}

private fun drawBottomLine(child: View, params: RecyclerView.LayoutParams, c: Canvas) {

// 下面的线

        val bottom = child.bottom + params.bottomMargin

        c.drawLine(child.left.toFloat() -spacing /3f, bottom.toFloat(), child.right.toFloat() +spacing /3f, bottom.toFloat(), paint)

}

override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {

val index = parent.getChildAdapterPosition(view)

val currentPosition = index +1

        if (currentPosition <=spanCount) {

if (currentPosition %spanCount ==1) {

// 每排最左侧的边距

                outRect.left =spacing

                outRect.top =spacing

                outRect.right =spacing

                outRect.bottom =spacing

            }else if (currentPosition %spanCount ==2) {

outRect.left =0

                outRect.top =spacing

                outRect.right =spacing

                outRect.bottom =spacing

            }else if (currentPosition %spanCount ==0) {

outRect.left =0

                outRect.top =spacing

                outRect.right =spacing

                outRect.bottom =spacing

            }

}else {

if (currentPosition %spanCount ==1) {

outRect.left =spacing

                outRect.right =spacing

                outRect.bottom =spacing

            }else if (currentPosition %spanCount ==2) {

outRect.left =0

                outRect.right =spacing

                outRect.bottom =spacing

            }else if (currentPosition %spanCount ==0) {

outRect.left =0

                outRect.right =spacing

                outRect.bottom =spacing

            }

outRect.top =0

        }

}

}

相关文章

网友评论

      本文标题:RecyclerView 绘制表格

      本文链接:https://www.haomeiwen.com/subject/sjhwjjtx.html