1
votes

How can I create a sectioned recyclerview in Kotlin based on list, and sections need to be grouped by one of the subtype of Data model?

1

1 Answers

1
votes

You can have your ViewHolder be a sealed class with one child for each type of view you wanna display (subtypeA, subtypeB, maybe divider etc)

Then in your adapter you create the correct viewholder based on the viewType


override fun getItemViewType(position: Int): Int = when (getItem(position)) {
        is XXXUIModel.SubTypeA -> SUBTYPE_A
        is XXXUIModel.SubTypeB -> SUBTYPE_B
        is XXXUIModel.Divider -> DIVIDER
    }


override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): XXXAdapterViewHolder = when (viewType) {
        SUBTYPE_A -> XXXViewHolder.SubYypeA
        SUBTYPE_B -> XXXViewHolder.SubYypeB
        DIVIDER -> XXXViewHolder.Divider
        ....