Skip to content

Commit

Permalink
修复 ScrollToPositionHelper#scrollToPosition(int):void 方法的 BUG
Browse files Browse the repository at this point in the history
- 原因:无法执行背景闪动动画。
  • Loading branch information
jrfeng committed Mar 22, 2020
1 parent 7616b90 commit a5a060c
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public class ScrollToPositionHelper {
private View mTargetView;
private Drawable mTargetViewBackground;

// 由于调用 scrollToPosition 方法时只会触发 RecyclerView.OnScrollListener 的 onScrolled 方法,而
// 不会触发 onScrollStateChanged 方法,由于任何滚动事件都会触发 onScrolled 方法,因此需要在
// onScrolled 方法中使用一个标志位来判断是否执行了 scrollToPosition 方法。
private boolean mExecutedScrollToPosition;

/**
* 创建一个 ScrollToPositionHelper 对象。
*
Expand Down Expand Up @@ -132,6 +137,14 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat

startBackgroundAnim();
}

@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (mExecutedScrollToPosition) {
mExecutedScrollToPosition = false;
startBackgroundAnim();
}
}
});
}

Expand Down Expand Up @@ -160,18 +173,19 @@ public void setAnimInterpolator(@NonNull TimeInterpolator interpolator) {
}

public void scrollToPosition(int position) {
if (mAnimator.isRunning()) {
mAnimator.cancel();
mPosition = position;
mExecutedScrollToPosition = true;

if (isViewHolderVisible()) {
mExecutedScrollToPosition = false;
startBackgroundAnim();
}

mPosition = position;
mRecyclerView.scrollToPosition(position);
}

public void smoothScrollToPosition(int position) {
if (mAnimator.isRunning()) {
mAnimator.cancel();
}
mExecutedScrollToPosition = false;

mPosition = position;
if (isViewHolderVisible()) {
Expand Down Expand Up @@ -230,6 +244,10 @@ private void startBackgroundAnim() {
return;
}

if (mAnimator.isRunning()) {
mAnimator.cancel();
}

RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(mPosition);
mPosition = NO_POSITION;

Expand Down

0 comments on commit a5a060c

Please sign in to comment.