繁体   English   中英

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

[英]OnTouch method of OnTouchListener subclass being called many times in Android

我在简单的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!

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

如果要检测一个向下轻扫你需要得到y上统筹ACTION_DOWN和比较它y统筹你得到ACTION_UP

如果ACTION_UPy大于ACTION_UP则向下滑动。

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

您可以尝试使用本教程中介绍的方法来处理滑动。

例如:

// 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
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM