我对OntouchListener中的MotionEvent有问题。ACTION_DOWN工作得很好,但它从不使用action\u up
我真的不知道我的问题在哪里,所以如果有人能帮我,那就太好了,谢谢。

public class MainActivity extends Activity {
    View.OnTouchListener gestureListener;
    private static final int MIN_DISTANCE = 50;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        layout.setOnTouchListener(new View.OnTouchListener() {

            float downX = 0, downY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                switch(event.getAction()){
                case MotionEvent.ACTION_DOWN :
                        downX = event.getX();
                        downY = event.getY();
                        Toast.makeText(getApplicationContext(), "action down", Toast.LENGTH_SHORT).show();
                    break;
                case MotionEvent.ACTION_UP :

                    Toast.makeText(getApplicationContext(), "action up", Toast.LENGTH_SHORT).show();
                        float deltaX = downX - event.getX();
                        Toast.makeText(getApplicationContext(), Math.abs(deltaX)+"k", Toast.LENGTH_SHORT).show();
                        if (Math.abs(deltaX) > 3){
                            if(deltaX < 0) { onLeftToRightSwipe(); return true; }
                            if(deltaX > 0) { onRightToLeftSwipe(); return true; }
                        }
                        else {
                                String str = "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE;
                                Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
                                return false;
                        }
                    break;
                }
                return false;
            }
        });
    }


    private void onRightToLeftSwipe() {
        Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
    }

    private void onLeftToRightSwipe() {
        Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
    }


}

最佳答案

如果在action_down时返回false到onTouch,则在action_up时将不会收到通知。
在action_down时返回true告诉android您希望继续接收此触摸事件的更新。返回false表示您不想接收进一步的更新。

07-28 04:05