在Android中多次调用OnTouchListener子类的OnTouch方法

时间:2014-09-16 13:47:10

标签: android ontouchlistener

我在简单的Android应用中创建了一个简单的[View.OnTouchListener][1]实现。

我现在的目标是让它检测向下滑动。例如,如果我向下滑动,我希望它能够检测到我已经这样做了。

然而,它成功地做到了这一点,每次向下滑动,它也被称为一大堆其他时间。我可以看到这是因为onTouch方法switch语句中的默认行为:

这是 MySwipeListener

public class MySwipeListener implements View.OnTouchListener{
    private Activity activity;

    public MySwipeListener(Activity activity){
        this.activity = activity;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()){
            case MotionEvent.ACTION_DOWN: {
                Log.i("MotionEvent", "Action Down!");
                Toast.makeText(activity, "Motion Down", Toast.LENGTH_LONG);
                return true;
            }
            default: {
                Log.i("MotionEvent", "Other Action!");
                Toast.makeText(activity, "Other Action", Toast.LENGTH_LONG);
                return true;
            }
        }
    }
}

这就是我在MainActivity中使用它的方式:

    Button topUp = (Button) findViewById(R.id.a);
    topUp.setOnTouchListener(new MySwipeListener(this));

当我在Android屏幕上进行物理刷卡时,我得到的结果如下:

09-16 16:39:48.276  26748-26748/mobi.corp.proj I/MotionEvent﹕ Action Down!
09-16 16:39:48.316  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.326  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.346  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.361  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.376  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.396  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.411  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.426  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.446  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.461  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.476  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.496  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.511  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.526  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.546  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.561  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.576  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.596  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.611  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.626  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.631  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.886  26748-26748/mobi.corp.proj I/MotionEvent﹕ Action Down!
09-16 16:39:48.911  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.926  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.946  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.961  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.976  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.996  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.011  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.026  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.046  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.061  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.076  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.096  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.111  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.126  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.146  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.161  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.176  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.196  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.201  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.206  26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!

当我只是期待一些

I/MotionEvent﹕ Action Down!

3 个答案:

答案 0 :(得分:1)

每次移动手指时都会调用{p> MotionEvent.ACTION_MOVE这是一项连续任务。这将被称为它移动的每个像素。因此,您可能希望从此类中排除MotionEvent.ACTION_MOVE以降低噪音

答案 1 :(得分:1)

正在打印default中的日志,因为当您在屏幕上滑动ACTION_MOVE并且可能会调用ACTION_UP时。

如果您要检测向下滑动,则需要y上的ACTION_DOWN坐标,并将其与y坐标进行比较ACTION_UP }。

如果y中的ACTION_UP大于ACTION_UP,则会向下滑动。

答案 2 :(得分:0)

您可以尝试使用this tutorial中的方法来处理滑动。

例如:

// 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) {
           YourActivity.this.onLeftSwipe();

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

private void onLeftSwipe() {
    // Do something
  }

private void onRightSwipe() {
    // Do something
  }