【发布时间】:2021-11-07 06:29:36
【问题描述】:
我有一个 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.smoothScrollToPosition(listSize);当您的 recyclerview 更新时
-
你的意思是messages.size?如果是这样,它不起作用
-
是的,你在哪里执行这个?
-
在回收器视图适配器中,在 onBindViewHolder 内部
-
不,您需要在 Activity 或 ViewModel 中执行此操作,无论您要更新最终为您的 recyclerview 提供的 ArrayList 的内容
标签: android android-recyclerview scroll chat smooth-scrolling