Skip to content

Commit

Permalink
fix(android): reset pull header position for recycler view
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and zoomchan-cxj committed Apr 27, 2022
1 parent f9059d1 commit 42ef3c9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ private boolean isPullHeader(int position) {
return false;
}

public void resetPullHeaderPositionIfNeeded(int offsetY) {
if (offsetY == 0) {
ListItemRenderNode renderNode = getChildNodeByAdapterPosition(0);
if (renderNode != null && renderNode.isPullHeader()) {
headerEventHelper.resetPullHeaderPositionIfNeeded();
}
}
}

/**
* 获取下拉刷新的事件辅助器
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public void setListData() {
//onLayout方法重载写成空方法,requestLayout不会回调孩子节点的onLayout,这里需要自己发起dispatchLayout
renderNodeCount = getAdapter().getRenderNodeCount();
dispatchLayout();
if (renderNodeCount > 0) {
getAdapter().resetPullHeaderPositionIfNeeded(getContentOffsetY());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ public int getVisibleHeight() {
public void requestLayout() {
recyclerView.dispatchLayout();
}

@Override
public void resetPullHeaderPositionIfNeeded() {
headerRefreshHelper.resetHeaderViewPosition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ private void onMove() {
headerRefreshView.onStartDrag();
}

/**
* 下拉之后,当正在刷新的时候,将位置从下拉到的位置恢复规定的位置的动画
*
* @param destHeight 规定的高度
*/
private void smoothScrollTo(int fromHeight, int destHeight) {
private void smoothScrollTo(int fromHeight, int destHeight, int duration) {
endAnimation();
animator = ValueAnimator.ofInt(fromHeight, destHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Expand All @@ -172,7 +167,16 @@ public void onAnimationEnd(Animator animation) {
}
}
});
animator.setDuration(DURATION).start();
animator.setDuration(duration).start();
}

/**
* 下拉之后,当正在刷新的时候,将位置从下拉到的位置恢复规定的位置的动画
*
* @param destHeight 规定的高度
*/
private void smoothScrollTo(int fromHeight, int destHeight) {
smoothScrollTo(fromHeight, destHeight, DURATION);
}

void gotoRefresh() {
Expand Down Expand Up @@ -281,4 +285,11 @@ private void endAnimation() {
animator = null;
}
}

public void resetHeaderViewPosition() {
if (refreshStatus == IHeaderRefreshView.HEADER_STATUS_FOLDED) {
setVisibleHeight(0);
smoothScrollTo(getVisibleHeight(), 0, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ public interface IHeaderRefreshView {
int getContentHeight();

void onFolded();

void resetPullHeaderPositionIfNeeded();
}

0 comments on commit 42ef3c9

Please sign in to comment.