Skip to content

Commit 5f67748

Browse files
committed
Update 1.0.2.
Fixes. Auto scroll. Disable scroll block.
1 parent 6692d04 commit 5f67748

File tree

6 files changed

+108
-10
lines changed

6 files changed

+108
-10
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ You can download a `.aar` from GitHub's [releases page](https://github.com/DevLi
2525
Or use Gradle:
2626

2727
```groovy
28-
compile 'com.github.devlight:infinitecycleviewpager:1.0.1'
28+
compile 'com.github.devlight:infinitecycleviewpager:1.0.2'
2929
```
3030

3131
Or Maven:
@@ -34,15 +34,15 @@ Or Maven:
3434
<dependency>
3535
<groupId>com.github.devlight</groupId>
3636
<artifactId>infinitecycleviewpager</artifactId>
37-
<version>1.0.1</version>
37+
<version>1.0.2</version>
3838
<type>pom</type>
3939
</dependency>
4040
```
4141

4242
Or Ivy:
4343

4444
```groovy
45-
<dependency org='com.github.devlight' name='infinitecycleviewpager' rev='1.0.1'>
45+
<dependency org='com.github.devlight' name='infinitecycleviewpager' rev='1.0.2'>
4646
<artifact name='$AID' ext='pom'></artifact>
4747
</dependency>
4848
```
@@ -94,6 +94,10 @@ For `InfiniteCycleViewPager` you can set such parameters as:
9494
- page transform listener:
9595

9696
allows you to set page transform listener.
97+
98+
- auto scroll:
99+
100+
allows you to set auto scroll in positive and negative directions.
97101

98102
<b>Tips</b>
99103

@@ -133,6 +137,14 @@ To update your ViewPager after some adapter update or else, you can call this me
133137
infiniteCycleViewPager.notifyDataSetChanged();
134138
```
135139

140+
If you want to start auto scroll or stop call this methods:
141+
```java
142+
// true - positive
143+
// false - negative
144+
infiniteCycleViewPager.startAutoScroll(...);
145+
infiniteCycleViewPager.stopAutoScroll();
146+
```
147+
136148
Other methods check out in sample.
137149

138150
And XML init:

app/src/main/java/com/gigamole/sample/screens/VerticalPagerFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ public void onViewCreated(final View view, @Nullable final Bundle savedInstanceS
2929
final VerticalInfiniteCycleViewPager verticalInfiniteCycleViewPager =
3030
(VerticalInfiniteCycleViewPager) view.findViewById(R.id.vicvp);
3131
verticalInfiniteCycleViewPager.setAdapter(new VerticalPagerAdapter(getContext(), null));
32+
33+
verticalInfiniteCycleViewPager.setScrollDuration(1000);
34+
verticalInfiniteCycleViewPager.startAutoScroll(true);
3235
}
3336
}

infinitecycleviewpager/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ apply plugin: "com.jfrog.bintray"
1919
apply plugin: 'com.github.dcendents.android-maven'
2020
apply plugin: 'maven'
2121

22-
version = "1.0.1"
22+
version = "1.0.2"
2323

2424
android {
2525
compileSdkVersion 23
@@ -29,7 +29,7 @@ android {
2929
minSdkVersion 11
3030
targetSdkVersion 23
3131
versionCode 1
32-
versionName "1.0.1"
32+
versionName "1.0.2"
3333
}
3434
buildTypes {
3535
release {

infinitecycleviewpager/src/main/java/com/gigamole/infinitecycleviewpager/HorizontalInfiniteCycleViewPager.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.support.v4.view.ViewPager;
66
import android.util.AttributeSet;
77
import android.view.MotionEvent;
8+
import android.view.View;
9+
import android.view.ViewGroup;
810
import android.view.animation.Interpolator;
911

1012
/**
@@ -150,6 +152,16 @@ public void setOverScrollMode(final int overScrollMode) {
150152
super.setOverScrollMode(OVER_SCROLL_NEVER);
151153
}
152154

155+
@Override
156+
protected boolean addViewInLayout(final View child, final int index, final ViewGroup.LayoutParams params) {
157+
return super.addViewInLayout(child, 0, params);
158+
}
159+
160+
@Override
161+
public void addView(final View child, final int index, final ViewGroup.LayoutParams params) {
162+
super.addView(child, 0, params);
163+
}
164+
153165
@Override
154166
public void setAdapter(final PagerAdapter adapter) {
155167
if (mInfiniteCycleManager == null) super.setAdapter(adapter);
@@ -193,6 +205,12 @@ public void onWindowFocusChanged(final boolean hasWindowFocus) {
193205
super.onWindowFocusChanged(hasWindowFocus);
194206
}
195207

208+
@Override
209+
protected void onDetachedFromWindow() {
210+
if (mInfiniteCycleManager != null) mInfiniteCycleManager.stopAutoScroll();
211+
super.onDetachedFromWindow();
212+
}
213+
196214
@Override
197215
public void setCurrentItem(final int item) {
198216
setCurrentItem(item, true);
@@ -225,4 +243,12 @@ public void invalidateTransformer() {
225243
public void postInvalidateTransformer() {
226244
if (mInfiniteCycleManager != null) mInfiniteCycleManager.postInvalidateTransformer();
227245
}
246+
247+
public void startAutoScroll(final boolean isAutoScrollPositive) {
248+
if (mInfiniteCycleManager != null) mInfiniteCycleManager.startAutoScroll(isAutoScrollPositive);
249+
}
250+
251+
public void stopAutoScroll() {
252+
if (mInfiniteCycleManager != null) mInfiniteCycleManager.stopAutoScroll();
253+
}
228254
}

infinitecycleviewpager/src/main/java/com/gigamole/infinitecycleviewpager/InfiniteCycleManager.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.res.TypedArray;
66
import android.graphics.Rect;
77
import android.os.Build;
8+
import android.os.Handler;
89
import android.support.v4.view.PagerAdapter;
910
import android.support.v4.view.ViewCompat;
1011
import android.support.v4.view.ViewPager;
@@ -113,6 +114,20 @@ class InfiniteCycleManager implements OnNotifyDataSetChangedListener {
113114
// Interpolator of snapping
114115
private Interpolator mInterpolator;
115116

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+
116131
public InfiniteCycleManager(
117132
final Context context,
118133
final ViewPageable viewPageable,
@@ -315,7 +330,7 @@ public PagerAdapter setAdapter(final PagerAdapter adapter) {
315330
public boolean onTouchEvent(final MotionEvent event) {
316331
if (mViewPageable.getAdapter() == null || mViewPageable.getAdapter().getCount() == 0)
317332
return false;
318-
if (mState == ViewPager.SCROLL_STATE_SETTLING || mViewPageable.isFakeDragging()) return false;
333+
if (mIsAutoScroll || mIsInitialItem || mViewPageable.isFakeDragging()) return false;
319334
if (event.getPointerCount() > MIN_POINTER_COUNT || !mViewPageable.hasWindowFocus())
320335
event.setAction(MotionEvent.ACTION_UP);
321336
checkHitRect(event);
@@ -343,8 +358,7 @@ public int setCurrentItem(final int item) {
343358
if (mIsAdapterInitialPosition) {
344359
mIsAdapterInitialPosition = false;
345360
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();
348362
}
349363

350364
// Need to get current position of original adapter. We cant override getCurrentItem() method,
@@ -448,6 +462,23 @@ private void resetScaleBy() {
448462
mCenterScaleBy = (mMaxPageScale - mMinPageScale) * 0.5F;
449463
}
450464

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+
451482
@Override
452483
public void onChanged() {
453484
mIsDataSetChanged = true;
@@ -705,8 +736,6 @@ public void onPageScrolled(
705736

706737
// We need to rewrite states when is dragging and when setCurrentItem() from idle
707738
if (mState != ViewPager.SCROLL_STATE_SETTLING || mIsInitialItem) {
708-
mIsInitialItem = false;
709-
710739
// Detect first state from idle
711740
if (mOuterPageScrolledState == PageScrolledState.IDLE && positionOffset > 0) {
712741
mPageScrolledPosition = mViewPageable.getCurrentItem();
@@ -740,6 +769,8 @@ else if (mOuterPageScrolledState == PageScrolledState.GOING_RIGHT && goingRight)
740769
mWasPlusOne = false;
741770
mIsLeftPageBringToFront = false;
742771
mIsRightPageBringToFront = false;
772+
773+
mIsInitialItem = false;
743774
}
744775
}
745776

infinitecycleviewpager/src/main/java/com/gigamole/infinitecycleviewpager/VerticalInfiniteCycleViewPager.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.support.v4.view.ViewPager;
66
import android.util.AttributeSet;
77
import android.view.MotionEvent;
8+
import android.view.View;
9+
import android.view.ViewGroup;
810
import android.view.animation.Interpolator;
911

1012
/**
@@ -150,6 +152,16 @@ public void setOverScrollMode(final int overScrollMode) {
150152
super.setOverScrollMode(OVER_SCROLL_NEVER);
151153
}
152154

155+
@Override
156+
protected boolean addViewInLayout(final View child, final int index, final ViewGroup.LayoutParams params) {
157+
return super.addViewInLayout(child, 0, params);
158+
}
159+
160+
@Override
161+
public void addView(final View child, final int index, final ViewGroup.LayoutParams params) {
162+
super.addView(child, 0, params);
163+
}
164+
153165
@Override
154166
public void setAdapter(final PagerAdapter adapter) {
155167
if (mInfiniteCycleManager == null) super.setAdapter(adapter);
@@ -193,6 +205,12 @@ public void onWindowFocusChanged(final boolean hasWindowFocus) {
193205
super.onWindowFocusChanged(hasWindowFocus);
194206
}
195207

208+
@Override
209+
protected void onDetachedFromWindow() {
210+
if (mInfiniteCycleManager != null) mInfiniteCycleManager.stopAutoScroll();
211+
super.onDetachedFromWindow();
212+
}
213+
196214
@Override
197215
public void setCurrentItem(final int item) {
198216
setCurrentItem(item, true);
@@ -225,4 +243,12 @@ public void invalidateTransformer() {
225243
public void postInvalidateTransformer() {
226244
if (mInfiniteCycleManager != null) mInfiniteCycleManager.postInvalidateTransformer();
227245
}
246+
247+
public void startAutoScroll(final boolean isAutoScrollPositive) {
248+
if (mInfiniteCycleManager != null) mInfiniteCycleManager.startAutoScroll(isAutoScrollPositive);
249+
}
250+
251+
public void stopAutoScroll() {
252+
if (mInfiniteCycleManager != null) mInfiniteCycleManager.stopAutoScroll();
253+
}
228254
}

0 commit comments

Comments
 (0)