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

前端 未结 26 2465
借酒劲吻你
借酒劲吻你 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:42

    I was facing same issue, java.lang.IndexOutOfBoundsException: Inconsistency detected.

    Create Custom LinearLayoutManager.

    HPLinearLayoutManager.java

    public class HPLinearLayoutManager extends LinearLayoutManager {
    
        public HPLinearLayoutManager(Context context) {
            super(context);
        }
    
        public HPLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }
    
        public HPLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        /**
         * Magic here
         */
        @Override
        public boolean supportsPredictiveItemAnimations() {
            return false;
        }
    }
    

    create instance of HPLinearLayoutManager.

    HPLinearLayoutManager hpLinearLayoutManager = new HPLinearLayoutManager(mContext);
    recyclerView.setLayoutManager(hpLinearLayoutManager);
    

    Hope this would help you.

    0 讨论(0)
  • 2020-12-01 01:42

    try to use a boolean flag, initialize it as false and inside OnRefresh method make it true, clear your dataList if flag is true just before adding the new data to it and after that make it false.

    your code might be like this

     private boolean pullToRefreshFlag = false ;
     private ArrayList<your object> dataList ;
     private Adapter adapter ;
    
     public class myClass extend Fragment implements SwipeRefreshLayout.OnRefreshListener{
    
     private void requestUpdateList() {
    
         if (pullToRefresh) {
            dataList.clear
            pullToRefreshFlag = false;
         }
    
         dataList.addAll(your data);
         adapter.notifyDataSetChanged;
    
    
     @Override
     OnRefresh() {
     PullToRefreshFlag = true
     reqUpdateList() ; 
     }
    
    }
    
    0 讨论(0)
  • 2020-12-01 01:42

    In my case the problem was me.

    My setup is a Recyclerview, Adapter & Cursor/Loader mechanism.

    At one point in my App the loader is destroyed.

    supportLoaderManager.destroyLoader(LOADER_ID_EVENTS)

    I was expecting the Recyclerview would display an empty list since i just deleted their datasource. What makes the error finding more complicated was, that the list was visible and the well known Exception occured only on a fling/scroll/animation.

    That cost me a few hrs. :)

    0 讨论(0)
  • 2020-12-01 01:43

    I recently ran into this issue and discovered my problem was I was modifying the adapter's data source on one loop/call stack and then calling notifyDataSetChanged on a subsequent loop/call stack. Between changing the data source and notifyDataSetChanged occurring, the RecyclerView was trying to fill in views due to the scrolling and noticed the adapter was in a weird state and justifiably threw this exception.

    Yigit Boyar explains over and over again two reasons why this crash would occur in your app:

    • You must be on the same call stack when you change your adapter source and notifyDataSetChanged()
    • You must be on the Main Thread when changing your adapter's data source

    If you're unsure how to debug this do, add the following Kotlin code where you change your adapter source and where you call notifyDataSetChanged

    Log.d("TEST", "isMainThread: ${Looper.myLooper() == Looper.getMainLooper()}")
    Log.d("TEST", Log.getStackTraceString(Exception("Debugging RV and Adapter")))
    
    0 讨论(0)
  • 2020-12-01 01:46

    Edit: The bug is fixed now, if you're still getting the same Exception, please make sure you're updating your Adapter data source only from the main thread and calling appropriate adapter notify method after it.

    Old answer: It seems to be a bug in RecyclerView, it's reported here and here. Hopefully it will be fixed in the next release.

    0 讨论(0)
  • 2020-12-01 01:46

    In my case it solved by changing mRecyclerView.smoothScrollToPosition(0) to

    mRecyclerView.scrollToPosition(0)
    
    0 讨论(0)
提交回复
热议问题