|
5 | 5 | import android.content.res.TypedArray; |
6 | 6 | import android.graphics.Rect; |
7 | 7 | import android.os.Build; |
| 8 | +import android.os.Handler; |
8 | 9 | import android.support.v4.view.PagerAdapter; |
9 | 10 | import android.support.v4.view.ViewCompat; |
10 | 11 | import android.support.v4.view.ViewPager; |
@@ -113,6 +114,20 @@ class InfiniteCycleManager implements OnNotifyDataSetChangedListener { |
113 | 114 | // Interpolator of snapping |
114 | 115 | private Interpolator mInterpolator; |
115 | 116 |
|
| 117 | + // Auto scroll values |
| 118 | + private boolean mIsAutoScroll; |
| 119 | + private boolean mIsAutoScrollPositive; |
| 120 | + // Auto scroll handlers |
| 121 | + private final Handler mAutoScrollHandler = new Handler(); |
| 122 | + private final Runnable mAutoScrollRunnable = new Runnable() { |
| 123 | + @Override |
| 124 | + public void run() { |
| 125 | + if (!mIsAutoScroll) return; |
| 126 | + mViewPageable.setCurrentItem(getRealItem() + (mIsAutoScrollPositive ? 1 : -1)); |
| 127 | + mAutoScrollHandler.postDelayed(this, mScrollDuration); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
116 | 131 | public InfiniteCycleManager( |
117 | 132 | final Context context, |
118 | 133 | final ViewPageable viewPageable, |
@@ -315,7 +330,7 @@ public PagerAdapter setAdapter(final PagerAdapter adapter) { |
315 | 330 | public boolean onTouchEvent(final MotionEvent event) { |
316 | 331 | if (mViewPageable.getAdapter() == null || mViewPageable.getAdapter().getCount() == 0) |
317 | 332 | return false; |
318 | | - if (mState == ViewPager.SCROLL_STATE_SETTLING || mViewPageable.isFakeDragging()) return false; |
| 333 | + if (mIsAutoScroll || mIsInitialItem || mViewPageable.isFakeDragging()) return false; |
319 | 334 | if (event.getPointerCount() > MIN_POINTER_COUNT || !mViewPageable.hasWindowFocus()) |
320 | 335 | event.setAction(MotionEvent.ACTION_UP); |
321 | 336 | checkHitRect(event); |
@@ -343,8 +358,7 @@ public int setCurrentItem(final int item) { |
343 | 358 | if (mIsAdapterInitialPosition) { |
344 | 359 | mIsAdapterInitialPosition = false; |
345 | 360 | return ((mInfiniteCyclePagerAdapter.getCount() / 2) / count) * count; |
346 | | - } else return mViewPageable.getCurrentItem() + |
347 | | - (Math.max(0, Math.min(count, item))) - getRealItem(); |
| 361 | + } else return mViewPageable.getCurrentItem() + Math.min(count, item) - getRealItem(); |
348 | 362 | } |
349 | 363 |
|
350 | 364 | // Need to get current position of original adapter. We cant override getCurrentItem() method, |
@@ -448,6 +462,23 @@ private void resetScaleBy() { |
448 | 462 | mCenterScaleBy = (mMaxPageScale - mMinPageScale) * 0.5F; |
449 | 463 | } |
450 | 464 |
|
| 465 | + // Start auto scroll |
| 466 | + public void startAutoScroll(final boolean isAutoScrollPositive) { |
| 467 | + if (mIsAutoScroll && isAutoScrollPositive == mIsAutoScrollPositive) return; |
| 468 | + mIsAutoScroll = true; |
| 469 | + mIsAutoScrollPositive = isAutoScrollPositive; |
| 470 | + |
| 471 | + mAutoScrollHandler.removeCallbacks(mAutoScrollRunnable); |
| 472 | + mAutoScrollHandler.post(mAutoScrollRunnable); |
| 473 | + } |
| 474 | + |
| 475 | + // Stop auto scroll |
| 476 | + public void stopAutoScroll() { |
| 477 | + if (!mIsAutoScroll) return; |
| 478 | + mIsAutoScroll = false; |
| 479 | + mAutoScrollHandler.removeCallbacks(mAutoScrollRunnable); |
| 480 | + } |
| 481 | + |
451 | 482 | @Override |
452 | 483 | public void onChanged() { |
453 | 484 | mIsDataSetChanged = true; |
@@ -705,8 +736,6 @@ public void onPageScrolled( |
705 | 736 |
|
706 | 737 | // We need to rewrite states when is dragging and when setCurrentItem() from idle |
707 | 738 | if (mState != ViewPager.SCROLL_STATE_SETTLING || mIsInitialItem) { |
708 | | - mIsInitialItem = false; |
709 | | - |
710 | 739 | // Detect first state from idle |
711 | 740 | if (mOuterPageScrolledState == PageScrolledState.IDLE && positionOffset > 0) { |
712 | 741 | mPageScrolledPosition = mViewPageable.getCurrentItem(); |
@@ -740,6 +769,8 @@ else if (mOuterPageScrolledState == PageScrolledState.GOING_RIGHT && goingRight) |
740 | 769 | mWasPlusOne = false; |
741 | 770 | mIsLeftPageBringToFront = false; |
742 | 771 | mIsRightPageBringToFront = false; |
| 772 | + |
| 773 | + mIsInitialItem = false; |
743 | 774 | } |
744 | 775 | } |
745 | 776 |
|
|
0 commit comments