Last active
September 14, 2020 15:32
-
-
Save DarrenAtherton49/9ac98b4aad9c081613869d94ba4ed7d4 to your computer and use it in GitHub Desktop.
Exclude certain Groupie Items from spacing item decoration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
recyclerView.addItemDecoration( | |
LinearSpacingItemDecoration( | |
spacingInPixels = resources.getDimensionPixelSize(R.dimen.item_spacing), | |
excludeItem = { item -> | |
item is RadioButtonItem || item is CheckBoxItem | |
} | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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