I have a RecyclerView, and I'm trying to get it to scroll down every time a new message pops up. I struggle to do so. I'm not even sure If I am doing it in the right place. I'm doing it inside of my RecyclerView adapter inside of the function onBindViewHolder()
. Here is the code:
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
holder.itemView.apply {
tvTitle.text = messages[position].title //here I am setting the title - the name of the person who wrote the message
tvMessage.text = messages[position].message //here I am setting the message - the message written by the person
rvTodos.smoothScrollToPosition(itemCount - 1) //and here, at last, I am setting the my recyclerview(rvTodos) to smoothly scrool down to the itemCount position (the size of the messages array)
}
}
override fun getItemCount(): Int {
return messages.size
}
I was simply not updating the recyclerview in the correct place. I was attempting to update it inside of the recyclerview adapter when it actually needs to be updated inside of my activity or viewmodel - where I update the contents of my messagesArrayList! Solved by: Tendai (in the comments of the question)