title: Android RecyclerView IndexOutOfBoundsException Inconsistency detected. Invalid view holder adapter positionViewHolder
body: <p>I have a recyclerView filled by sessios of an event in some countries.i have a spinner in which the user choose the country then load data from api and display it in recyclerView. Adding the data to recyclerView and refresh it when user change the country works perfeclty.i want to add a simple code, if the session is on going now then the recyclerView has to scroll to the position of the session, it does it but when it does it if i changes the country i get this error </p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{3f69195a position=3 id=-1, oldPos=-1, pLpos:-1 no parent} androidx.recyclerview.widget.RecyclerView{870f167 GFED.... ......ID 0,417-860,570 #7f0800a3 app:id/mEA_recyclerView}, adapter:is.com.raisse.companionfornite.EventSessionsRecyclerViewAdapter@1e4e5b14, layout:is.com.raisse.companionfornite.EventsActivity$1@c950fbd, context:is.com.raisse.companionfornite.EventsActivity@706582b
      at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)
      at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)
      at androidx.recyclerview.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:286)
      at androidx.recyclerview.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:343)
      at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:359)
      at androidx.recyclerview.widget.GapWorker.prefetch(GapWorker.java:366)
      at androidx.recyclerview.widget.GapWorker.run(GapWorker.java:397)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:145)
      at android.app.ActivityThread.main(ActivityThread.java:6934)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
  </code></pre>
  
  <p>to scroll to the event that is live i used this code </p>
  
  <pre><code>recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false) {
          @Override
          public void onLayoutCompleted(RecyclerView.State state) {
              super.onLayoutCompleted(state);
              Log.d("position===&gt;", "" + mPositionEvent);
              scrollRecycler();
          }
      });
  
      private void scrollRecycler() {
      try {
          LinearSmoothScroller smoothScroller = new LinearSmoothScroller(this) {
              @Override
              protected int getHorizontalSnapPreference() {
                  return LinearSmoothScroller.SNAP_TO_START;
  
              }
          };
          smoothScroller.setTargetPosition(mPositionEvent);
          recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>The variable mPositionEvent is static i change it from the recyclerViewAdapter in onBindViewHolder</p>
  
  <pre><code>@Override
  public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
      //...
      //some code here works perfeclty
      try {
          Date dateStart = format.parse(eventTimeStart);
          Date dateEnd = format.parse(eventTimeEnd);
          Date dateCurrent = format.parse(currentTime);
  
          long longStart = dateStart.getTime();
          long longEnd = dateEnd.getTime();
          long current = dateCurrent.getTime();
  
          if (current&gt;=longStart &amp;&amp; current&lt;longEnd){
              holder.txtStatus.setText("Live");
              holder.txtStatus.setTextColor(Color.RED);
              holder.viewS.setBackground(ContextCompat.getDrawable(context,R.drawable.parallelogram_view_live));
              EventsActivity.mPositionEvent = position;
          }
  
          if(current&gt;=longEnd){
              holder.txtStatus.setText("ENDED");
          }
  
      } catch (ParseException e) {
          e.printStackTrace();
      }
  
  
  }
  </code></pre>
  
  <p>what i notice is when the position is 0 so there is no event live i don't get any error </p>