Add margins to divider in RecyclerView

后端 未结 8 887
深忆病人
深忆病人 2020-12-14 01:11

i am building an android app which is using RecyclerView. I want to add dividers to RecyclerView, which I did using this code:

Divi         


        
8条回答
  •  無奈伤痛
    2020-12-14 01:31

    Here is a simple Kotlin code snippet to implement ItemDecoration with RecyclerView:

     recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
                    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                        outRect.left = 20    // Left Margin.
                        outRect.right = 20   // Right Margin.
                        outRect.top = 16     // Top Margin.
                        outRect.bottom = 16  // Bottom Margin.
                    }
                })
    

    Explantation:- In the above example code, we are adding margins to each item of RecyclerView from all four directions.

    Happy Coding...

提交回复
热议问题