重温RecyclerView.ItemDecoration

主要知识点:
1 重写getItemOffsets

 override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.set(100,50,10,10)
    }

outRect.set(100,50,10,10) 四个值分别对应左上右下四个位置 代表item相对自己位移的位置,也就是留给我们可以自由绘制的地方
2 重写onDraw

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        var childCount = parent.childCount
        for(i in 0..childCount-1){
          var child = parent.getChildAt(i)
            var left = child.left -50
            var top = child.top

            var rect = Rect(left, top,left+100,top+100)
            c.drawBitmap(bitmap,null,rect,paint);

            c.drawText("left", left.toFloat(), top.toFloat(),paint)
        }
    }

可以根据自己的需要 根据item的index 绘制自己想要展示的内容

你可能感兴趣的:(重温RecyclerView.ItemDecoration)