title: Recyclerview IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
body: <p>I have a chat application in which when a new chat is created, there are no messages in the message list. When the user goes in the chat and the first message is sent. The app crashes with the following error code</p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{213d192 position=3 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent}
                                                                         at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
                                                                         at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
                                                                         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                         at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                         at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                         at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                         at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                         at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3534)
                                                                         at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
                                                                         at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                         at android.view.View.layout(View.java:16653)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                         at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:636)
                                                                         at android.view.View.layout(View.java:16653)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                         at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
                                                                         at android.view.View.layout(View.java:16653)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                         at android.view.View.layout(View.java:16653)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                         at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                                         at android.view.View.layout(View.java:16653)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                         at android.view.View.layout(View.java:16653)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                         at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                         at android.view.View.layout(View.java:16653)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                         at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                         at android.view.View.layout(View.java:16653)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                         at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2198)
                                                                         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1958)
                                                                         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
                                                                         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6050)
                                                                         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
                                                                         at android.view.Choreographer.doCallbacks(Choreographer.java:672)
                                                                         at android.view.Choreographer.doFrame(Choreographer.java:608)
                                                                         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5441)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
  </code></pre>
  
  <p>The following is my code for attaching the adapter to the recyclerview.</p>
  
  <pre><code>mMessageAdapter = new MessagesAdapter(currentConversationKey, this, this);
      conversationRecyclerView.setLayoutManager(messageLinearLayout);
      conversationRecyclerView.setItemAnimator(new DefaultItemAnimator());
      conversationRecyclerView.setAdapter(mMessageAdapter);
      mConversationRecyclerDataObserver = new RecyclerView.AdapterDataObserver() {
          @Override
          public void onChanged() {
              super.onChanged();
              Log.d(TAG, "Conversation Adapter is changed");
              checkIfMessagesInConversation();
          }
  
          @Override
          public void onItemRangeChanged(int positionStart, int itemCount) {
              super.onItemRangeChanged(positionStart, itemCount);
              Log.d(TAG, "Conversation Adapter item range changed");
              checkIfMessagesInConversation();
          }
  
          @Override
          public void onItemRangeInserted(int positionStart, int itemCount) {
              super.onItemRangeInserted(positionStart, itemCount);
              Log.d(TAG, "Conversation Adapter item range inserted");
              checkIfMessagesInConversation();
              int messageCount = mMessageAdapter.getItemCount();
              Log.d(TAG, "message Count is : "+ messageCount);
              int lastVisiblePosition = messageLinearLayout.findLastCompletelyVisibleItemPosition();
              Log.d(TAG, "Last Visible Position is : "+ lastVisiblePosition);
  
              Log.d(TAG, "positionStart is : "+ positionStart);
  
              Log.d(TAG, "Insert Item count  : "+ itemCount);
  
              if (lastVisiblePosition == -1 ||
                      (positionStart &gt;= (messageCount - 1) &amp;&amp; lastVisiblePosition == (positionStart - 1))) {
  
                  Log.d(TAG, "Scrolling to pos "+ (messageCount - 1));
                  try {
                      conversationRecyclerView.scrollToPosition(mMessageAdapter.getItemCount() - 1);
                  } catch (Exception e){
                      Log.d(TAG, e.toString());
                  }
              }
              if(isRefreshing){
                  onItemsLoadComplete();
              }
          }
  
          @Override
          public void onItemRangeRemoved(int positionStart, int itemCount) {
              super.onItemRangeRemoved(positionStart, itemCount);
              Log.d(TAG, "Conversation adapter item range removed");
              checkIfMessagesInConversation();
          }
  
          @Override
          public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
              super.onItemRangeMoved(fromPosition, toPosition, itemCount);
              Log.d(TAG, "Conversation Adapter item range moved");
              checkIfMessagesInConversation();
          }
      };
      mMessageAdapter.registerAdapterDataObserver(mConversationRecyclerDataObserver);
  </code></pre>
  
  <p>Please help</p>