if your item is not more than 2 span, then you can achieve this with staggeredGridLayoutManager while working with multiple viewtype recyclerview, try initial span count to 2
val staggeredGridLayoutManager = StaggeredGridLayoutManager(
2, // initial span count
StaggeredGridLayoutManager.VERTICAL // Orientation
)
binding.recyclerView.layoutManager = staggeredGridLayoutManager
private fun bindHeader(item: DataModelMyCreate.Header, holder: DataAdapterViewHolder) {
val staggeredGridLayoutManager =
holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
staggeredGridLayoutManager.isFullSpan = true
itemView.findViewById<AppCompatTextView>(R.id.tvNameLabel)?.text = item.title
}
private fun bindHeaderTwo(
item: DataModelMyCreate.HeaderTwo,
holder: DataAdapterViewHolder
) {
val staggeredGridLayoutManager =
holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
staggeredGridLayoutManager.isFullSpan = false
itemView.findViewById<AppCompatTextView>(R.id.tvNameLabel)?.text = item.title
}
/////
suppose bindHeader has to show 1 span item count, you should make it isFullSpan = true, while bindHeaderTwo has to show 2 span count, then you have to make it isFullSpan = false.
its working i implemented this