Skip to content

Instantly share code, notes, and snippets.

@DarrenAtherton49
Last active September 14, 2020 15:32
Show Gist options
  • Save DarrenAtherton49/9ac98b4aad9c081613869d94ba4ed7d4 to your computer and use it in GitHub Desktop.
Save DarrenAtherton49/9ac98b4aad9c081613869d94ba4ed7d4 to your computer and use it in GitHub Desktop.
Exclude certain Groupie Items from spacing item decoration
recyclerView.addItemDecoration(
LinearSpacingItemDecoration(
spacingInPixels = resources.getDimensionPixelSize(R.dimen.item_spacing),
excludeItem = { item ->
item is RadioButtonItem || item is CheckBoxItem
}
)
)
class LinearSpacingItemDecoration(
private val spacingInPixels: Int,
private val orientation: Orientation = Orientation.Vertical,
private val excludeItem: ((item: Item) -> Boolean)? = null
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val adapterPosition = parent.getChildAdapterPosition(view)
if (adapterPosition <= 0) {
return
}
if (excludeItem != null) {
val adapter = parent.adapter as GroupAdapter
val item = adapter.getItem(adapterPosition) as Item
if (excludeItem.invoke(item)) {
return
}
}
when (orientation) {
is Orientation.Horizontal -> outRect.left = spacingInPixels
is Orientation.Vertical -> outRect.top = spacingInPixels
}
}
sealed class Orientation {
object Horizontal : Orientation()
object Vertical : Orientation()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment