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;
        }
    }
}

@@ -889,37 +889,8 @@
 if (
-!mAutoSet) %7B%0A            if (
 newS
@@ -920,32 +920,304 @@
 ROLL_STATE_IDLE)
+%7B%0A            if (mAutoSet) %7B%0A                if (onPageChangeListener != null) %7B%0A                    onPageChangeListener.onPageSelected(((ViewPagerLayoutManager) layoutManager).getCurrentPosition());%0A                %7D%0A                mAutoSet = false;%0A            %7D else
  %7B%0A             
@@ -1314,24 +1314,59 @@
 nterView();%0A
+                if (dx != 0) %7B%0A    
             
@@ -1405,38 +1405,32 @@
 dx, 0);%0A        
-    %7D%0A
             mAut
@@ -1454,65 +1454,33 @@
     
-%7D else if (newState == RecyclerView.SCROLL_STATE_IDLE) %7B%0A
+        %7D else %7B%0A        
     
@@ -1515,32 +1515,40 @@
 ener != null) %7B%0A
+        
                 
@@ -1651,32 +1651,40 @@
 ));%0A            
+        
 %7D%0A        %7D%0A    
@@ -1677,25 +1677,92 @@
         
-%7D%0A
        
+     mAutoSet = false;%0A                %7D%0A            %7D%0A        %7D else
  if (new

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