Home > database >  How to show or hide date textview when user scroll just like whatsapp chat android Kotlin
How to show or hide date textview when user scroll just like whatsapp chat android Kotlin

Time:10-11

Hey I am working on chat application. I completed my incoming and outgoing message from enter image description here enter image description here

CodePudding user response:

Where is the holder of TimingViewHolder?

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is IncomingViewHolder) {
            holder.bindItem(getItem(position))
        } else if (holder is OutGoingViewHolder) {
            holder.bindItem(getItem(position))
        }
    }

CodePudding user response:

even though it's not clear what do you mean by "this is not working in my solution" and I don't know if the problem is a crash or empty list or ... but the logic of checking items in methods areItemsTheSame and areContentsTheSame is wrong : the method areItemsTheSame is used to check if the identity of the old item is equal to the identity of the new item. and the method areContentsTheSame is used to check if values in 2 items are equal

so in areContentsTheSame you have to check only id of items because other things are values of item and what you have written in areContentsTheSame have no meaning beacause you are just checking the reference of the items. so change it to this :

   private val MESSAGE_COMPARATOR = object : DiffUtil.ItemCallback<MM>() {
        override fun areItemsTheSame(oldItem: MM, newItem: MM): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: MM, newItem: MM): Boolean {
   return ((oldItem.added == newItem.added)
                    && (oldItem.addedTime == newItem.addedTime)
                    && (oldItem.sentAt == newItem.sentAt)
                    && (oldItem.text == newItem.text))            }

    }
  • Related