【发布时间】:2014-06-22 21:43:32
【问题描述】:
我在 Android 中使用 viewpager,想控制用户滑动页面的方式。
我正在使用此代码来检测滑动的方向:
// Detects the direction of swipe. Right or left.
// Returns true if swipe is in right direction
public boolean detectSwipeToRight(MotionEvent event){
int initialXValue = 0; // as we have to detect swipe to right
final int SWIPE_THRESHOLD = 100; // detect swipe
boolean result = false;
try {
float diffX = event.getX() - initialXValue;
if (Math.abs(diffX) > SWIPE_THRESHOLD ) {
if (diffX > 0) {
// swipe from left to right detected ie.SwipeRight
result = false;
} else {
Log.e("swipeToLeft", "swipeToLeft");
result = true;
}
}
}
catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
然而,此代码总是返回“false”。必须对代码进行哪些更改才能正常工作?
【问题讨论】:
-
你在手势检测监听器中使用这个方法吗?
-
@mapo 不,我在我的自定义类的 onInterceptTouchEvent(MotionEvent event) 方法中使用它,它从 viewpager 扩展而来。
标签: android android-viewpager swipe