【问题标题】:How to automatically scroll down when RecyclerView is renewedRecyclerView更新时如何自动向下滚动
【发布时间】: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


【解决方案1】:

我只是没有在正确的位置更新回收站视图。当它实际上需要在我的活动或视图模型中更新时,我试图在 recyclerview 适配器内部更新它 - 我在其中更新了我的 messagesArrayList 的内容!解决者:Tendai(在问题的 cmets 中)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多