【问题标题】:How to draw colored internal grid in Recyclerview with GridLayoutManager?如何使用 GridLayoutManager 在 Recyclerview 中绘制彩色内部网格?
【发布时间】:2019-12-03 10:54:54
【问题描述】:

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

向 ViewHolders 添加空间不是我的解决方案,因为我在 recyclerView 中使用了圆形背景,而在 viewHolders 中使用了背景会破坏它们。 Standart DividerItemDecoration 正是我所需要的,除了我只能选择水平线或垂直线,不能同时选择两者。 我找到了答案,这些答案通过getItemOffsets() 的间距给了我内部网格,但我无法用颜色填充矩形。

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

【问题讨论】:

    标签: android android-recyclerview gridlayoutmanager


    【解决方案1】:

    因此,我通过删除代码中水平和垂直的所有分隔来简化标准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()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2015-03-06
      相关资源
      最近更新 更多