Android - Trouble with swipe gesture

余生颓废 提交于 2019-12-01 20:11:28
5hssba

In your SimpleOnGestureListener, override onDown for your gestures to register. It can just return true but it has to be defined like this..

@Override
    public boolean onDown(MotionEvent e) {
            return true;
    }

....See this link.. and the comment below the answer..

You need to change a couple of things, here's an example.

Set your OnTouchListener:

root.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return false;
                }
                return false;
            }
        });

SwipeGesture class:

class SwipeGesture extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Do something

                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Do something
                return true;

            }

        } catch (Exception e) {
            Log.e("Fling", "There was an error processing the Fling event:"
                    + e.getMessage());
        }
        return true;
    }

    // Necessary for the onFling event to register
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
}

It looks like you're swiping between Tabs. Using Fragments and ViewPager is much easier and smoother for your users.

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