簡體   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