private int scrollHorizontallyInternal(int dx){
  int childCount=getChildCount();
  if (childCount == 0) {
    return 0;
  }
  int itemCount=getItemCount();
  View leftView=getChildAt(0);
  View rightView=getChildAt(childCount - 1);
  for (int i=0; i < getChildCount(); i++) {
    View view=getChildAt(i);
    if (getDecoratedLeft(leftView) > getDecoratedLeft(view)) {
      leftView=view;
    }
    if (getDecoratedRight(rightView) < getDecoratedRight(view)) {
      rightView=view;
    }
  }
  int viewSpan=getDecoratedRight(rightView) > getWidth() ? getDecoratedRight(rightView) : getWidth() - (getDecoratedLeft(leftView) < 0 ? getDecoratedLeft(leftView) : 0);
  if (viewSpan < getWidth()) {
    return 0;
  }
  int delta=0;
  if (dx < 0) {
    int firstViewAdapterPos=getPosition(leftView);
    if (firstViewAdapterPos > 0) {
      delta=dx;
    }
 else {
      int viewLeft=getDecoratedLeft(leftView) - getWidth() / 2 + getDecoratedMeasuredWidth(leftView) / 2;
      delta=Math.max(viewLeft,dx);
    }
  }
 else   if (dx > 0) {
    int lastViewAdapterPos=getPosition(rightView);
    if (lastViewAdapterPos < itemCount - 1) {
      delta=dx;
    }
 else {
      int viewRight=getDecoratedRight(rightView) + getWidth() / 2 - getDecoratedMeasuredWidth(rightView) / 2;
      int parentRight=getWidth();
      delta=Math.min(viewRight - parentRight,dx);
    }
  }
  return delta;
}

private int scrollHorizontallyInternal ( int dx ) { int childCount = getChildCount ( ) ; if ( childCount == 0 ) { return 0 ; } int itemCount = getItemCount ( ) ; View leftView = getChildAt ( 0 ) ; View rightView = getChildAt ( childCount - 1 ) ; for ( int i = 0 ; i < getChildCount ( ) ; i ++ ) { View view = getChildAt ( i ) ; if ( getDecoratedLeft ( leftView ) > getDecoratedLeft ( view ) ) { leftView = view ; } if ( getDecoratedRight ( rightView ) < getDecoratedRight ( view ) ) { rightView = view ; } } int viewSpan = getDecoratedRight ( rightView ) > getWidth ( ) ? getDecoratedRight ( rightView ) : getWidth ( ) - ( getDecoratedLeft ( leftView ) < 0 ? getDecoratedLeft ( leftView ) : 0 ) ; if ( viewSpan < getWidth ( ) ) { return 0 ; } int delta = 0 ; if ( dx < 0 ) { int firstViewAdapterPos = getPosition ( leftView ) ; if ( firstViewAdapterPos > 0 ) { delta = dx ; } else { int viewLeft = getDecoratedLeft ( leftView ) - getWidth ( ) / 2 + getDecoratedMeasuredWidth ( leftView ) / 2 ; delta = Math . max ( viewLeft , dx ) ; } } else if ( dx > 0 ) { int lastViewAdapterPos = getPosition ( rightView ) ; if ( lastViewAdapterPos < itemCount - 1 ) { delta = dx ; } else { int viewRight = getDecoratedRight ( rightView ) + getWidth ( ) / 2 - getDecoratedMeasuredWidth ( rightView ) / 2 ; int parentRight = getWidth ( ) ; delta = Math . min ( viewRight - parentRight , dx ) ; } } return delta ; }
calculate delta x for views .
