I have a bug RecyclerView where items are not coming as expected. RecyclerView is using multiple ViewType (Category and items) but Currently, The adapter is printing items like this. (The Category & Items doesn't seems to be mapped correctly)
Category 1
Category 2
Category 3
- item
- item
- item
- item
- item
- item
I want the adapter to print items like this
Category 1
- item
- item
Category 2
- item
- item
- item
Category 2
- item
My adapter looks like this
class MyProfileAdapter(private val context: Context, private val list: List<HelpResourceModel>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val VIEW_TYPE_RESOURCE_CATEGORY = 1
const val VIEW_TYPE_RESOURCE_ITEM = 2
}
private val resourceCategories = list.groupBy { it.ProfileCategory }.keys.toList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
VIEW_TYPE_RESOURCE_CATEGORY -> {
val binding = HelphulCategoryRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
ProfileCategoryViewHolder(binding)
}
VIEW_TYPE_RESOURCE_ITEM -> {
val binding = ItemMyProfileAdapterBinding.inflate(LayoutInflater.from(parent.context), parent, false)
ViewHolder(binding)
}
else -> throw IllegalArgumentException("Invalid view type")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
VIEW_TYPE_RESOURCE_CATEGORY -> {
val viewHolder = holder as ProfileCategoryViewHolder
viewHolder.bind(resourceCategories[position])
}
VIEW_TYPE_RESOURCE_ITEM -> {
val viewHolder = holder as ViewHolder
val resource = getItemByPosition(position)
viewHolder.bind(resource)
}
}
}
override fun getItemCount() = list.size + resourceCategories.size
override fun getItemViewType(position: Int): Int {
return if (isProfileCategory(position)) {
VIEW_TYPE_RESOURCE_CATEGORY
} else {
VIEW_TYPE_RESOURCE_ITEM
}
}
private fun isProfileCategory(position: Int): Boolean {
return position in resourceCategories.indices
}
private fun getItemByPosition(position: Int): HelpResourceModel {
return list[position - resourceCategories.size]
}
inner class ViewHolder(val binding: ItemMyProfileAdapterBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(model: HelpResourceModel) {
binding.apply {
tvTitle.text = model.title
tvType.text = context.getString(if (model.getType() == HelpResourceTypeEnum.FAQ) R.string.faq else R.string.resource)
tvType.background = (ContextCompat.getDrawable(context, if (model.getType() == HelpResourceTypeEnum.FAQ) R.drawable.dark_blue_30_top_plan_box else R.drawable.orange_30_top_plan_box))
tvType.setTextColor(ContextCompat.getColor(context, if (model.getType() == HelpResourceTypeEnum.FAQ) R.color.darkBlue else R.color.orange))
}
}
}
inner class ProfileCategoryViewHolder(val binding: HelphulCategoryRowBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(ProfileCategory: String) {
binding.category.text = ProfileCategory
}
}
}
My HelpResourceModel looks like this
@Keep
@Parcelize
data class MyProfileModel(
val title: String = "",
val contents: String = "",
val resourceType: String = "",
val persona: String = "",
val resourceCategory: String = "",
) : Parcelable {
fun getType(): MyProfileTypeEnum {
return when (resourceType) {
MyProfileTypeEnum.FAQ.name -> MyProfileTypeEnum.FAQ
MyProfileTypeEnum.ResourceArticle.name,
MyProfileTypeEnum.RESOURCE.name -> MyProfileTypeEnum.RESOURCE
else -> MyProfileTypeEnum.FAQ
}
}
}
Not sure what I am doing wrong here