Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda