Inconsistency detected in RecyclerView, How to change contents of RecyclerView while scrolling

前端 未结 26 2522
借酒劲吻你
借酒劲吻你 2020-12-01 00:59

I\'m using RecyclerView to display name of the items. My row contains single TextView. Item names are stored in List mItemList<

26条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 01:39

    The problem is definitely not because of recyclerview scrolling, but it is related to notifyDataSetChanged(). I had a recycler view in which i was constantly changing data i.e. adding and removing data. I was calling notifyDataSetChanged() everytime I was adding items to my list, But was not refreshing the adapter whenever the item is being removed or the list was cleared.

    So to fix the :

    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 2(offset:2).state:12 at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5456)
    

    I called adapter.notifyDataSetChanged() after list.clear(), wherever it was required.

    if (!myList.isEmpty()) {
            myList.clear();
            myListAdapter.notifyDataSetChanged();
        }
    

    Since then, I never encountered the exception. Hope it works out the same for others as well. :)

提交回复
热议问题