|
7 | 7 |
|
8 | 8 | package com.facebook.react.views.scroll;
|
9 | 9 |
|
| 10 | +import static com.facebook.react.views.scroll.ReactScrollViewHelper.SNAP_ALIGNMENT_CENTER; |
10 | 11 | import static com.facebook.react.views.scroll.ReactScrollViewHelper.SNAP_ALIGNMENT_DISABLED;
|
| 12 | +import static com.facebook.react.views.scroll.ReactScrollViewHelper.SNAP_ALIGNMENT_END; |
| 13 | +import static com.facebook.react.views.scroll.ReactScrollViewHelper.SNAP_ALIGNMENT_START; |
11 | 14 |
|
12 | 15 | import android.animation.Animator;
|
13 | 16 | import android.animation.ObjectAnimator;
|
|
23 | 26 | import android.view.KeyEvent;
|
24 | 27 | import android.view.MotionEvent;
|
25 | 28 | import android.view.View;
|
| 29 | +import android.view.ViewGroup; |
26 | 30 | import android.view.accessibility.AccessibilityEvent;
|
27 | 31 | import android.widget.HorizontalScrollView;
|
28 | 32 | import android.widget.OverScroller;
|
@@ -912,7 +916,7 @@ private void flingAndSnap(int velocityX) {
|
912 | 916 | }
|
913 | 917 |
|
914 | 918 | // pagingEnabled only allows snapping one interval at a time
|
915 |
| - if (mSnapInterval == 0 && mSnapOffsets == null) { |
| 919 | + if (mSnapInterval == 0 && mSnapOffsets == null && mSnapToAlignment == SNAP_ALIGNMENT_DISABLED) { |
916 | 920 | smoothScrollAndSnap(velocityX);
|
917 | 921 | return;
|
918 | 922 | }
|
@@ -955,6 +959,36 @@ private void flingAndSnap(int velocityX) {
|
955 | 959 | }
|
956 | 960 | }
|
957 | 961 | }
|
| 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(""); |
| 979 | + } |
| 980 | + if (itemStartOffset <= targetOffset) { |
| 981 | + if (targetOffset - itemStartOffset < targetOffset - smallerOffset) { |
| 982 | + smallerOffset = itemStartOffset; |
| 983 | + } |
| 984 | + } |
| 985 | + |
| 986 | + if (itemStartOffset >= targetOffset) { |
| 987 | + if (itemStartOffset - targetOffset < largerOffset - targetOffset) { |
| 988 | + largerOffset = itemStartOffset; |
| 989 | + } |
| 990 | + } |
| 991 | + } |
958 | 992 | } else {
|
959 | 993 | double interval = (double) getSnapInterval();
|
960 | 994 | double ratio = (double) targetOffset / interval;
|
|
0 commit comments