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

前端 未结 26 2524
借酒劲吻你
借酒劲吻你 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: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")))
    

提交回复
热议问题