Fix crash with gestures

This commit is contained in:
inorichi 2015-12-22 13:22:13 +01:00
parent 322f54380d
commit 72b2471619
2 changed files with 64 additions and 48 deletions

View File

@ -37,44 +37,52 @@ public class HorizontalViewPager extends ViewPager implements ViewPagerInterface
@Override @Override
public boolean onInterceptTouchEvent(MotionEvent ev) { public boolean onInterceptTouchEvent(MotionEvent ev) {
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) { try {
if (getCurrentItem() == 0 || getCurrentItem() == getAdapter().getCount() - 1) { if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
startDragX = ev.getX(); if (getCurrentItem() == 0 || getCurrentItem() == getAdapter().getCount() - 1) {
startDragX = ev.getX();
}
} }
}
return super.onInterceptTouchEvent(ev); return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
return true;
}
} }
@Override @Override
public boolean onTouchEvent(MotionEvent ev) { public boolean onTouchEvent(MotionEvent ev) {
if (onChapterBoundariesOutListener != null) { try {
if (getCurrentItem() == 0) { if (onChapterBoundariesOutListener != null) {
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) { if (getCurrentItem() == 0) {
float displacement = ev.getX() - startDragX; if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
float displacement = ev.getX() - startDragX;
if (ev.getX() > startDragX && displacement > getWidth() * SWIPE_TOLERANCE) { if (ev.getX() > startDragX && displacement > getWidth() * SWIPE_TOLERANCE) {
onChapterBoundariesOutListener.onFirstPageOutEvent(); onChapterBoundariesOutListener.onFirstPageOutEvent();
return true; return true;
}
startDragX = 0;
} }
} else if (getCurrentItem() == getAdapter().getCount() - 1) {
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
float displacement = startDragX - ev.getX();
startDragX = 0; if (ev.getX() < startDragX && displacement > getWidth() * SWIPE_TOLERANCE) {
} onChapterBoundariesOutListener.onLastPageOutEvent();
} else if (getCurrentItem() == getAdapter().getCount() - 1) { return true;
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) { }
float displacement = startDragX - ev.getX();
if (ev.getX() < startDragX && displacement > getWidth() * SWIPE_TOLERANCE) { startDragX = 0;
onChapterBoundariesOutListener.onLastPageOutEvent();
return true;
} }
startDragX = 0;
} }
} }
}
return super.onTouchEvent(ev); return super.onTouchEvent(ev);
} catch (IllegalArgumentException e) {
return true;
}
} }
@Override @Override

View File

@ -36,44 +36,52 @@ public class VerticalViewPager extends VerticalViewPagerImpl implements ViewPage
@Override @Override
public boolean onInterceptTouchEvent(MotionEvent ev) { public boolean onInterceptTouchEvent(MotionEvent ev) {
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) { try {
if (getCurrentItem() == 0 || getCurrentItem() == getAdapter().getCount() - 1) { if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
startDragY = ev.getY(); if (getCurrentItem() == 0 || getCurrentItem() == getAdapter().getCount() - 1) {
startDragY = ev.getY();
}
} }
}
return super.onInterceptTouchEvent(ev); return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
return true;
}
} }
@Override @Override
public boolean onTouchEvent(MotionEvent ev) { public boolean onTouchEvent(MotionEvent ev) {
if (onChapterBoundariesOutListener != null) { try {
if (getCurrentItem() == 0) { if (onChapterBoundariesOutListener != null) {
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) { if (getCurrentItem() == 0) {
float displacement = ev.getY() - startDragY; if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
float displacement = ev.getY() - startDragY;
if (ev.getY() > startDragY && displacement > getHeight() * SWIPE_TOLERANCE) { if (ev.getY() > startDragY && displacement > getHeight() * SWIPE_TOLERANCE) {
onChapterBoundariesOutListener.onFirstPageOutEvent(); onChapterBoundariesOutListener.onFirstPageOutEvent();
return true; return true;
}
startDragY = 0;
} }
} else if (getCurrentItem() == getAdapter().getCount() - 1) {
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
float displacement = startDragY - ev.getY();
startDragY = 0; if (ev.getY() < startDragY && displacement > getHeight() * SWIPE_TOLERANCE) {
} onChapterBoundariesOutListener.onLastPageOutEvent();
} else if (getCurrentItem() == getAdapter().getCount() - 1) { return true;
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) { }
float displacement = startDragY - ev.getY();
if (ev.getY() < startDragY && displacement > getHeight() * SWIPE_TOLERANCE) { startDragY = 0;
onChapterBoundariesOutListener.onLastPageOutEvent();
return true;
} }
startDragY = 0;
} }
} }
}
return super.onTouchEvent(ev); return super.onTouchEvent(ev);
} catch (IllegalArgumentException e) {
return true;
}
} }
@Override @Override