개발일지

Android in A..Z - RecyclerView (ItemDecoration) 본문

Android (안드로이드)/RecyclerView

Android in A..Z - RecyclerView (ItemDecoration)

강태종 2021. 1. 10. 17:54

ItemDecoration

RecyclerView의 ViewHolder를 꾸미는 역할을 한다. ViewHolder간의 동일한 여백을 주거나, 구분선을 넣기, RecyclerView Background 이미지 주기 등 다양한 효과를 줄 수 있다.


GridSpacingItemDecoration

여백을 주는 ItemDecoration이다.

GridSpacingItemDecoration

class GridSpacingItemDecoration(private val spanCount: Int, private val spacing: Int, private val includeEdge: Boolean = true) : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)

        val position = parent.getChildAdapterPosition(view)

        val column = position % spanCount
        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount
            outRect.right = (column + 1) * spacing / spanCount
            outRect.bottom = spacing
            if (position < spanCount) {
                outRect.top = spacing
            }
        } else {
            outRect.left = column * spacing / spanCount
            outRect.right = spacing - (column + 1) * spacing / spanCount
            if (position >= spanCount) {
                outRect.top = spacing
            }
        }
    }

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

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
    }
}
  • onDraw : ViewHolder가 그려지기 전에 호출된다. => ViewHolder 아래에 보여지게 된다.
  • onDrawOver : ViewHolder가 그려진 후에 호출된다. => ViewHolder 위에 보여지게 된다.
  • getItemOffsets : ViewHolder에 margin을 주는 함수이다.

DivisionItemDecoration

구분선을 넣는 ItemDecoration이다.

DivisionItemDecoration

class DivisionItemDecoration(private val colorString: String, private val margin: Int) : RecyclerView.ItemDecoration() {
    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)

        val paint = Paint().apply {
            color = Color.parseColor(colorString)
        }
        val height = 1.toDp()

        for (i in 0 until parent.childCount) {
            val child = parent.getChildAt(i)
            if (i != parent.childCount - 1) {

                c.drawRect(child.left.toFloat() + margin, child.bottom.toFloat() + margin, child.right.toFloat() - margin, child.bottom.toFloat() + height + margin, paint)
            }
        }
    }
}

적용

binding.recyclerView.adapter = adapter.apply { submitList(list) }
binding.recyclerView.addItemDecoration(GridSpacingItemDecoration(1, 10.toDp()))
//binding.recyclerView.addItemDecoration(DivisionItemDecoration("#FFFFFF", 5.toDp()))

Git (예제코드)

github.com/KangTaeJong98/Example/tree/main/Android/RecyclerView

 

KangTaeJong98/Example

My Example Code. Contribute to KangTaeJong98/Example development by creating an account on GitHub.

github.com

 

Comments