@@ -960,37 +960,45 @@ private void flingAndSnap(int velocityX) {
960
960
}
961
961
}
962
962
} else if (mSnapToAlignment != SNAP_ALIGNMENT_DISABLED ) {
963
- ViewGroup contentView = (ViewGroup ) getContentView ();
964
- for (int i = 1 ; i < contentView .getChildCount (); i ++) {
965
- View item = contentView .getChildAt (i );
966
- int itemStartOffset ;
967
- switch (mSnapToAlignment ) {
968
- case SNAP_ALIGNMENT_CENTER :
969
- itemStartOffset = item .getLeft () - (width - item .getWidth ()) / 2 ;
970
- break ;
971
- case SNAP_ALIGNMENT_START :
972
- itemStartOffset = item .getLeft ();
973
- break ;
974
- case SNAP_ALIGNMENT_END :
975
- itemStartOffset = item .getLeft () - (width - item .getWidth ());
976
- break ;
977
- default :
978
- throw new IllegalStateException ("Invalid SnapToAlignment value: " + mSnapToAlignment );
979
- }
980
- if (itemStartOffset <= targetOffset ) {
981
- if (targetOffset - itemStartOffset < targetOffset - smallerOffset ) {
982
- smallerOffset = itemStartOffset ;
963
+ if (mSnapInterval > 0 ) {
964
+ double ratio = (double ) targetOffset / mSnapInterval ;
965
+ smallerOffset =
966
+ Math .max (
967
+ getItemStartOffset (
968
+ mSnapToAlignment ,
969
+ (int ) (Math .floor (ratio ) * mSnapInterval ),
970
+ mSnapInterval ,
971
+ width ),
972
+ 0 );
973
+ largerOffset =
974
+ Math .min (
975
+ getItemStartOffset (
976
+ mSnapToAlignment ,
977
+ (int ) (Math .ceil (ratio ) * mSnapInterval ),
978
+ mSnapInterval ,
979
+ width ),
980
+ maximumOffset );
981
+ } else {
982
+ ViewGroup contentView = (ViewGroup ) getContentView ();
983
+ for (int i = 1 ; i < contentView .getChildCount (); i ++) {
984
+ View item = contentView .getChildAt (i );
985
+ int itemStartOffset =
986
+ getItemStartOffset (mSnapToAlignment , item .getLeft (), item .getWidth (), width );
987
+ if (itemStartOffset <= targetOffset ) {
988
+ if (targetOffset - itemStartOffset < targetOffset - smallerOffset ) {
989
+ smallerOffset = itemStartOffset ;
990
+ }
983
991
}
984
- }
985
992
986
- if (itemStartOffset >= targetOffset ) {
987
- if (itemStartOffset - targetOffset < largerOffset - targetOffset ) {
988
- largerOffset = itemStartOffset ;
993
+ if (itemStartOffset >= targetOffset ) {
994
+ if (itemStartOffset - targetOffset < largerOffset - targetOffset ) {
995
+ largerOffset = itemStartOffset ;
996
+ }
989
997
}
990
998
}
991
999
}
992
1000
} else {
993
- double interval = ( double ) getSnapInterval ();
1001
+ double interval = getSnapInterval ();
994
1002
double ratio = (double ) targetOffset / interval ;
995
1003
smallerOffset = (int ) (Math .floor (ratio ) * interval );
996
1004
largerOffset = Math .min ((int ) (Math .ceil (ratio ) * interval ), maximumOffset );
@@ -1073,6 +1081,25 @@ private void flingAndSnap(int velocityX) {
1073
1081
}
1074
1082
}
1075
1083
1084
+ private int getItemStartOffset (
1085
+ int snapToAlignment , int itemStartPosition , int itemWidth , int viewPortWidth ) {
1086
+ int itemStartOffset ;
1087
+ switch (snapToAlignment ) {
1088
+ case SNAP_ALIGNMENT_CENTER :
1089
+ itemStartOffset = itemStartPosition - (viewPortWidth - itemWidth ) / 2 ;
1090
+ break ;
1091
+ case SNAP_ALIGNMENT_START :
1092
+ itemStartOffset = itemStartPosition ;
1093
+ break ;
1094
+ case SNAP_ALIGNMENT_END :
1095
+ itemStartOffset = itemStartPosition - (viewPortWidth - itemWidth );
1096
+ break ;
1097
+ default :
1098
+ throw new IllegalStateException ("Invalid SnapToAlignment value: " + mSnapToAlignment );
1099
+ }
1100
+ return itemStartOffset ;
1101
+ }
1102
+
1076
1103
private void smoothScrollToNextPage (int direction ) {
1077
1104
if (DEBUG_MODE ) {
1078
1105
FLog .i (TAG , "smoothScrollToNextPage[%d] direction %d" , getId (), direction );
0 commit comments