SimpleOnGestureListener onSingleTapUp从未调用过

时间:2015-02-10 09:20:46

标签: android gesturedetector

我用onTouchListener和手势检测器创建了一个自定义组件, 我将自定义组件放在MainActivity的xml文件中,该文件同时包含onTouchEvent和手势检测器。 我想检测自定义组件上的单个点击并长按MainActivity,但似乎某些方式触摸侦听器互动并且单个点击从未被检测到。

MainActivity.java:

public class MainActivity extends ActionBarActivity {
private GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    detector = new GestureDetector(this, new LongPressDetector());
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    detector.onTouchEvent(event);
    int action = event.getActionMasked();
    switch (action){
        case MotionEvent.ACTION_DOWN:{
            Log.d("TouchEvent", "Action_Down at MainActivity.java");
            break;
        }
    }
    return super.onTouchEvent(event);
}

private class LongPressDetector extends GestureDetector.SimpleOnGestureListener{
    @Override
    public boolean onDown(MotionEvent e) {
        Log.d("TouchEvent", "onDown at MainActivity.java");
        return super.onDown(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Log.d("TouchEvent", "onLongPress at MainActivity.java");
        super.onLongPress(e);
    }
}
}

CustomView.java:

public class CustomView extends RelativeLayout {
private GestureDetector detector;

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

private void init(Context c){
    LayoutInflater layoutInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.customview, this);
    detector = new GestureDetector(c, new TapDetector());
    this.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            detector.onTouchEvent(event);
            int action = event.getActionMasked();
            switch (action){
                case MotionEvent.ACTION_DOWN:{
                    Log.d("TouchEvent", "Action_Down at CustomView.java");
                    break;
                }
            }
            return false;
        }
    });
}

private class TapDetector extends GestureDetector.SimpleOnGestureListener{
    @Override
    public boolean onDown(MotionEvent e) {
        Log.d("TouchEvent", "onDown at CustomView.java");
        return super.onDown(e);
    }
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.d("TouchEvent", "onSingleTapUp at CustomView.java");
        return super.onSingleTapUp(e);
    }
}
}

4 个答案:

答案 0 :(得分:14)

如果您要响应一个tap,你还需要从onDown返回true,默认将返回false(我在这里猜测)。

至少对我来说,在确保onSingleTapUp返回true之前,我没有得到onDown我的代码。

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

我见过其他框架,只有在接受'down'事件时才会传递'up'事件,所以我认为这就是这里发生的事情。

答案 1 :(得分:3)

无需猜测(@alexndr)!!! 查看来源

如果你将GestureDetector.SimpleOnGestureListener调用扩展到super.onDown(事件)

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

将返回false,但如果你实现手势检测器监听器,则没有超级!

public class GestureDetector {
/**
 * The listener that is used to notify when gestures occur.
 * If you want to listen for all the different gestures then implement
 * this interface. If you only want to listen for a subset it might
 * be easier to extend {@link SimpleOnGestureListener}.
 */
public interface OnGestureListener {

    /**
     * Notified when a tap occurs with the down {@link MotionEvent}
     * that triggered it. This will be triggered immediately for
     * every down event. All other events should be preceded by this.
     *
     * @param e The down motion event.
     */
    boolean onDown(MotionEvent e);

PS。规则很简单:

  

如果EVENT消费则返回   如果没有返回错误

答案 2 :(得分:0)

您只需要在onTouchEvent(MotionEvent event)中返回true。

有点像这样:

@Override
public boolean onTouchEvent(MotionEvent event) {
    detector.onTouchEvent(event);
    int action = event.getActionMasked();
    switch (action){
        case MotionEvent.ACTION_DOWN:{
            Log.d("TouchEvent", "Action_Down at MainActivity.java");
            break;
        }
    }
    return true;
}

答案 3 :(得分:0)

您需要在onSingleTapUp中设置return true,如下所示,用于事件

布尔布尔onSingleTapUp(MotionEvent e){返回true; }