【发布时间】:2021-12-31 15:56:51
【问题描述】:
我正在回收站视图中显示一些从 WordpressAPI 获得的文章。为了触发重新加载,我使用了 SwipeRefreshLayout。当应用程序加载新文章时用户重新加载并向下滚动时,应用程序崩溃并且我收到以下错误
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionThomsLineArticleViewHolder{5c87efc position=2 id=-1, oldPos=-1, pLpos:-1 no parent}
我将文章存储在一个 Viewmodel 中,每包 10 个(也就是来自 Wordpress 的页面),这是我用来更新给定页面的代码:
fun setArticlePage(id: Int, content:ThomsLineWordpressArticlePage, recyclerAdapter: ThomsLineRecyclerAdapter){
//Only do Something if actually something changed
if (_articles.value == null || id >= _articles.value!!.size || (id < _articles.value!!.size && !_articles.value!!.get(id).equals(content))) {
//If there were no articles previously
if (_articles.value == null) _articles.value = arrayListOf(content)
//if the Page is completly new
else if (id >= _articles.value!!.size) _articles.value?.add(content)
//If it's just a old Page updating
else if (id < _articles.value!!.size) _articles.value?.set(id, content)
// Save the new Values
_articles.postValue(_articles.value)
//Update Recycler View
recyclerAdapter.notifyItemChanged(id)
}
}
编辑:我应该说当用户不滚动时没有错误,当用户在重新加载时滚动时应用程序会崩溃
【问题讨论】:
标签: android kotlin android-recyclerview