package rouchuan.customlayoutmanager;

import android.support.v7.widget.RecyclerView;


/**
 * Created by Dajavu on 16/8/18.
 */
public class CenterScrollListener extends RecyclerView.OnScrollListener {
    private boolean mAutoSet = false;

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (!(layoutManager instanceof ViewPagerLayoutManager)) {
            mAutoSet = true;
            return;
        }

        final ViewPagerLayoutManager.OnPageChangeListener onPageChangeListener = ((ViewPagerLayoutManager) layoutManager).onPageChangeListener;
        if (onPageChangeListener != null) {
            onPageChangeListener.onPageScrollStateChanged(newState);
        }

        if (!mAutoSet) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                final int dx;
                dx = ((ViewPagerLayoutManager) layoutManager).getOffsetCenterView();
                recyclerView.smoothScrollBy(dx, 0);
            }
            mAutoSet = true;
        } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            if (onPageChangeListener != null) {
                onPageChangeListener.onPageSelected(((ViewPagerLayoutManager) layoutManager).getCurrentPosition());
            }
        }
        if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
            mAutoSet = false;
        }
    }
}

package rouchuan.customlayoutmanager;

import android.support.v7.widget.RecyclerView;


/**
 * Created by Dajavu on 16/8/18.
 */
public class CenterScrollListener extends RecyclerView.OnScrollListener {
    private boolean mAutoSet = false;

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (!(layoutManager instanceof ViewPagerLayoutManager)) {
            mAutoSet = true;
            return;
        }

        final ViewPagerLayoutManager.OnPageChangeListener onPageChangeListener = ((ViewPagerLayoutManager) layoutManager).onPageChangeListener;
        if (onPageChangeListener != null) {
            onPageChangeListener.onPageScrollStateChanged(newState);
        }

        if (newState == RecyclerView.SCROLL_STATE_IDLE){
            if (mAutoSet) {
                if (onPageChangeListener != null) {
                    onPageChangeListener.onPageSelected(((ViewPagerLayoutManager) layoutManager).getCurrentPosition());
                }
                mAutoSet = false;
            } else {
                final int dx;
                dx = ((ViewPagerLayoutManager) layoutManager).getOffsetCenterView();
                if (dx != 0) {
                    recyclerView.smoothScrollBy(dx, 0);
                    mAutoSet = true;
                } else {
                    if (onPageChangeListener != null) {
                        onPageChangeListener.onPageSelected(((ViewPagerLayoutManager) layoutManager).getCurrentPosition());
                    }
                    mAutoSet = false;
                }
            }
        } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
            mAutoSet = false;
        }
    }
}

修复使用CenterScrollListener时，当回弹距离为0，比如快速滑动到recyclerview最后一个item，不触发ViewPagerLayoutManger.OnPageChangeListener.onPageSelected()问题，因为recyclerView.smoothScrollBy(0,0)时不会触发滑动状态变化。