diff --git a/flap/src/main/java/me/yifeiyuan/flap/widget/FlapIndexedStaggeredGridLayoutManager.java b/flap/src/main/java/me/yifeiyuan/flap/widget/FlapIndexedStaggeredGridLayoutManager.java index 99d1e2b9..5fd0659e 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/widget/FlapIndexedStaggeredGridLayoutManager.java +++ b/flap/src/main/java/me/yifeiyuan/flap/widget/FlapIndexedStaggeredGridLayoutManager.java @@ -316,11 +316,15 @@ boolean checkForGaps() { @Override public void onScrollStateChanged(int state) { - if (state == RecyclerView.SCROLL_STATE_IDLE) { - checkForGaps(); - if (reLayoutOnScrollToTop) { - handleAutoReLayoutWhenScrollToTop(); + try { + if (state == RecyclerView.SCROLL_STATE_IDLE) { + checkForGaps(); + if (reLayoutOnScrollToTop) { + handleAutoReLayoutWhenScrollToTop(); + } } + } catch (Exception e) { + e.printStackTrace(); } } @@ -2544,6 +2548,14 @@ int getStartLine(int def) { } void calculateCachedStart() { + // Fatal Exception: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 + // https://stackoverflow.com/questions/44571895/staggeredgridlayoutmanager-calculatecachedstart-indexoutofboundsexception + // 解法: 设置 mGapStrategy == GAP_HANDLING_NONE + // 这里主动规避一下 IndexOutOfBoundsException + if (mViews.size() == 0) { + return; + } + final View startView = mViews.get(0); final FlapIndexedStaggeredGridLayoutManager.LayoutParams lp = getLayoutParams(startView); mCachedStart = mPrimaryOrientation.getDecoratedStart(startView); diff --git a/flap/src/main/java/me/yifeiyuan/flap/widget/FlapStaggeredGridLayoutManager.kt b/flap/src/main/java/me/yifeiyuan/flap/widget/FlapStaggeredGridLayoutManager.kt index 2089cfc3..4741da30 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/widget/FlapStaggeredGridLayoutManager.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/widget/FlapStaggeredGridLayoutManager.kt @@ -42,4 +42,13 @@ open class FlapStaggeredGridLayoutManager FlapDebug.e(TAG, "onLayoutChildren: ", e) } } + + override fun onScrollStateChanged(state: Int) { + try { + super.onScrollStateChanged(state) + } catch (e: Exception) { + e.printStackTrace() + FlapDebug.e(TAG, "onScrollStateChanged: ", e) + } + } } \ No newline at end of file