我正在将RecyclerView
(com.android.support:recyclerview-v7:21.0.2)与GridLayoutManager
一起使用,并应用dynamic column count。滚动 View 时,将引发异常。当RecyclerView
将要显示以前未显示的项目(重新使用或重新绑定(bind))时,似乎发生了错误。但是,如果我不使用这样的动态跨度/列数,或者只是对调用GridLayoutManager.setSpanCount
的行进行注释,则不会发生。
我试图检查source code,似乎问题与一个临时数组mSet
有关。
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)
如何解决这个问题,使我可以拥有一个具有动态跨度计数的网格?谢谢你。
最佳答案
我针对此问题的解决方法是调整mSet的大小。通过扩展GridLayoutManager做到了。
package android.support.v7.widget;
import android.content.Context;
import android.view.View;
public class MyGridLayoutManager extends GridLayoutManager {
public MyGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public MyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
@Override
public void setSpanCount(int spanCount) {
super.setSpanCount(spanCount);
if (mSet != null && mSet.length != spanCount) {
View[] newSet = new View[spanCount];
System.arraycopy(mSet, 0, newSet, 0, Math.min(mSet.length, spanCount));
mSet = newSet;
}
}
}
关于android - 使用GridLayoutManager滚动RecyclerView时出现ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27227777/