Recyclerview : java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position

Viewed 279

im made a dynamic nested recyclerview with vertical rows, and horizontal buttons. it works fine when i insert rows with equal number of buttons in them, or with small "jumps" (for example: [1,2,3,4,5] or [5,5,5,5] are ok) enter image description here

but if i insert big differences in the number of buttons in each row (for example [9,4,4,4]) enter image description here

and scroll horizontally, it craches with the error "IndexOutOfBoundsException: Inconsistency detected.."

  1. I tryied to make new LinearLayoutManager and override the method supportsPredictiveItemAnimations(), and set to fale --> did not work
  2. I tryied to put mRecyclerView.getRecycledViewPool().clear(); before .notifyDataSetChanged(); --> did not work i call a horizontal adapter from vertical adapter:

'''

   @Override
public void onBindViewHolder(@NonNull WorkoutInterfacesAdapter.WorkoutInterfaceViewHolder holder, int position) {
    reps.clear();//clears the arraylist (right now its void)
    weights.clear();//clears the arraylist (right now its void)
    reps = stringToArraylist(workout.numberOfReps);// this method provides "reps" with each set numbers. for example: reps = [5,4,3,2]
    //each "position" variable, is reps's index.

    weights = stringToArraylist(workout.weights);//this method provides "weights" wit each set's weight. for example [100,110,120]
    try {
        holder.numberOfSets.setText("Set No#" + (position + 1) + ": " + weights.get(position));

        repsForEachSet = setCurrentRepsArray(reps.get(position));//this method makes arraylist for each set. for eaxmple: for set 1, with 5 reps, the arraylist
        //would be[1,2,3,4,5]. for set two with 4 reps, the arraylist would be[1,2,3,4]

    } catch (Exception e) {
        holder.numberOfSets.setText("Set No#" + (position + 1));
    }

    //here we set the new horizontal  recyclerview, that would get the repsForEachSet with the current set's repetitions.
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
    holder.ChildRV.setLayoutManager(layoutManager);
    holder.ChildRV.setHasFixedSize(true);
    WorkoutInterfaceAdapter_Child workoutInterfaceAdapter_child = new WorkoutInterfaceAdapter_Child(mContext, repsForEachSet, workout, position);
    holder.ChildRV.setAdapter(workoutInterfaceAdapter_child);
    workoutInterfaceAdapter_child.notifyDataSetChanged();

'''

Stack trace:

'''

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.workouttracker, PID: 5962
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 7(offset:7).state:8 androidx.recyclerview.widget.RecyclerView{5e9028a VFED..... ........ 0,0-1054,183 #7f09007a app:id/child_recyclerview}, adapter:com.example.workouttracker.WorkoutInterfaceAdapter_Child@3b850fb, layout:androidx.recyclerview.widget.LinearLayoutManager@b492518, context:com.example.workouttracker.WorkoutInterface@ab3f88d
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6364)
    at androidx.recyclerview.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:288)
    at androidx.recyclerview.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:345)
    at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:361)
    at androidx.recyclerview.widget.GapWorker.prefetch(GapWorker.java:368)
    at androidx.recyclerview.widget.GapWorker.run(GapWorker.java:399)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

'''

any ideas or workarounds wold be welcome. thanks

0 Answers
Related