|
3 | 3 | import android.animation.Animator; |
4 | 4 | import android.animation.AnimatorListenerAdapter; |
5 | 5 | import android.animation.ValueAnimator; |
| 6 | +import android.view.animation.AccelerateDecelerateInterpolator; |
6 | 7 | import android.view.animation.Interpolator; |
7 | 8 |
|
8 | 9 |
|
|
14 | 15 | public class FragmentContainerHelper extends AnimatorListenerAdapter implements ValueAnimator.AnimatorUpdateListener { |
15 | 16 | private MagicIndicator mMagicIndicator; |
16 | 17 | private ValueAnimator mScrollAnimator; |
17 | | - private int mDuration = 200; |
18 | | - private Interpolator mInterpolator; |
| 18 | + private int mLastSelectedIndex; |
| 19 | + private int mDuration = 150; |
| 20 | + private Interpolator mInterpolator = new AccelerateDecelerateInterpolator(); |
19 | 21 |
|
20 | 22 | public FragmentContainerHelper(MagicIndicator magicIndicator) { |
21 | 23 | mMagicIndicator = magicIndicator; |
22 | 24 | } |
23 | 25 |
|
24 | 26 | public void handlePageSelected(int selectedIndex) { |
25 | | - if (mScrollAnimator == null || !mScrollAnimator.isRunning()) { |
26 | | - dispatchPageScrollStateChanged(ScrollState.SCROLL_STATE_SETTLING); |
| 27 | + handlePageSelected(selectedIndex, true); |
| 28 | + } |
| 29 | + |
| 30 | + public void handlePageSelected(int selectedIndex, boolean smooth) { |
| 31 | + if (mLastSelectedIndex == selectedIndex) { |
| 32 | + return; |
27 | 33 | } |
28 | | - dispatchPageSelected(selectedIndex); |
29 | | - float currentPositionOffsetSum = 0.0f; // position = 0, positionOffset = 0.0f |
30 | | - if (mScrollAnimator != null) { |
31 | | - currentPositionOffsetSum = (Float) mScrollAnimator.getAnimatedValue(); |
32 | | - mScrollAnimator.cancel(); |
33 | | - mScrollAnimator = null; |
| 34 | + if (smooth) { |
| 35 | + if (mScrollAnimator == null || !mScrollAnimator.isRunning()) { |
| 36 | + dispatchPageScrollStateChanged(ScrollState.SCROLL_STATE_SETTLING); |
| 37 | + } |
| 38 | + dispatchPageSelected(selectedIndex); |
| 39 | + float currentPositionOffsetSum = mLastSelectedIndex; |
| 40 | + if (mScrollAnimator != null) { |
| 41 | + currentPositionOffsetSum = (Float) mScrollAnimator.getAnimatedValue(); |
| 42 | + mScrollAnimator.cancel(); |
| 43 | + mScrollAnimator = null; |
| 44 | + } |
| 45 | + mScrollAnimator = new ValueAnimator(); |
| 46 | + mScrollAnimator.setFloatValues(currentPositionOffsetSum, selectedIndex); // position = selectedIndex, positionOffset = 0.0f |
| 47 | + mScrollAnimator.addUpdateListener(this); |
| 48 | + mScrollAnimator.addListener(this); |
| 49 | + mScrollAnimator.setInterpolator(mInterpolator); |
| 50 | + mScrollAnimator.setDuration(mDuration); |
| 51 | + mScrollAnimator.start(); |
| 52 | + } else { |
| 53 | + dispatchPageSelected(selectedIndex); |
| 54 | + if (mScrollAnimator != null && mScrollAnimator.isRunning()) { |
| 55 | + dispatchPageScrolled(mLastSelectedIndex, 0.0f, 0); |
| 56 | + } |
| 57 | + dispatchPageScrollStateChanged(ScrollState.SCROLL_STATE_IDLE); |
| 58 | + dispatchPageScrolled(selectedIndex, 0.0f, 0); |
34 | 59 | } |
35 | | - mScrollAnimator = new ValueAnimator(); |
36 | | - mScrollAnimator.setFloatValues(currentPositionOffsetSum, selectedIndex); // position = selectedIndex, positionOffset = 0.0f |
37 | | - mScrollAnimator.addUpdateListener(this); |
38 | | - mScrollAnimator.addListener(this); |
39 | | - mScrollAnimator.setInterpolator(mInterpolator); |
40 | | - mScrollAnimator.setDuration(mDuration); |
41 | | - mScrollAnimator.start(); |
| 60 | + mLastSelectedIndex = selectedIndex; |
42 | 61 | } |
43 | 62 |
|
44 | 63 | public void setDuration(int duration) { |
45 | 64 | mDuration = duration; |
46 | 65 | } |
47 | 66 |
|
48 | 67 | public void setInterpolator(Interpolator interpolator) { |
49 | | - mInterpolator = interpolator; |
| 68 | + if (interpolator == null) { |
| 69 | + mInterpolator = new AccelerateDecelerateInterpolator(); |
| 70 | + } else { |
| 71 | + mInterpolator = interpolator; |
| 72 | + } |
50 | 73 | } |
51 | 74 |
|
52 | 75 | private void dispatchPageSelected(int pageIndex) { |
@@ -76,5 +99,6 @@ public void onAnimationUpdate(ValueAnimator animation) { |
76 | 99 | @Override |
77 | 100 | public void onAnimationEnd(Animator animation) { |
78 | 101 | mMagicIndicator.onPageScrollStateChanged(ScrollState.SCROLL_STATE_IDLE); |
| 102 | + mScrollAnimator = null; |
79 | 103 | } |
80 | 104 | } |
0 commit comments