Skip to content

Commit

Permalink
fix(android): different sticky item position crash (Tencent#3930)
Browse files Browse the repository at this point in the history
Co-authored-by: maxli <[email protected]>
Co-authored-by: OpenHippy <[email protected]>
  • Loading branch information
3 people committed Nov 11, 2024
1 parent 890711e commit a71381b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down

0 comments on commit a71381b

Please sign in to comment.