From 40b3f29f8167e51825251fa3f7af4a2d6cbb86b2 Mon Sep 17 00:00:00 2001 From: siguangli Date: Wed, 26 Jun 2024 16:42:37 +0800 Subject: [PATCH] fix(android): different sticky item position crash (#3930) Co-authored-by: maxli Co-authored-by: OpenHippy <124017524+open-hippy@users.noreply.github.com> --- .../views/hippylist/HippyRecyclerListAdapter.java | 2 +- .../helper/skikcy/StickyHeaderHelper.java | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/hippylist/HippyRecyclerListAdapter.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/hippylist/HippyRecyclerListAdapter.java index 981f25ba232..818ae6f3b01 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/hippylist/HippyRecyclerListAdapter.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/hippylist/HippyRecyclerListAdapter.java @@ -252,7 +252,7 @@ public int getItemViewType(int position) { return 0; } if (node.shouldSticky()) { - return STICK_ITEM_VIEW_TYPE_BASE - position; + return STICK_ITEM_VIEW_TYPE_BASE - node.getItemViewType(); } return node.getItemViewType(); } diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/hippylist/recyclerview/helper/skikcy/StickyHeaderHelper.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/hippylist/recyclerview/helper/skikcy/StickyHeaderHelper.java index 059bea6bdf9..772cb4e392b 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/hippylist/recyclerview/helper/skikcy/StickyHeaderHelper.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/hippylist/recyclerview/helper/skikcy/StickyHeaderHelper.java @@ -39,6 +39,7 @@ public class StickyHeaderHelper extends OnScrollListener implements ViewTreeObserver.OnGlobalLayoutListener { + private static final String TAG = "StickyHeaderHelper"; private static final int INVALID_POSITION = -1; private IHeaderAttachListener headerAttachListener; private RecyclerViewBase recyclerView; @@ -75,8 +76,12 @@ public void setOrientation(int orientation) { public void onScrolled(RecyclerView recyclerView, int dx, int dy) { int newStickyPosition = getStickyItemPosition(); if (currentStickPos != newStickyPosition) { - detachSticky(); - attachSticky(newStickyPosition); + try { + detachSticky(); + attachSticky(newStickyPosition); + } catch (Exception e) { + LogUtils.e(TAG, "sticky handle error: " + e.getMessage()); + } } offsetSticky(); } @@ -101,7 +106,7 @@ public void onDestroy() { * 如果当前stickHolder和新的stickyHolder 不一样,那么把当前的stickyHolder删除掉,并还原HeaderView的Translation */ public void detachSticky() { - if (headerOrgViewHolder != null) { + if (headerOrgViewHolder != null && this.currentHeaderView != null) { removeViewFromParent(this.currentHeaderView); currentHeaderView.setTranslationY(0); currentHeaderView.setTranslationX(0);