下拉刷新動畫不能關閉的BUG

最近項目中遇到一個下拉刷新動畫不能關閉的bug,在連續點擊強制手動刷新的時候,刷新動畫一直存在不能關閉;下拉刷新則沒有此問題。查閱代碼發現是刷新控件的問題:

    //比較新舊滾動值
    if(oldScrollValue !=newScrollValue) {
       //滾動
        if (delayMillis > 0) {
            postDelayed(mCurrentSmoothScrollRunnable, delayMillis);
        } else {
            post(mCurrentSmoothScrollRunnable);
        }
    }

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 we're not at the target Y, keep going...
            if (mContinueRunning && mScrollToY != mCurrentY) {
                ViewCompat.postOnAnimation(PullToRefreshBase.this, this);
            }  
            //mScrollToY == mCurrentY滾動到指定位置,下拉界面完全展開,調用滾動結束監聽
            else {
                if (null != mListener) {
                    mListener.onSmoothScrollFinished();
                }
            }
        }

原因:實現機制滾動到指定位置,下拉界面完全展開,調用滾動結束監聽。如果正好下拉界面完全展開的時候點擊了強制刷新,則不會調用滾動結束監聽

解決方案:

    if(oldScrollValue !=newScrollValue) {
       //滾動
        if (delayMillis > 0) {
            postDelayed(mCurrentSmoothScrollRunnable, delayMillis);
        } else {
            post(mCurrentSmoothScrollRunnable);
        }
    }
    //增加以下代碼解決手動刷新問題
    else{
        if (null != listener) {
            listener.onSmoothScrollFinished();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章