【发布时间】:2011-08-16 09:11:16
【问题描述】:
Helo Evryone!
我正在尝试在我的应用程序中添加“滑动”功能,但我无法这样做。我有故事可以从左向右滑动,反之亦然。我编写了一个子类 MYGestureListener,它具有以下实现。
class MyGestureListener extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.e("Now", "onFling");
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// checking if current index is the last index
if (ind == items.size() - 1 || ind > items.size() - 1) {
Log.e("Last Index", "Can't move further anymore");
return false;
} else {
ind++;
loadAndShowNextActivity();
}
}
// left to right
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (ind <= 0) {
Log.d("First Index", "Can't move back anymore");
return false;
} else {
ind--;
loadAndShowNextActivity();
}
}
} catch (Exception e) {
System.out.println("Exception occured while flinging");
e.printStackTrace();
}
return false;
}
}
然后我在主类中覆盖了 onTouchEvent() 函数。实现如下:
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("Now","onTouchEvent");
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
然后在主类的onCreate中调用它的实例如下:
gestureDetector = new GestureDetector(new MyGestureListener());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
它不起作用,请提出建议并确定我哪里出错了。提前致谢..:-)
【问题讨论】:
-
@Raghu .. 没有错误。但功能没有实现。当我滑动时没有任何反应。
标签: android uigesturerecognizer gesture-recognition