event.getAction() never use MotionEvent.ACTION_UP

此生再无相见时 提交于 2019-12-10 15:21:52

问题


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


}

回答1:


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.



来源:https://stackoverflow.com/questions/20359849/event-getaction-never-use-motionevent-action-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!