Recycler adapter using diffutil , unable to see newly added item user need to scroll down

39 views Asked by At

Adapter class, with DiffUtil implementation

class MyAdapter(private val context: Context, private val onItemClickListener: 
(id:String)->Unit) :ListAdapter<SMS, MyAdapter.ViewHolder>(MyDiffCallback()) {



override fun onCreateViewHolder(parent: ViewGroup, viewType: 
  Int):ViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.list_view, parent, false)

    return ViewHolder(view)
}



override fun onBindViewHolder(holder:  ViewHolder, position: Int) {

when(holder) {

    is ViewHolder -> {
        holder.bind(getItem(position),context, onItemClickListener)
    }

  }
}

inner class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
    private val msg = view.tvMsg

    fun bind(
        sms: SMS, context: Context,
        onContactItemClickListener:(id:String)->Unit ) {
    msg.text = sms.msgString

    }

 }
class MyDiffCallback : DiffUtil.ItemCallback<SMS>() {
    override fun areItemsTheSame(oldItem: SMS, newItem: SMS): Boolean {
      
        return oldItem.time == newItem.time

    }

    override fun areContentsTheSame(oldItem: SMS, newItem: SMS): Boolean {
        return oldItem.time == newItem.time && oldItem.msgString == newItem.msgString
    }

}

}

Oncreate() method in activity where I set the new list/livedata to the adapter

  viewModel.data.observe(this, Observer { data->
        data.let {
                  adapter.submitList(it)
                 }
   }

The problem is I have to scroll to see new item that is being added to the list. how can I make sure that new item is visible without scrolling, like we see in whatsapp chat.

0

There are 0 answers