RecyclerView: Inconsistency detected. Invalid item position. Cause -Removing item using timer
RecyclerView: Inconsistency detected. Invalid item position. Cause -Removing item using timer
Before moving ahead i search about this bug on google and i found the so many answer which are available but my scenario is different than their situation.
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 2(offset:2).state:3
I am using recyclerview to display the poll questions and every question has a timer, Item will removed from the list when timer runs out.
Exception is occurring when the timer runs out but in only some rare scenario when less time (for ex 100ms) is remaining. so in that case may be recyclerview is inflating item and at that same time, timer rans out and recyclerview try to remove that item.
Bug is when timer removing item i am getting the exception.
So I solved it after removing the elements from data set when 1 or less seconds is remaining for that poll. So it will not add item in list and that will run the timer.
If you want to produce the bug just start countdown timer in
bindView
and once the timer runs out remove that particular item.
You have to make timer below 500ms.
bindView
So now Everything works perfectly. Crashing on a rare scenario was solved but I don't want to remove the element from dataset even if less time is remaining. Please give me the proper solution for this bug.
Edited
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:1).state:2 com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager{182da0c VFED..... .F....ID 0,0-720,1024 #7f0a01a1 app:id/recycler_view}, adapter:com.lsjwzh.widget.recyclerviewpager.RecyclerViewPagerAdapter@c9403f, layout:android.support.v7.widget.LinearLayoutManager@6fdbd0c, context:com.bitpoll.polls.MainActivity@b40f4c0
android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5817)
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:606)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:894)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
android.widget.FrameLayout.onLayout(FrameLayout.java:321)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
android.widget.FrameLayout.onLayout(FrameLayout.java:321)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
android.view.View.layout(View.java:17969)
android.view.ViewGroup.layout(ViewGroup.java:5721)
android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
android.widget.FrameLayout.onLayout(FrameLayout.java:321)
com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
android.view.View.layout(View.java:17969)
This question has not received enough attention.
@Cheticamp added full stacktrace.
– Moinkhan
Jun 29 at 7:14
Is it possible for you to share your code?
– Rahul Shukla
23 hours ago
@RahulShukla there is no extra code. Just consider normal recycler view adapter. Extra part is i starting the timer in
onBindView
method and removing the element on it onFinished
method with calling notifyItemRemoved(pos)
. Everything work perfectly except, while i am adding element timer is below 1 second then it crashed.– Moinkhan
18 hours ago
onBindView
onFinished
notifyItemRemoved(pos)
@Moinkhan, well anything below 1sec is practically not going to visible to the user. Anyways, are you creating new timers for every item in onBindView()?
– Rahul Shukla
4 hours ago
4 Answers
4
This error comes when the Adapter
of the RecyclerView
is not being notified for a change.
Adapter
RecyclerView
To tackle this error, look for all the points where data is being changed in the underlying Adapter
there is no straight forward solution to this as this is an internal crash, thrown by the RecyclerView
Adapter
RecyclerView
What you could do is:
notifyItemAdded
notifyItemRemoved
To test this without any optimizations you can also call notifyDataSetChanged
both the time.
notifyDataSetChanged
I tried
notifyItemRemoved()
still getting that crash. But i haven't used notifyItemAdded()
i will try it and get back to you.– Moinkhan
Jun 26 at 7:27
notifyItemRemoved()
notifyItemAdded()
sure, remember you have to call it from both the places, adding and removing and best possible even for changing. The adapter should always have the most updated data. it fails sometime only because it fails when recyclerview checks for inconsistencies.
– MadScientist
Jun 26 at 7:29
there is no such a method as
notifyItemAdded()
.– Moinkhan
Jun 26 at 9:04
notifyItemAdded()
notifyItemInserted
not using an IDE while typing this, so name mismatches can occur.– MadScientist
Jun 26 at 9:06
notifyItemInserted
I tried
notifyItemRangeInserted
and notifyItemInserted
, but still i am getting the exception.– Moinkhan
Jun 26 at 11:41
notifyItemRangeInserted
notifyItemInserted
This Problem is mostly caused by position issue .
if u remove a postion or make any change call notifydatasetchanged then only the change will takes place.
simply if your remove a postion or clear a array if notydatasetchanged is not called the recyclerview will keep the postions init.
if there is an array of data, i just cleared the array and try to scroll recyclerview this issue will be shown because notydatasetchanged not called . (recycler view has postion but array no positon is found)
NB: sorry for my bad english
I am sorry but I already know notifyDatasetChnaged is used to update recyclerview. and i already did it.
– Moinkhan
Jun 26 at 6:17
@Moinkhan try to add holder.setIsRecyclable(false) in onBindViewHolder
Use custome LinearLayoutManager
LinearLayoutManager
public class CustLinearLayoutManager extends LinearLayoutManager {
public CustLinearLayoutManager(Context context) {
super(context);
}
public CustLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
// Something is happening here
@Override
public boolean supportsPredictiveItemAnimations() {
return false;
}
}
Use:-
CustLinearLayoutManager clm = new CustLinearLayoutManagerr(mContext);
recyclerView.setLayoutManager(clm);
I hope it ll works
I tried it. Not working.
– Moinkhan
18 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are doing any manipulations on a non-UI thread? Posting the full stack trace may help.
– Cheticamp
Jun 28 at 15:03