Skip to content

Commit e9ad47e

Browse files
committed
Do not perform any ViewPager operations on ACTION_CANCEL.
Closes JakeWharton#108.
1 parent f8d595f commit e9ad47e

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

library/src/com/viewpagerindicator/CirclePageIndicator.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,8 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
286286
return false;
287287
}
288288

289-
final int action = ev.getAction();
290-
291-
switch (action & MotionEventCompat.ACTION_MASK) {
289+
final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
290+
switch (action) {
292291
case MotionEvent.ACTION_DOWN:
293292
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
294293
mLastMotionX = ev.getX();
@@ -324,10 +323,14 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
324323
final float sixthWidth = width / 6f;
325324

326325
if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
327-
mViewPager.setCurrentItem(mCurrentPage - 1);
326+
if (action != MotionEvent.ACTION_CANCEL) {
327+
mViewPager.setCurrentItem(mCurrentPage - 1);
328+
}
328329
return true;
329330
} else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
330-
mViewPager.setCurrentItem(mCurrentPage + 1);
331+
if (action != MotionEvent.ACTION_CANCEL) {
332+
mViewPager.setCurrentItem(mCurrentPage + 1);
333+
}
331334
return true;
332335
}
333336
}
@@ -339,8 +342,7 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
339342

340343
case MotionEventCompat.ACTION_POINTER_DOWN: {
341344
final int index = MotionEventCompat.getActionIndex(ev);
342-
final float x = MotionEventCompat.getX(ev, index);
343-
mLastMotionX = x;
345+
mLastMotionX = MotionEventCompat.getX(ev, index);
344346
mActivePointerId = MotionEventCompat.getPointerId(ev, index);
345347
break;
346348
}
@@ -357,7 +359,7 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
357359
}
358360

359361
return true;
360-
};
362+
}
361363

362364
@Override
363365
public void setViewPager(ViewPager view) {
@@ -456,7 +458,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
456458
* @return The width of the view, honoring constraints from measureSpec
457459
*/
458460
private int measureLong(int measureSpec) {
459-
int result = 0;
461+
int result;
460462
int specMode = MeasureSpec.getMode(measureSpec);
461463
int specSize = MeasureSpec.getSize(measureSpec);
462464

@@ -484,7 +486,7 @@ private int measureLong(int measureSpec) {
484486
* @return The height of the view, honoring constraints from measureSpec
485487
*/
486488
private int measureShort(int measureSpec) {
487-
int result = 0;
489+
int result;
488490
int specMode = MeasureSpec.getMode(measureSpec);
489491
int specSize = MeasureSpec.getSize(measureSpec);
490492

@@ -537,6 +539,7 @@ public void writeToParcel(Parcel dest, int flags) {
537539
dest.writeInt(currentPage);
538540
}
539541

542+
@SuppressWarnings("UnusedDeclaration")
540543
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
541544
@Override
542545
public SavedState createFromParcel(Parcel in) {

library/src/com/viewpagerindicator/LinePageIndicator.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,8 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
198198
return false;
199199
}
200200

201-
final int action = ev.getAction();
202-
203-
switch (action & MotionEventCompat.ACTION_MASK) {
201+
final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
202+
switch (action) {
204203
case MotionEvent.ACTION_DOWN:
205204
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
206205
mLastMotionX = ev.getX();
@@ -236,10 +235,14 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
236235
final float sixthWidth = width / 6f;
237236

238237
if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
239-
mViewPager.setCurrentItem(mCurrentPage - 1);
238+
if (action != MotionEvent.ACTION_CANCEL) {
239+
mViewPager.setCurrentItem(mCurrentPage - 1);
240+
}
240241
return true;
241242
} else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
242-
mViewPager.setCurrentItem(mCurrentPage + 1);
243+
if (action != MotionEvent.ACTION_CANCEL) {
244+
mViewPager.setCurrentItem(mCurrentPage + 1);
245+
}
243246
return true;
244247
}
245248
}
@@ -251,8 +254,7 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
251254

252255
case MotionEventCompat.ACTION_POINTER_DOWN: {
253256
final int index = MotionEventCompat.getActionIndex(ev);
254-
final float x = MotionEventCompat.getX(ev, index);
255-
mLastMotionX = x;
257+
mLastMotionX = MotionEventCompat.getX(ev, index);
256258
mActivePointerId = MotionEventCompat.getPointerId(ev, index);
257259
break;
258260
}
@@ -269,7 +271,7 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
269271
}
270272

271273
return true;
272-
};
274+
}
273275

274276
@Override
275277
public void setViewPager(ViewPager viewPager) {
@@ -351,7 +353,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
351353
* @return The width of the view, honoring constraints from measureSpec
352354
*/
353355
private int measureWidth(int measureSpec) {
354-
float result = 0;
356+
float result;
355357
int specMode = MeasureSpec.getMode(measureSpec);
356358
int specSize = MeasureSpec.getSize(measureSpec);
357359

@@ -378,7 +380,7 @@ private int measureWidth(int measureSpec) {
378380
* @return The height of the view, honoring constraints from measureSpec
379381
*/
380382
private int measureHeight(int measureSpec) {
381-
float result = 0;
383+
float result;
382384
int specMode = MeasureSpec.getMode(measureSpec);
383385
int specSize = MeasureSpec.getSize(measureSpec);
384386

@@ -430,6 +432,7 @@ public void writeToParcel(Parcel dest, int flags) {
430432
dest.writeInt(currentPage);
431433
}
432434

435+
@SuppressWarnings("UnusedDeclaration")
433436
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
434437
@Override
435438
public SavedState createFromParcel(Parcel in) {

library/src/com/viewpagerindicator/TitlePageIndicator.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,8 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
549549
return false;
550550
}
551551

552-
final int action = ev.getAction();
553-
554-
switch (action & MotionEventCompat.ACTION_MASK) {
552+
final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
553+
switch (action) {
555554
case MotionEvent.ACTION_DOWN:
556555
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
557556
mLastMotionX = ev.getX();
@@ -591,17 +590,21 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
591590

592591
if (eventX < leftThird) {
593592
if (mCurrentPage > 0) {
594-
mViewPager.setCurrentItem(mCurrentPage - 1);
593+
if (action != MotionEvent.ACTION_CANCEL) {
594+
mViewPager.setCurrentItem(mCurrentPage - 1);
595+
}
595596
return true;
596597
}
597598
} else if (eventX > rightThird) {
598599
if (mCurrentPage < count - 1) {
599-
mViewPager.setCurrentItem(mCurrentPage + 1);
600+
if (action != MotionEvent.ACTION_CANCEL) {
601+
mViewPager.setCurrentItem(mCurrentPage + 1);
602+
}
600603
return true;
601604
}
602605
} else {
603606
//Middle third
604-
if (mCenterItemClickListener != null) {
607+
if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
605608
mCenterItemClickListener.onCenterItemClick(mCurrentPage);
606609
}
607610
}
@@ -614,8 +617,7 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
614617

615618
case MotionEventCompat.ACTION_POINTER_DOWN: {
616619
final int index = MotionEventCompat.getActionIndex(ev);
617-
final float x = MotionEventCompat.getX(ev, index);
618-
mLastMotionX = x;
620+
mLastMotionX = MotionEventCompat.getX(ev, index);
619621
mActivePointerId = MotionEventCompat.getPointerId(ev, index);
620622
break;
621623
}
@@ -632,7 +634,7 @@ public boolean onTouchEvent(android.view.MotionEvent ev) {
632634
}
633635

634636
return true;
635-
};
637+
}
636638

637639
/**
638640
* Set bounds for the right textView including clip padding.
@@ -791,7 +793,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
791793
final int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
792794

793795
//Determine our height
794-
float height = 0;
796+
float height;
795797
final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
796798
if (heightSpecMode == MeasureSpec.EXACTLY) {
797799
//We were told how big to be
@@ -844,6 +846,7 @@ public void writeToParcel(Parcel dest, int flags) {
844846
dest.writeInt(currentPage);
845847
}
846848

849+
@SuppressWarnings("UnusedDeclaration")
847850
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
848851
@Override
849852
public SavedState createFromParcel(Parcel in) {

library/src/com/viewpagerindicator/UnderlinePageIndicator.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ public boolean onTouchEvent(MotionEvent ev) {
186186
return false;
187187
}
188188

189-
final int action = ev.getAction();
190-
191-
switch (action & MotionEventCompat.ACTION_MASK) {
189+
final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
190+
switch (action) {
192191
case MotionEvent.ACTION_DOWN:
193192
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
194193
mLastMotionX = ev.getX();
@@ -224,10 +223,14 @@ public boolean onTouchEvent(MotionEvent ev) {
224223
final float sixthWidth = width / 6f;
225224

226225
if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
227-
mViewPager.setCurrentItem(mCurrentPage - 1);
226+
if (action != MotionEvent.ACTION_CANCEL) {
227+
mViewPager.setCurrentItem(mCurrentPage - 1);
228+
}
228229
return true;
229230
} else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
230-
mViewPager.setCurrentItem(mCurrentPage + 1);
231+
if (action != MotionEvent.ACTION_CANCEL) {
232+
mViewPager.setCurrentItem(mCurrentPage + 1);
233+
}
231234
return true;
232235
}
233236
}
@@ -239,8 +242,7 @@ public boolean onTouchEvent(MotionEvent ev) {
239242

240243
case MotionEventCompat.ACTION_POINTER_DOWN: {
241244
final int index = MotionEventCompat.getActionIndex(ev);
242-
final float x = MotionEventCompat.getX(ev, index);
243-
mLastMotionX = x;
245+
mLastMotionX = MotionEventCompat.getX(ev, index);
244246
mActivePointerId = MotionEventCompat.getPointerId(ev, index);
245247
break;
246248
}
@@ -257,7 +259,7 @@ public boolean onTouchEvent(MotionEvent ev) {
257259
}
258260

259261
return true;
260-
};
262+
}
261263

262264
@Override
263265
public void setViewPager(ViewPager viewPager) {
@@ -384,6 +386,7 @@ public void writeToParcel(Parcel dest, int flags) {
384386
dest.writeInt(currentPage);
385387
}
386388

389+
@SuppressWarnings("UnusedDeclaration")
387390
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
388391
@Override
389392
public SavedState createFromParcel(Parcel in) {

0 commit comments

Comments
 (0)