且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android OnTouch向上/向下滑动方向更改

更新时间:2022-12-05 16:47:50

是否要检测滑动的方向变化,有一种简单的方法:

Should you want to detect the direction change of the swipe there is a simple way:

    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        findViewById(R.id.myView).setOnTouchListener(this);
        gestureDetector = new GestureDetector(this, this);
    }

您可以像这样实现OnTouch和GestureListener:

And you implement OnTouch and GestureListeners like this:

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if (distanceY > 0){
            // you are going up
        } else {
            // you are going down
        }
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    //the rest of the methods you must implement...