Skip to content

Instantly share code, notes, and snippets.

@wassim93
Last active December 16, 2021 21:01
Show Gist options
  • Save wassim93/c1fb0b59235f2a2bb34e5e2f90dcef1c to your computer and use it in GitHub Desktop.
Save wassim93/c1fb0b59235f2a2bb34e5e2f90dcef1c to your computer and use it in GitHub Desktop.
Adding item decorator class to set space between recycler view items
// create a new class for the custom decorator
class MarginItemDecorator(private val horizontalSpacing:Int , private val verticalSpacing:Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
outRect.right = horizontalSpacing
outRect.left = horizontalSpacing
outRect.top = verticalSpacing
outRect.bottom = verticalSpacing
}
}
// call in your activity or fragment
recycler_list.addItemDecoration(MarginItemDecorator(5,5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment