I have similar situation and think it is a good choice to use kotlin: sealed class
. You may set any spanSizeLookup
for every item in your adapter list.
Example of setup spanSizeLookup
for GridLayoutManager
:
val spanCount = 2
val layoutManager = GridLayoutManager(context, spanCount)
layoutManager.spanSizeLookup = object : SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
val item = adapter.getItemList().getOrNull(position) ?: return spanCount
return when (item) {
is ProfileItem.Header -> spanCount
is ProfileItem.Status -> spanCount
is ProfileItem.Progress -> spanCount
is ProfileItem.Empty -> spanCount
is ProfileItem.Item -> spanCount / 2
is ProfileItem.Load -> spanCount / 2
}
}
}
My sealed class
:
sealed class ProfileItem {
object Header : ProfileItem()
data class Status(var content: UserItem.User?) : ProfileItem()
object Progress : ProfileItem()
object Empty : ProfileItem()
data class Item(val content: RecordItem.Record) : ProfileItem()
object Load : ProfileItem()
}