You will need to do, define the view type for the item you are using.
data class MedicineItem(val name, val imageUrl, val viewType)
Then pass the List into your adapter
Override the getViewItemType() method in your adapter. As the design, I think you will need three type of view of Medicine item. The first one is the title, the second one is the normal item and the third one will be the Medicine item with an image.
You can define viewType value by yourself, for example, I just made
class Constants {
const val TITLE = 1
const val NORMAL_ITEM = 2
const val IMAGE_ITEM = 3
}
override fun getItemViewType(position: Int): Int {
val item : MedicineItem = list[position]
if(item.viewType == Constants.TITLE)
return Constants.TITLE
else if(item.viewType == Constant.NORMAL_ITEM)
return Constants.NORMAL_ITEM
else if(item.viewType == Constant.NORMAL_ITEM)
return Constants.IMAGE_ITEM
}
For the different layout for each view item, you need to create three layout(xml file) and three ViewHolder class for each type. Then define which layout will be displayed by.
First, in onCreatViewHolder(), decide which ViewHolder will be used for each view item. You can see the return value of viewType.
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
if(viewType == Constants.TITLE) {
return MedicineTitleViewHolder()
} else if(viewType == Constants.NORMAL_ITEM) {
return MedicineNormalViewHolder()
} else if(viewType == Constants.IMAGE_ITEM) {
return MedicineIamgeViewHolder()
}
}
Finally, now return the view holder of each item for recycling in onBindViewHolder(), as you can see the bind() method receive the single data, you can pass a MedicinItem data object for display.
override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
if (holder is MedicineTitleViewHolder) {
(holder as MedicineTitleViewHolder).bind(list[position])
} else if(holder as MedicineNormalViewHolder){
(holder as MedicineNormalViewHolder).bind(list[position])
} else if(viewType == Constants.IMAGE_ITEM) {
(holder as MedicineIamgeViewHolder).bind(list[position])
}
}