2

我有一个简单的滑动动作来LinearLayout View使用TouchListener

当我触摸和移动时,问题是MotionEvent.ACTION_MOVE不断被调用。

我怎样才能停止MotionEvent.ACTION_MOVE被叫?

v.setOnTouchListener(null)不好:返回 false 并且不工作。

我只想MotionEvent.ACTION_MOVE被叫一次。

    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);
                }
            }

它工作不完美。

但是这个实现还不错(这段代码是我的想法)。

编码:

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);
        }
4

2 回答 2

2

制作 SWIPELISTENER。

编码。

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;


public abstract class OnSwipeListener implements OnTouchListener {
public final static int SWIPED_LEFT = 1;
public final static int SWIPED_RIGHT = 2;
public final static int SWIPED_UP = 3;
public final static int SWIPED_DOWN = 4;
public final static int CLICKED = 5;

private int SWIPE_ACTION = 0;
private float firstTouchX = 0;
private float firstTouchY = 0;
private float lastTouchX = 0;
private float lastTouchY = 0;
private int activePointerID;

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction() == event.ACTION_DOWN){
        final float x = event.getX();
        final float y = event.getY();

        firstTouchX = x;
        firstTouchY = y;

        activePointerID = event.getPointerId(0);

        v.setBackgroundResource(R.drawable.layoutbutton2);
    }

    if(event.getAction() == event.ACTION_UP){
        float dX = lastTouchX - firstTouchX;
        float dY = lastTouchY - firstTouchY;

        final int pointerIndex = event.findPointerIndex(activePointerID);

        if((firstTouchX + 10) == event.getX(activePointerID) &&
                (firstTouchY + 10) == event.getY(activePointerID)){

            SWIPE_ACTION = CLICKED;

        }else{
            if(Math.abs(dX) > Math.abs(dY)){
                if(dX > 0){
                    SWIPE_ACTION = SWIPED_RIGHT;
                }else{
                    SWIPE_ACTION = SWIPED_LEFT;
                }
            }else{
                if(dY > 0){
                    SWIPE_ACTION = SWIPED_DOWN;
                }else{
                    SWIPE_ACTION = SWIPED_UP;
                }
            }
        }
        v.setBackgroundResource(R.drawable.layoutbutton1);
        onSwipe(v, event);
    }
    if(event.getAction() == event.ACTION_MOVE){
        final int pointerIndex = event.findPointerIndex(activePointerID);
        final float x = event.getX(pointerIndex);
        final float y = event.getY(pointerIndex);

        lastTouchX = x;
        lastTouchY = y;

        v.setBackgroundResource(R.drawable.layoutbutton1);
    }
    if(event.getAction() == event.ACTION_CANCEL){
        v.setBackgroundResource(R.drawable.layoutbutton1);
    }

    return true;

}

public int getSwipeTo(){
    return SWIPE_ACTION;
}

abstract public void onSwipe(View v, MotionEvent event);

}
于 2012-06-15T01:24:41.037 回答
0

我假设您在问如何在用户抬起手指时终止事件?您可以调用 ACTION_UP,就像用户从设备上“抬起”手指一样。

 if(event.getAction() == MotionEvent.ACTION_UP){
           //your code, maybe tempX = null;
        }

然后您可以重新初始化您的程序,以便它可以接受另一个用户的触摸。

于 2012-06-12T03:42:17.927 回答