title: Got ArrayIndexOutOfBoundsException when scrolling RecyclerView with GridLayoutManager
body: <p>I am using <code>RecyclerView</code> (com.android.support:recyclerview-v7:21.0.2) with <code>GridLayoutManager</code> and applying <a href="https://stackoverflow.com/a/27000759/1197317">dynamic column count</a>. When the view is scrolled an exception is thrown. It seems the error happens when the <code>RecyclerView</code> about to display items that were not displayed previously (reusing or rebinding). However, if I don't use such dynamic span/column count or simply comment the line calling <code>GridLayoutManager.setSpanCount</code>, it does not happen.</p>
  
  <p>I've tried to inspect the <a href="https://android.googlesource.com/platform/frameworks/support/+/master/v7/recyclerview/src/android/support/v7/widget/GridLayoutManager.java" rel="nofollow noreferrer">source code</a>, it seems the problem is related to a temporary array <code>mSet</code>.</p>
  
  <pre><code>E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.ArrayIndexOutOfBoundsException
              at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:361)
              at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
              at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1097)
              at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:957)
              at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:985)
              at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:1686)
              at android.view.View.dispatchTouchEvent(View.java:3885)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
              at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
              at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
              at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
              at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
              at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:123)
              at android.app.ActivityThread.main(ActivityThread.java:3683)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:507)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>How to solve this so that I can have a grid with dynamic span count? Thank you.</p>