Recyclerview and Inconsistency detected crash

Posted By : Ajit Jati | 03-Dec-2017

If you have ever worked with Recyclerview in android with infinite scrolling feature, you might came across one exception that hasn't an easy fix.

The exception generally happens when you change/update your dataset rapidly while the layout(UI) is updating. Another reason can be the scrolling and updating the adapter at the same time.

The exception looks like this :
 

E/AndroidRuntime( 7546): java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 2(offset:2).state:3
    >E/AndroidRuntime( 7546):   at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3382)
    >E/AndroidRuntime( 7546):   at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
    >E/AndroidRuntime( 7546):   at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
    >E/AndroidRuntime( 7546):   at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
    >E/AndroidRuntime( 7546):   at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
    >E/AndroidRuntime( 7546):   at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
    >E/AndroidRuntime( 7546):   recycler_view.RecyclerViewContainer$LiLinearLayoutManager.onLayoutChildren(RecyclerViewContainer.java:179)
    >E/AndroidRuntime( 7546):   at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1942)
    >E/AndroidRuntime( 7546):   at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
    >E/AndroidRuntime( 7546):   at org.liboid.recycler_view.LiRecyclerView.onLayout(LiRecyclerView.java:30)
    >E/AndroidRuntime( 7546):   at android.view.View.layout(View.java:14946)
    >E/AndroidRuntime( 7546):   at android.view.ViewGroup.layout(ViewGroup.java:4651)
    >E/AndroidRuntime( 7546):   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    >E/AndroidRuntime( 7546):   at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    >E/AndroidRuntime( 7546):   at android.view.View.layout(View.java:14946)
    >E/AndroidRuntime( 7546):   at android.view.ViewGroup.layout(ViewGroup.java:4651)
    >E/AndroidRuntime( 7546):   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    >E/AndroidRuntime( 7546):   at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        

In order to solve the above you could follow below solutions. Please note i am posting a number of solutions to try out as the above issue can be there due to one of many reasons.

  • Overridding the layoutmanager class to add below 
    @Override
    public boolean supportsPredictiveItemAnimations() {
        return false;
    }     
  • Using notifyDataSetChanged() instead of other adapter update methods.
  • Another solution is try to avoid updating the recyclerview items while its in scroll.You can use onScrollChanged method to detect the scroll state
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:
                        isScrolling = false;
                     
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        isScrolling = true;
                        break;
                }
            }


        
  • Use Android's DiffUtil class to update recyclerview's items smartly. It handles all the updation complexity internally and uses best method to update UI depending on your new and old list. You can read more about DiffUtils here .

Hope that helps, cheers!

About Author

Author Image
Ajit Jati

Ajit is an proficient Project Manager specializing in Mobile technologies. He possesses extensive development experience in Titanium, Android, Kotlin, Roku, and PHP. Ajit has successfully delivered various projects in the OTT, AR/VR, and Travel industries. His skill set includes developing and maintaining project plans, schedules, and budgets, ensuring timely delivery while staying within the allocated budget. He excels in collaborating closely with clients to define project scope and requirements, establish project timelines and milestones, and effectively manage expectations. He conducts regular project status meetings, ensuring effective communication and providing updates to clients and stakeholders on project progress, risks, and issues. Furthermore, Ajit takes on the role of a coach and mentor for team,offering guidance on project management best practices and assisting in their skill development.

Request for Proposal

Name is required

Comment is required

Sending message..