Reputation: 1
I am having a listview
, I want to detect the touch movement up and down. I am using this code. However, when I touch the listview both UP
& DOWN
are called. Why? I used log to view up & down movement. What I do with listview
are both in log.
This is the code:
lstviewOrders.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
Log.w("down:","YES");
break;
case MotionEvent.ACTION_UP:
Log.w("UP:","YES");
break;
}
return false;
}
});
Upvotes: 0
Views: 47
Reputation: 376
If you want to check SWIPE_UP
and SWIPE_DOWN
you have to implement your own OnTouchListener
or you can you this one :
OnSwipeTouchListener.java:
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View view, final MotionEvent motionEvent) {
super.onTouch(view, motionEvent);
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {}
public void onSwipeLeft() {}
public void onSwipeTop() {}
public void onSwipeBottom() {}
}
and apply it to your listView
like this :
lstviewOrders.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeTop() {
Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0
Reputation: 12365
Look at the documentation of MotionEvent
The ACTION_UP
- link - you get when pressed gesture has finished and the ACTION_DOWN
- link - you get when a pressed gesture has started. Every time when you make pressed gesture you call both.
The ACTION_UP
and ACTION_DOWN
are not the same as swipe up and swipe down.
Upvotes: 1