问题描述
我的RecyclerView和ListAdapter出现问题.
I have an issue with my RecyclerView and ListAdapter.
通过API,我收到的物品从旧到新的升序.
Through an API, I receive items in a ascending order from older to newer.
然后通过调用submitList(items)方法刷新我的列表.
My list is then being refreshed by calling the submitList(items) method.
但是,由于所有内容都是异步的,因此在接收到第一个项目后,RecyclerView会保留在接收并显示的第一个项目的位置.由于在ListAdapter类中,当commitList()方法完成时没有回调,因此在更新到已添加的新项目之一之后,我找不到滚动的方法.
However, since everything is asynchronous, after receiving the first item, the RecyclerView remains on the position of the first item received and showed.Since in the ListAdapter class there is no callback when the submitList() method completed, I cannot find a way to scroll after the updates to one of the new items that has been added.
是否有一种方法可以在ListAdapter更新时进行拦截?
Is there a way to intercept when ListAdapter has been updated ?
推荐答案
不是特定于ListAdapter的,但是它仍然可以工作:
Not specific to ListAdapter, but it should work nonetheless:
只需使用 adapter.registerAdapterDataObserver(RecyclerView.AdapterDataObserver)
并覆盖相关的onItemXXX方法,以告知RecyclerView滚动到任意位置.
Just use adapter.registerAdapterDataObserver(RecyclerView.AdapterDataObserver)
and override the relevant onItemXXX method to tell your RecyclerView to scroll to whatever position.
例如:
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
(recycler_view.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(positionStart, 0)
}
})
这篇关于ListAdapter SubmitList()-如何滚动到开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!