滑动检测器手势android [英] Swipt detector gestures android

查看:62
本文介绍了滑动检测器手势android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onTouchEvent(event);
}
private void onLeftSwipe() {
// Do something
    System.out.println("left swipe");
}

private void onRightSwipe() {
// Do something
    System.out.println("right swipe");
}

// Private class for gestures
private class SwipeGestureDetector extends SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            float diffAbs = Math.abs(e1.getY() - e2.getY());
            float diff = e1.getX() - e2.getX();

            if (diffAbs > SWIPE_MAX_OFF_PATH)
            return false;

            // Left swipe
            if (diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                SlidingMenuActivity.this.onLeftSwipe();

                // Right swipe
            } else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                SlidingMenuActivity.this.onRightSwipe();
            }
        } catch (Exception e) {
            Log.e("YourActivity", "Error on gestures");
        }
        return false;
    }
}

我知道了,但是它不起作用,我无法检测到向左或向右滑动.我在类的顶部和onCreate的活动方法中声明了private GestureDetector gestureDetector;.

I have that but its not working, I am not able to detect swiping left or right. I have declared private GestureDetector gestureDetector; on top of my class, and in onCreate method of activity.

gestureDetector = new GestureDetector(new SwipeGestureDetector());

但是它不起作用,什么都没打印出来?我正在使用kitkat nexus 4.

But its not working, nothing is printed? I'm using kitkat nexus 4.

推荐答案

您必须将侦听器应用于您希望对其进行处理的View,例如:

You must apply the listener to the View you like it to work on, something like:

myView.setOnTouchListener(new SwipeGestureDetector(this)....

这篇关于滑动检测器手势android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆