最近項目中遇到一個下拉刷新動畫不能關閉的bug,在連續點擊強制手動刷新的時候,刷新動畫一直存在不能關閉;下拉刷新則沒有此問題。查閱代碼發現是刷新控件的問題:
if(oldScrollValue !=newScrollValue) {
if (delayMillis > 0) {
postDelayed(mCurrentSmoothScrollRunnable, delayMillis);
} else {
post(mCurrentSmoothScrollRunnable);
}
}
final class SmoothScrollRunnable implements Runnable {
private final Interpolator mInterpolator;
private final int mScrollToY;
private final int mScrollFromY;
private final long mDuration;
private OnSmoothScrollFinishedListener mListener;
private boolean mContinueRunning = true;
private long mStartTime = -1;
private int mCurrentY = -1;
public SmoothScrollRunnable(int fromY, int toY, long duration, OnSmoothScrollFinishedListener listener) {
mScrollFromY = fromY;
mScrollToY = toY;
mInterpolator = mScrollAnimationInterpolator;
mDuration = duration;
mListener = listener;
}
@Override
public void run() {
if (mStartTime == -1) {
mStartTime = System.currentTimeMillis();
} else {
......
mCurrentY = mScrollFromY - deltaY;
setHeaderScroll(mCurrentY);
}
if (mContinueRunning && mScrollToY != mCurrentY) {
ViewCompat.postOnAnimation(PullToRefreshBase.this, this);
}
else {
if (null != mListener) {
mListener.onSmoothScrollFinished();
}
}
}
原因:實現機制滾動到指定位置,下拉界面完全展開,調用滾動結束監聽。如果正好下拉界面完全展開的時候點擊了強制刷新,則不會調用滾動結束監聽
解決方案:
if(oldScrollValue !=newScrollValue) {
if (delayMillis > 0) {
postDelayed(mCurrentSmoothScrollRunnable, delayMillis);
} else {
post(mCurrentSmoothScrollRunnable);
}
}
else{
if (null != listener) {
listener.onSmoothScrollFinished();
}
}