Reputation: 1341
I was Trying to implement onTouchListner for my custome listview for detecting swipe.
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 .
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
Upvotes: 1
Views: 978
Reputation: 22527
float downx = 0,downy = 0, upx = 0, upy = 0, movex = 0, movey = 0;
They get initialized everytime onTouch() is called.
Declare them globally as class members.
Upvotes: 0
Reputation: 157457
@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. If you want to avoid this, keep it as class members,
Upvotes: 1