【问题标题】:how to make recycler view not scroll back to top after update更新后如何使回收站视图不滚动回顶部
【发布时间】:2021-04-20 00:00:54
【问题描述】:

我有数据的回收器视图,当它在底部使用此代码findLastCompletelyVisibleItemPosition() 时,它将加载新数据。我的初始化数据和我的加载数据是相同的功能,只是在 recyclerview 滚动到最底部后添加更改正文请求。我的问题是当它加载新数据时,它会滚动回顶部。如何让它在同一个地方而不是向后滚动?

我已经尝试在加载数据功能中实现这一点

val recyclerViewState =list_view_history_program.layoutManager?.onSaveInstanceState()
                if (loadMore > 0) {
                    listAdapter.notifyItemRangeChanged(loadMoreCount, listAdapter.itemCount, programNames)
                    listAdapter.notifyDataSetChanged()
                    list_view_history_program.layoutManager?.onRestoreInstanceState(recyclerViewState)
                    list_view_history_program.scrollToPosition(loadMoreCount)
                }

但它仍然向后滚动。请帮忙

【问题讨论】:

标签: android android-recyclerview android-adapter


【解决方案1】:

您已经使用了 notifyItemRangeChanged,然后使用了 notifyDataSetChanged。删除listAdapter.notifyDataSetChanged()

【讨论】:

    【解决方案2】:

    在 itemList 数组中添加项目后使用下面的代码
    注意:但您必须第一次调用适配器,然后第二次使用下面的代码

    adapter.notifyItemInserted(itemList.size());
    

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 2022-01-18
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多