RecyclerView更新时如何自动向下滚动

How to automatically scroll down when RecyclerView is renewed

我有一个 RecyclerView,我试图让它在每次弹出新消息时向下滚动。我很难这样做。我什至不确定我是否在正确的地方这样做。我在函数 onBindViewHolder() 内的 RecyclerView 适配器内执行此操作。这是代码:

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
}

我只是没有在正确的地方更新 recyclerview。当它实际上需要在我的 activity 或视图模型中更新时,我试图在 recyclerview 适配器内部更新它 - 我在其中更新了我的 messagesArrayList 的内容!解决者:Tendai(在问题的评论中)