Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Load recyclerview different types adapter with month header

Trying to show month name as header if it get different month name from api. Here I do load month header but when I load header it doesn't load other view. I need it load month as header if it different in data and also load other view holder.

I saw some examples they all use groupItem inside Model class. I can not change Model class now. If can solve this without it.

ReportsAdapter.kt

    class ReportsAdapter(
        private val onClickListener: (TimelineVisit) -> Unit
    ) : ListAdapter<TimelineVisit, RecyclerView.ViewHolder>(
        DiffUtil()
    ) {
    
        private var month: String = ""
    
        inner class ReportsViewHolder(private val binding: ItemVisitBinding) : RecyclerView.ViewHolder(
            binding.root
        ) {
            fun bind(item: TimelineVisit, onClickListener: (TimelineVisit) -> Unit) {
                binding.apply {
                    linearLayoutPaymentStatus.visibility = View.GONE
                    imgFeedback.visibility = View.GONE
                    var text = item.visitFrom
    
                    root.setOnClickListener { onClickListener.invoke(item) }
                }
            }
        }
    
        inner class MonthHeaderViewHolder(val binding: ItemMonthNameBinding, val bindingVisit: ItemVisitBinding) : RecyclerView.ViewHolder(
            binding.root
        ) {
            fun bind(month: String, item: TimelineVisit, onClickListener: (TimelineVisit) -> Unit) {
                bindingVisit.txtMonthName.text = month
 binding.apply {
                    linearLayoutPaymentStatus.visibility = View.GONE
                    imgFeedback.visibility = View.GONE
                    var text = item.visitFrom
    
                    root.setOnClickListener { onClickListener.invoke(item) }
                }
            }
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return when (viewType) {
                R.layout.asm_item_month_name -> {
                    val monthView = ItemMonthNameBinding.inflate(LayoutInflater.from(parent.context), parent, false)
 val mView = ItemVisitBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                MonthHeaderViewHolder(monthView, mView)
                }
                R.layout.asm_item_visit -> {
                    val mView = ItemVisitBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                    ReportsViewHolder(mView)
                }
                else -> {
                    val mView = ItemVisitBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                    ReportsViewHolder(mView)
                }
            }
        }
    
        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            when (holder) {
                is ReportsAdapter.MonthHeaderViewHolder -> holder.bind(month, getItem(position), onClickListener )
                is ReportsAdapter.ReportsViewHolder -> holder.bind(getItem(position), onClickListener)
            }
        }
    
        override fun getItemViewType(position: Int): Int {
            //if month get different month will load monthHeaderViewHolder
    
            val currentMonth = getItem(position).visitFrom
            return if (month != convertCurrentMonth) {
                month = convertCurrentMonth
                R.layout.item_month_name
            } else {
                R.layout.item_visit
            }
        }
    }
    
    class DiffUtil : DiffUtil.ItemCallback<TimelineVisit>() {
        override fun areItemsTheSame(oldItem: TimelineVisit, newItem: TimelineVisit): Boolean {
            return oldItem.profileId == newItem.profileId
        }
    
        override fun areContentsTheSame(oldItem: TimelineVisit, newItem: TimelineVisit): Boolean {
            return oldItem == newItem
        }
    }
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!