@Override
    public void onScrollStateChanged(int state) {
        if (DEBUG)
            Log.d("RecyclerViewPager", "onScrollStateChanged:" + state);
        super.onScrollStateChanged(state);
        // 当处于滑动状态时
        if (state == SCROLL_STATE_DRAGGING) {
            mNeedAdjust = true;
            mCurView = getLayoutManager().canScrollHorizontally() ? ViewUtils.getCenterXChild(this) :
                    ViewUtils.getCenterYChild(this);
            if (mCurView != null) {
                if (mHasCalledOnPageChanged) {
                    // While rvp is scrolling, mPositionBeforeScroll will be previous value.
                    mPositionBeforeScroll = getChildLayoutPosition(mCurView);
                    mHasCalledOnPageChanged = false;
                }
                if (DEBUG) {
                    Log.d("RecyclerViewPager", "mPositionBeforeScroll:" + mPositionBeforeScroll);
                }
                mFisrtLeftWhenDragging = mCurView.getLeft();
                mFirstTopWhenDragging = mCurView.getTop();
            } else {
                mPositionBeforeScroll = -1;
            }
            mTouchSpan = 0;
        } else if (state == SCROLL_STATE_SETTLING) { // 处于惯性状态
            mNeedAdjust = false;
            if (mCurView != null) {
                if (getLayoutManager().canScrollHorizontally()) {
                    mTouchSpan = mCurView.getLeft() - mFisrtLeftWhenDragging;
                } else {
                    mTouchSpan = mCurView.getTop() - mFirstTopWhenDragging;
                }
            } else {
                mTouchSpan = 0;
            }
            mCurView = null;
        } else if (state == SCROLL_STATE_IDLE) { // 处于静止状态
            if (mNeedAdjust) {
                int targetPosition = getLayoutManager().canScrollHorizontally() ? ViewUtils.getCenterXChildPosition(this) :
                        ViewUtils.getCenterYChildPosition(this);
                if (mCurView != null) {
                    targetPosition = getChildAdapterPosition(mCurView);
                    if (getLayoutManager().canScrollHorizontally()) {
                        int spanX = mCurView.getLeft() - mFisrtLeftWhenDragging;
                        // if user is tending to cancel paging action, don't perform position changing
                        if (spanX > mCurView.getWidth() * mTriggerOffset && mCurView.getLeft() >= mMaxLeftWhenDragging) {
                            targetPosition--;
                        } else if (spanX < mCurView.getWidth() * -mTriggerOffset && mCurView.getLeft() <= mMinLeftWhenDragging) {
                            targetPosition++;
                        }
                    } else {
                        int spanY = mCurView.getTop() - mFirstTopWhenDragging;
                        if (spanY > mCurView.getHeight() * mTriggerOffset && mCurView.getTop() >= mMaxTopWhenDragging) {
                            targetPosition--;
                        } else if (spanY < mCurView.getHeight() * -mTriggerOffset && mCurView.getTop() <= mMinTopWhenDragging) {
                            targetPosition++;
                        }
                    }
                }
                smoothScrollToPosition(safeTargetPosition(targetPosition, mViewPagerAdapter.getItemCount()));
                mCurView = null;
            } else if (mSmoothScrollTargetPosition != mPositionBeforeScroll) {
                if (DEBUG) {
                    Log.d("RecyclerViewPager", "onScrollStateChanged SCROLL_STATE_IDLE:" + mSmoothScrollTargetPosition);
                    Log.d("RecyclerViewPager", "onScrollStateChanged SCROLL_STATE_IDLE:" + mPositionBeforeScroll);
                }
                if (mOnPageChangedListeners != null) {
                    for (OnPageChangedListener onPageChangedListener : mOnPageChangedListeners) {
                        if (onPageChangedListener != null) {
                            onPageChangedListener.OnPageChanged(mPositionBeforeScroll, mSmoothScrollTargetPosition);
                        }
                    }
                }
                mHasCalledOnPageChanged = true;
                mPositionBeforeScroll = mSmoothScrollTargetPosition;
            }
            // reset
            mMaxLeftWhenDragging = Integer.MIN_VALUE;
            mMinLeftWhenDragging = Integer.MAX_VALUE;
            mMaxTopWhenDragging = Integer.MIN_VALUE;
            mMinTopWhenDragging = Integer.MAX_VALUE;
        }
    }
@ Override public void onScrollStateChanged ( int state ) { if ( DEBUG ) Log . d ( "RecyclerViewPager" , "onScrollStateChanged:" + state ) ; super . onScrollStateChanged ( state ) ; if ( state == SCROLL_STATE_DRAGGING ) { mNeedAdjust = true ; mCurView = getLayoutManager ( ) . canScrollHorizontally ( ) ? ViewUtils . getCenterXChild ( this ) : ViewUtils . getCenterYChild ( this ) ; if ( mCurView != null ) { if ( mHasCalledOnPageChanged ) { mPositionBeforeScroll = getChildLayoutPosition ( mCurView ) ; mHasCalledOnPageChanged = false ; } if ( DEBUG ) { Log . d ( "RecyclerViewPager" , "mPositionBeforeScroll:" + mPositionBeforeScroll ) ; } mFisrtLeftWhenDragging = mCurView . getLeft ( ) ; mFirstTopWhenDragging = mCurView . getTop ( ) ; } else { mPositionBeforeScroll = - 1 ; } mTouchSpan = 0 ; } else if ( state == SCROLL_STATE_SETTLING ) { mNeedAdjust = false ; if ( mCurView != null ) { if ( getLayoutManager ( ) . canScrollHorizontally ( ) ) { mTouchSpan = mCurView . getLeft ( ) - mFisrtLeftWhenDragging ; } else { mTouchSpan = mCurView . getTop ( ) - mFirstTopWhenDragging ; } } else { mTouchSpan = 0 ; } mCurView = null ; } else if ( state == SCROLL_STATE_IDLE ) { if ( mNeedAdjust ) { int targetPosition = getLayoutManager ( ) . canScrollHorizontally ( ) ? ViewUtils . getCenterXChildPosition ( this ) : ViewUtils . getCenterYChildPosition ( this ) ; if ( mCurView != null ) { targetPosition = getChildAdapterPosition ( mCurView ) ; if ( getLayoutManager ( ) . canScrollHorizontally ( ) ) { int spanX = mCurView . getLeft ( ) - mFisrtLeftWhenDragging ; if ( spanX > mCurView . getWidth ( ) * mTriggerOffset && mCurView . getLeft ( ) >= mMaxLeftWhenDragging ) { targetPosition -- ; } else if ( spanX < mCurView . getWidth ( ) * - mTriggerOffset && mCurView . getLeft ( ) <= mMinLeftWhenDragging ) { targetPosition ++ ; } } else { int spanY = mCurView . getTop ( ) - mFirstTopWhenDragging ; if ( spanY > mCurView . getHeight ( ) * mTriggerOffset && mCurView . getTop ( ) >= mMaxTopWhenDragging ) { targetPosition -- ; } else if ( spanY < mCurView . getHeight ( ) * - mTriggerOffset && mCurView . getTop ( ) <= mMinTopWhenDragging ) { targetPosition ++ ; } } } smoothScrollToPosition ( safeTargetPosition ( targetPosition , mViewPagerAdapter . getItemCount ( ) ) ) ; mCurView = null ; } else if ( mSmoothScrollTargetPosition != mPositionBeforeScroll ) { if ( DEBUG ) { Log . d ( "RecyclerViewPager" , "onScrollStateChanged SCROLL_STATE_IDLE:" + mSmoothScrollTargetPosition ) ; Log . d ( "RecyclerViewPager" , "onScrollStateChanged SCROLL_STATE_IDLE:" + mPositionBeforeScroll ) ; } if ( mOnPageChangedListeners != null ) { for ( OnPageChangedListener onPageChangedListener : mOnPageChangedListeners ) { if ( onPageChangedListener != null ) { onPageChangedListener . OnPageChanged ( mPositionBeforeScroll , mSmoothScrollTargetPosition ) ; } } } mHasCalledOnPageChanged = true ; mPositionBeforeScroll = mSmoothScrollTargetPosition ; } mMaxLeftWhenDragging = Integer . MIN_VALUE ; mMinLeftWhenDragging = Integer . MAX_VALUE ; mMaxTopWhenDragging = Integer . MIN_VALUE ; mMinTopWhenDragging = Integer . MAX_VALUE ; } }
当滑动状态改变时所做的各种处理。