最近项目中遇到一个下拉刷新动画不能关闭的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();
}
}