Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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)

Updated

This is the adapter code.

public class PollAdapter extends RecyclerView.Adapter<PollAdapter.PollViewHolder> {

    // .... 

    @Override
    public void onBindViewHolder(@NonNull PollViewHolder holder, int position) {

    // ...

        if (holder.countDownTimer != null) {
            holder.countDownTimer.cancel();
        }

        // CountDown to set Timer in poll
        holder.countDownTimer = new CountDownTimer(<Timer to be finished.>, 1000) {

            public void onTick(long millisUntilFinished) {
                holder.time.setText("<Remaining time>");
            }

            public void onFinish() {
                removeAt(holder.getAdapterPosition());
            }

        }.start();
    }

    @Override
    public int getItemCount() {
        return pollList.size();
    }

    // To Remove Poll after Time finished
    public void removeAt(int position) {
    pollList.remove(position);
        notifyItemRemoved(position);
    }
}
like image 936
Moinkhan Avatar asked Jun 26 '18 05:06

Moinkhan


2 Answers

The issue may be related to the adapter. If you are using setHasStableIds(true); in the adapter constructor, you need to make sure that you are using stable ids.

Make sure that you override getItemId correctly in the adapter. It should be:

@Override
public long getItemId(int position) {
   return yourList == null ? 0 : yourList.get(position).getId();
}

And not:

@Override
public long getItemId(int position) {
     return position;
}
like image 136
Nermeen Avatar answered Sep 24 '22 07:09

Nermeen


Use custom 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

like image 32
Nikunj Paradva Avatar answered Sep 22 '22 07:09

Nikunj Paradva