[英]MotionEvent.ACTION_UP not working properly
I was Trying to implement onTouchListner for my custome listview for detecting swipe. 我正在尝试为我的自定义列表视图实现onTouchListner,以检测滑动。 in this code both switch cases (
ACTION_DOWN
, ACTION_UP
) works , but when ACTION_UP
triggers the values I got in the downx,downy becomes 0 . 在这段代码中,这两种切换方式(
ACTION_DOWN
, ACTION_UP
)都可以工作,但是当ACTION_UP
触发我在ACTION_UP
的值时,downy变为0。 and the code for detecting swipes is always saying swiped right(even when I just click!!) 并且用于检测滑动的代码总是说向右滑动(即使我只是单击!!!)
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
float downx = 0,downy = 0, upx = 0, upy = 0, movex = 0, movey = 0;
switch(event.getAction())
{
case(MotionEvent.ACTION_DOWN):
{
downx = event.getX();
downy = event.getY();
Log.i("TOUCH","DOWN");
break;
}
case(MotionEvent.ACTION_UP):
{
upx = event.getX();
upy = event.getY();
Log.i("TOUCH","UP");
if(downx < upx)
{
Log.w("TODO","Swiped right");
}
else if(upx < downx)
{
Log.w("TODO","Swiped left");
}
break;
}
}
Log.i("TOUCH_DOWN","x = "+downx+"y = "+ downy);
Log.i("TOUCH_MOVE","x = "+movex+"y = "+ movey);
Log.i("TOUCH_UP","x = "+upx+"y = "+ upy);
return true;
}
});
return view;
}
}
Is there any way to make it work advises needed!! 有什么方法可以使它发挥作用吗? thanks
谢谢
@Override
public boolean onTouch(View v, MotionEvent event) {
float downx = 0,downy = 0, upx = 0, upy = 0, movex = 0, movey = 0;
onTouch
is called for each touch event, reinitializing all your local members. 每个触摸事件都会调用
onTouch
,从而重新初始化所有本地成员。 If you want to avoid this, keep it as class members, 如果您想避免这种情况,请将其保留为班级成员,
float downx = 0,downy = 0, upx = 0, upy = 0, movex = 0, movey = 0;
They get initialized everytime onTouch() is called. 每次调用onTouch()时,它们都会初始化。
Declare them globally as class members. 将它们声明为班级成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.