Skip to content

Instantly share code, notes, and snippets.

@amalhanaja
Created September 15, 2018 14:56
Show Gist options
  • Save amalhanaja/0be229b3612266d95c82d55cb40fd497 to your computer and use it in GitHub Desktop.
Save amalhanaja/0be229b3612266d95c82d55cb40fd497 to your computer and use it in GitHub Desktop.
ItemOffsetDecorator for RecyclerView
class ItemOffsetsDecorator(
private val block: (position: Int, count: Int, rect: Rect) -> Unit
): RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
super.getItemOffsets(outRect, view, parent, state)
parent?.getChildAdapterPosition(view)?.run position@ {
outRect?.run rect@ {
state?.itemCount?.run count@ {
block.invoke(this@position, this@count, this@rect)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment