我有一个GirdView
和onTouchListener
,我想知道当有人用手指在屏幕上滑动时的方向,我可以很容易地左右移动,但我也想上下移动。
但我的问题是。。
float currentX;
float currentY;
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("Showpatch", "Down Triggered " + x + ", " + y);
currentX = x;
currentY = y;
break;
case MotionEvent.ACTION_UP:
Log.d("Showpatch", "Up Triggered" + x + ", " + y);
Log.d("Showpatch", "Testing Left... " + x + ", " + y);
if (currentX > x) {
Log.d("Showpatch", "Moved Left");
return false;
}
Log.d("Showpatch", "Testing Right... " + x + ", " + y);
if (currentX < x) {
Log.d("Showpatch", "Moved Right");
return false;
}
Log.d("Showpatch", "Testing Down... " + x + ", " + y);
if (currentY < y) {
Log.d("Showpatch", "Moved Down");
return false;
}
Log.d("Showpatch", "Testing Up... " + x + ", " + y);
if (currentY < y) {
Log.d("Showpatch", "Moved Up");
return false;
}
break;
}
return false;
}
正如我所说,它可以左右移动,没有问题,但它从来没有检测到向上或向下,因为它在到达向上/向下语句之前会触发左/右,除非你准确地上下移动你的手指,否则它不起作用。。。
有没有一种方法可以让用户上下滑动?
非常感谢您的帮助,谢谢。
我相信手势监听器可以帮助你,它是在1.6 中引入的
http://developer.android.com/resources/articles/gestures.html
欢呼tobias