event.getAction() never use MotionEvent.ACTION_UP
Asked Answered
L

1

5

i have a problem with MotionEvent in the OnTouchListener. The ACTION_DOWN works perfectly, but it never use ACTION_UP I really don't know where is my problem, so if someone can help me with it, that would be really great, thank you.

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


}
Lozar answered 3/12, 2013 at 19:24 Comment(0)
C
17

If you return false to onTouch on Action_Down, you will not get a notification on Action_Up.

Returning true on Action_Down tells Android that you want to continue receiving updates to this touch event. Returning false tells it that you don't want to receive further updates.

Chivers answered 3/12, 2013 at 19:26 Comment(1)
Just tested it and worked perfectly, i'll read more about the function in documentation next time. Really helped me, thanks again.Lozar

© 2022 - 2024 — McMap. All rights reserved.