From abeca3d382d0d5a478fa6c8bf2680e8635d866c3 Mon Sep 17 00:00:00 2001 From: maxli Date: Wed, 26 Jun 2024 14:52:57 +0800 Subject: [PATCH] fix(android): different sticky item position crash --- .../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 c4324be68a3..54772321c35 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 final com.tencent.mtt.hippy.views.hippylist.recyclerview.helper.skikcy.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(); } @@ -93,7 +98,7 @@ public void setUpdateStickyViewWhenLayout(boolean bindStickyHolderWhenLayout) { * 如果当前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);