Home > database >  RecyclerView Crashing when scrolling while refreshing
RecyclerView Crashing when scrolling while refreshing

Time:11-22

I'm displaying some Articles I got from a WordpressAPI in a recycler View. To trigger a reload I'm using a SwipeRefreshLayout. When the user reloads and scrolls down while the App is loading new Articles, the App crashes and I get the following error

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionThomsLineArticleViewHolder{5c87efc position=2 id=-1, oldPos=-1, pLpos:-1 no parent}

I store the Articles in Packages of 10 (aka the Pages from Wordpress) in a Viewmodel, here is the Code I use to update a given page:

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)
        }
    }

EDIT: I should probably say that there is no error when the user doesn't scroll, the App just crashes when the user is scrolling while reloading

CodePudding user response:

Inconsistency occurs when you try to refresh(recyclerAdapter.notifyItemChanged(id)) recycler view while its last update operation is not finished yet.

There're few things not clear from question. Are you calling setArticlePage method inside loop ? Then you should refresh recycler view after loop is finished.

In additions to this, Try to load New Articals one page at time. Don't allow to load new artical page untill last call to API is finished.

CodePudding user response:

I'm sorry, the Error was't actually with the RecyclerView it self, but with other Parts of my code: I was always loading pages to a given point, an all Pages after that were removed, which ment that while loading the new Pages, the Values the RecyclerView wanted to access where actually not loaded.

Sorry for stealing your time

  • Related