双击在Android中滑动?

时间:2012-03-20 07:20:10

标签: java android ontouchlistener gesturedetector

我在DoubleTap in android使用Mohan的答案但是我不知道如何实现它。

我有一个webView,我想检测左右滑动并双击。我怎么能一次实现这两个。请帮助我是新手势。

2 个答案:

答案 0 :(得分:2)

试试这个,

private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

MyGestureDetector

class MyGestureDetector 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;
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

//你的代码

            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

//你的代码

            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "onFling", e);

        }
        return false;
    }
}

通过检查滑动距离,您可以区分触摸和滑动。我希望这会对你有所帮助。

答案 1 :(得分:0)

这是一个处理自己手势的扩展WebView。这可能有助于那些发现外部UI区域正在吸收手势并且难以让听众进入的人。这是我的问题。这是WebView,它可以检测所有自己的滑动和双击:

package org.this_voice.modabrowser;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.Toast;

public class MoDaView extends WebView implements
    GestureDetector.OnDoubleTapListener {

Context mContext;
GestureDetector gestureDetector = new GestureDetector(
        new mGestureDetector());

public MoDaView(Context context) {
    super(context);
    mContext = context;
    gestureDetector.setOnDoubleTapListener(this);
    gestureDetector.setIsLongpressEnabled(true);
}

public MoDaView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
    gestureDetector.setOnDoubleTapListener(this);
    gestureDetector.setIsLongpressEnabled(true);
}

public MoDaView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    gestureDetector.setOnDoubleTapListener(this);
    gestureDetector.setIsLongpressEnabled(true);
}

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

class mGestureDetector extends SimpleOnGestureListener {

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

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
        return super.onScroll(e1, e2, distanceX, distanceY);
    }

    @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;
         // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(mContext, "Left Swipe", Toast.LENGTH_SHORT)
                    .show();
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(mContext, "Right Swipe", Toast.LENGTH_SHORT)
                    .show();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }

}

public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}

@Override
public boolean onDoubleTap(MotionEvent event) {
    Toast.makeText(mContext, "onDoubleTap", Toast.LENGTH_SHORT).show();
    return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent event) {
    Toast.makeText(mContext, "onDoubleTapEvent", Toast.LENGTH_SHORT).show();
    return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
    Toast.makeText(mContext, "onSingleTapConfirmed", Toast.LENGTH_SHORT)
        .show();
    return true;
}
}

这是使用扩展WebView的类中的设置:

wv = (MoDaView) findViewById(R.id.webview);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebSettings wvOptions = wv.getSettings();
wvOptions.setJavaScriptEnabled(true);
wvOptions.setGeolocationEnabled(false);
wvOptions.setBuiltInZoomControls(true);
wvOptions.setUseWideViewPort(true);

这是检测上面的所有手势。我应该补充说,这个类结合了stackoverflow中的大约6个不同的答案。

还有一件事,就是为了拯救人们所经历的一切。双击不是双指敲击。我以为是(长篇故事,我们会跳过它)。双击是一根手指快速两次。双指是两个同时 - 这就是为什么我提到它 - 并非所有的手机都可以检测到它。我打算用它,但现在我会用实际的双击。 Android有两个类“伪造”双指手势,但他们说这些类在任何地方都不起作用。只是一个FYI。