I have a simple slide action for LinearLayout View using TouchListener.
The problem is MotionEvent.ACTION_MOVE keeps being called when I touch and move.
How can I stop MotionEvent.ACTION_MOVEfrom being called?
v.setOnTouchListener(null) is not good: returns false and is not working.
I want MotionEvent.ACTION_MOVE to be called just one time.
private View.OnTouchListener touchEvent = new View.OnTouchListener() {
private float tempX;
private final float BASE_VALUE = 100;
private final boolean LEFT = false;
private final boolean RIGHT = true;
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
tempX = event.getX();
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
if(event.getX() < (tempX + BASE_VALUE)){
changeView(LEFT);
}else if(event.getX() > (tempX + BASE_VALUE)){
changeView(RIGHT);
}
}
It is not working perfectly.
But this implementation is not bad (this code is my idea).
The code:
private View.OnTouchListener touchEvent = new View.OnTouchListener() {
private VelocityTracker mVelocityTracker;
private float tempX;
private final int MIN_DISTANCE = 120;
private final int THRESHOLD_VELOCITY = 200;
private final boolean LEFT = false;
private final boolean RIGHT = true;
private MotionEvent cancelEvent = MotionEvent.obtain(100, 100, MotionEvent.ACTION_CANCEL, 0, 0, 0);
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
Log.d(TAG, "Down");
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(event);
tempX = event.getX();
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(100);
float velocityX = mVelocityTracker.getXVelocity();
if(tempX - event.getX() > MIN_DISTANCE
&& Math.abs(velocityX) > THRESHOLD_VELOCITY){
Log.d(TAG, "LEFT");
changeView(LEFT);
}else if(event.getX() - tempX > MIN_DISTANCE
&& Math.abs(velocityX) > THRESHOLD_VELOCITY){
Log.d(TAG, "RIGHT");
changeView(RIGHT);
}
v.dispatchTouchEvent(cancelEvent);
}
Make SWIPELISTENER.
The Code.