我正在使用RecyclerView显示项目的名称.我的行包含单行TextView.项目名称存储在List<String> mItemList.
要更改内容RecyclerView,我替换字符串mItemList并调用notifyDataSetChanged()RecyclerViewAdapter.
但是如果我尝试更改mItemListRecyclerView滚动时的内容,有时它会给我
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 157(offset:157).state:588
如果尺寸mItemList小于之前,就会发生这种情况.那么改变内容的正确方法是RecyclerView什么?这是一个错误RecyclerView吗?
这是Exception的完整堆栈跟踪:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 157(offset:157).state:588
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3300)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3258)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1803)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1302)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1265)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1093)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:956)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:2715)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:524)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at …Run Code Online (Sandbox Code Playgroud) 我目前有一个回收站视图,其数据每5秒更新一次.要更新列表中的数据,我正在使用
notifyItemChanged(position);
notifyItemRangeChanged(position, mList.size());
Run Code Online (Sandbox Code Playgroud)
每次我调用notifyItemChanged()时,我的recycler视图上的项都会正确更新,但是它会闪烁,因为这会导致再次调用onBindViewHolder.因此,每次都是新鲜的负载.如果可能的话,我怎样才能防止这种情况发生?