title: RecyclerView Inconsistency detected. Invalid item position
body: <p>This might look like a repeated question but it's not!</p>
  
  <p>I get this rare crash on my RecyclerView </p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: 
  Inconsistency detected. 
  Invalid item position 1(offset:1).state:4 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5014).
  </code></pre>
  
  <p>I am pretty sure that I am not missing any notification to my adapter after data change!.</p>
  
  <p>Apart from the crash, one thing I can't get my head around is that I am using android.support.v7.widget.RecyclerView and the crash is at line 5014  in that java file</p>
  
  <p><strong>CODE FROM RecyclerView.java where my crash was happening</strong></p>
  
  <pre><code> if (offsetPosition &lt; 0 || offsetPosition &gt;= mAdapter.getItemCount()) {
              throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item "
                      + "position " + position + "(offset:" + offsetPosition + ")."
                      + "state:" + mState.getItemCount());
          }
  </code></pre>
  
  <p>so for this to crash either </p>
  
  <pre><code>offsetPosition &lt; 0
  </code></pre>
  
  <p>or</p>
  
  <pre><code>offsetPosition &gt;= mAdapter.getItemCount()
  </code></pre>
  
  <p>from the crash log clearly, my offsetPosition is 1 so</p>
  
  <pre><code>offsetPosition &gt;= mAdapter.getItemCount()
  </code></pre>
  
  <p>has to be true!! but in my adapter, I implemented getItemCount like this</p>
  
  <pre><code>@Override
  public int getItemCount() {
      return list.size() + 3;
  }
  </code></pre>
  
  <p>so at least getItemCount should return 3 so 2nd statement also can't be true.</p>
  
  <p>full crash log </p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:1).state:4
                                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5014)
                                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
                                       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
                                       at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
                                       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
                                       at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
                                       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
                                       at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
                                       at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568)
                                       at android.view.View.layout(View.java:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       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:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                       at android.view.View.layout(View.java:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                       at android.view.View.layout(View.java:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                                       at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364)
                                       at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:846)
                                       at android.view.View.layout(View.java:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1197)
                                       at android.view.View.layout(View.java:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                       at android.view.View.layout(View.java:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       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:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                       at android.view.View.layout(View.java:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       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:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       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:16651)
                                       at android.view.ViewGroup.layout(ViewGroup.java:5440)
                                       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1943)
  </code></pre>
  
  <p>what am I missing here? please help!!
  thanks!.</p>