content: package com.an.dagger.ui.custom.recyclerview;
  
  import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.LinearSnapHelper;
  import android.support.v7.widget.OrientationHelper;
  import android.support.v7.widget.RecyclerView;
  import android.view.View;
  
  
  public class PagerSnapHelper extends LinearSnapHelper {
  
      private RecyclerSnapItemListener recyclerSnapItemListener;
      private OrientationHelper mVerticalHelper, mHorizontalHelper;
  
      public PagerSnapHelper(RecyclerSnapItemListener recyclerSnapItemListener) {
          this.recyclerSnapItemListener = recyclerSnapItemListener;
      }
  
      @Override
      public void attachToRecyclerView(@Nullable RecyclerView recyclerView) throws IllegalStateException {
          super.attachToRecyclerView(recyclerView);
      }
  
      @Override
      public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager,
                                                @NonNull View targetView) {
          int[] out = new int[2];
  
          if (layoutManager.canScrollHorizontally()) {
              out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));
          } else {
              out[0] = 0;
          }
  
          if (layoutManager.canScrollVertically()) {
              out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager));
          } else {
              out[1] = 0;
          }
          return out;
      }
  
      @Override
      public View findSnapView(RecyclerView.LayoutManager layoutManager) {
  
          if (layoutManager instanceof LinearLayoutManager) {
  
              if (layoutManager.canScrollHorizontally()) {
                  return getStartView(layoutManager, getHorizontalHelper(layoutManager));
              } else {
                  return getStartView(layoutManager, getVerticalHelper(layoutManager));
              }
          }
  
          return super.findSnapView(layoutManager);
      }
  
  
      @Override
      public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
  
          View centerView = findSnapView(layoutManager);
          if (centerView == null)
              return RecyclerView.NO_POSITION;
  
          int position = layoutManager.getPosition(centerView);
          int targetPosition = -1;
          if (layoutManager.canScrollHorizontally()) {
              if (velocityX < 0) {
                  targetPosition = position - 1;
              } else {
                  targetPosition = position + 1;
              }
          }
  
          if (layoutManager.canScrollVertically()) {
              if (velocityY < 0) {
                  targetPosition = position - 1;
              } else {
                  targetPosition = position + 1;
              }
          }
  
          final int firstItem = 0;
          final int lastItem = layoutManager.getItemCount() - 1;
          targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem));
          if(targetPosition >= 0) recyclerSnapItemListener.onItemSnap(targetPosition);
          return targetPosition;
      }
  
      private int distanceToStart(View targetView, OrientationHelper helper) {
          return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
      }
  
      private View getStartView(RecyclerView.LayoutManager layoutManager,
                                OrientationHelper helper) {
  
          if (layoutManager instanceof LinearLayoutManager) {
              int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
  
              boolean isLastItem = ((LinearLayoutManager) layoutManager)
                      .findLastCompletelyVisibleItemPosition()
                      == layoutManager.getItemCount() - 1;
  
              if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
                  return null;
              }
  
              View child = layoutManager.findViewByPosition(firstChild);
  
              if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
                      && helper.getDecoratedEnd(child) > 0) {
                  return child;
              } else {
                  if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
                          == layoutManager.getItemCount() - 1) {
                      return null;
                  } else {
                      return layoutManager.findViewByPosition(firstChild + 1);
                  }
              }
          }
  
          return super.findSnapView(layoutManager);
      }
  
      private OrientationHelper getVerticalHelper(RecyclerView.LayoutManager layoutManager) {
          if (mVerticalHelper == null) {
              mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager);
          }
          return mVerticalHelper;
      }
  
      private OrientationHelper getHorizontalHelper(RecyclerView.LayoutManager layoutManager) {
          if (mHorizontalHelper == null) {
              mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
          }
          return mHorizontalHelper;
      }
  }