如何使用GridLayoutManager在Recyclerview中绘制彩色内部网格?

问题描述 投票:0回答:1

我必须使用GridLayoutManager将内部彩色网格添加到我的RecyclerView中。

向ViewHolders添加空间不是我的解决方案,因为我在recyclerView中使用了四舍五入的背景,而在viewHolders中使用的背景却破坏了背景。Standart DividerItemDecoration完全满足了我的需求,但我只能选择水平或垂直线,而不能全部选择。我已经找到了通过getItemOffsets()通过间距给我内部网格的答案,但我无法对fillRect进行颜色填充。

实现此想法的最佳方法是什么?

android android-recyclerview gridlayoutmanager
1个回答
0
投票

因此,我通过删除代码中水平和垂直的所有分隔符来简化标准androidx.recyclerview.widget.DividerItemDecoration,从而完成了此任务。结果,我有内部水平线和垂直线。

应用装饰:

myRecyclerView.addItemDecoration(GridDividerItemDecoration(requireContext()))

整个装饰课:

class GridDividerItemDecoration(context: Context) : ItemDecoration() {
    private val mBounds = Rect()
    private var mDivider: Drawable?

    fun setDrawable(drawable: Drawable) {
        mDivider = drawable
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.layoutManager == null || mDivider == null) {
            return
        }
        drawVertical(c, parent)
        drawHorizontal(c, parent)
    }

    private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
        canvas.save()
        val left: Int
        val right: Int
        if (parent.clipToPadding) {
            left = parent.paddingLeft
            right = parent.width - parent.paddingRight
            canvas.clipRect(left, parent.paddingTop, right,
                    parent.height - parent.paddingBottom)
        } else {
            left = 0
            right = parent.width
        }
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            parent.getDecoratedBoundsWithMargins(child, mBounds)
            val bottom = mBounds.bottom + child.translationY.roundToInt()
            val top = bottom - mDivider!!.intrinsicHeight
            mDivider!!.setBounds(left, top, right, bottom)
            mDivider!!.draw(canvas)
        }
        canvas.restore()
    }

    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
        canvas.save()
        val top: Int
        val bottom: Int
        if (parent.clipToPadding) {
            top = parent.paddingTop
            bottom = parent.height - parent.paddingBottom
            canvas.clipRect(parent.paddingLeft, top,
                    parent.width - parent.paddingRight, bottom)
        } else {
            top = 0
            bottom = parent.height
        }
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            parent.layoutManager!!.getDecoratedBoundsWithMargins(child, mBounds)
            val right = mBounds.right + child.translationX.roundToInt()
            val left = right - mDivider!!.intrinsicWidth
            mDivider!!.setBounds(left, top, right, bottom)
            mDivider!!.draw(canvas)
        }
        canvas.restore()
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView,
                                state: RecyclerView.State) {
        if (mDivider == null) {
            outRect[0, 0, 0] = 0
            return
        }
        outRect[0, 0, 0] = mDivider!!.intrinsicHeight
        outRect[0, 0, mDivider!!.intrinsicWidth] = 0
    }

    companion object {
        private const val TAG = "DividerItem"
        private val ATTRS = intArrayOf(R.attr.listDivider)
    }

    init {
        val a = context.obtainStyledAttributes(ATTRS)
        mDivider = a.getDrawable(0)
        if (mDivider == null) {
            Log.w(TAG, "@android:attr/listDivider was not set in the theme used for this "
                    + "DividerItemDecoration. Please set that attribute all call setDrawable()")
        }
        a.recycle()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.