From 39f20b7adf2688631ef594fe5e5e2c8f2931a6db Mon Sep 17 00:00:00 2001 From: Fitz Date: Tue, 1 Nov 2022 19:05:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=84=E9=81=BF=E4=B8=80=E4=B8=8B=20Stag?= =?UTF-8?q?geredGridLayoutManager=20calculateCachedStart()=20IndexOutOfBou?= =?UTF-8?q?ndsException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../widget/FlapIndexedStaggeredGridLayoutManager.java | 8 ++++++++ 1 file changed, 8 insertions(+) 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..c63f8c01 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/widget/FlapIndexedStaggeredGridLayoutManager.java +++ b/flap/src/main/java/me/yifeiyuan/flap/widget/FlapIndexedStaggeredGridLayoutManager.java @@ -2544,6 +2544,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); From 8741a0dfd9f239e4e8f21d70932a6348757a6219 Mon Sep 17 00:00:00 2001 From: Fitz Date: Tue, 1 Nov 2022 19:20:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?catch=20onScrollStateChanged=20=E8=A7=84?= =?UTF-8?q?=E9=81=BF=E7=B3=BB=E7=BB=9F=20StaggeredGridLayoutManager=20?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FlapIndexedStaggeredGridLayoutManager.java | 12 ++++++++---- .../flap/widget/FlapStaggeredGridLayoutManager.kt | 9 +++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) 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 c63f8c01..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(); } } 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