SimpleOnGestureListener 永远不会进入 onFling(...) 方法

发布于 2025-01-06 10:37:57 字数 2795 浏览 0 评论 0原文

您好,SimpleOnGestureListener 在我的应用程序中不起作用,这就是我实现它的方式。也许你能发现问题所在。问题是调试显示应用程序永远不会进入 onFling(...) 方法和 gdt.onTouchEvent(event);总是返回 false :/ 有什么想法吗?感谢

我的活动课

 public class SimpleActivity extends Activity{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simpleLayout);

    final ImageView imageView = (ImageView) findViewById(R.id.gggbbb);
     imageView.setOnTouchListener(new OnFlingGestureListener() {

        @Override
        public void onTopToBottom() {
           System.out.println("top");
        }

        @Override
        public void onRightToLeft() {
            System.out.println("right");
        }

        @Override
        public void onLeftToRight() {
            System.out.println("left");
        }

        @Override
        public void onBottomToTop() {
            System.out.println("bottom");
        }
     });



}


    }

我的抽象听众

 package com.dmd.client.detailsMenus;

 import android.view.GestureDetector;
 import android.view.GestureDetector.SimpleOnGestureListener;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnTouchListener;

 public abstract class OnFlingGestureListener implements OnTouchListener {

  private final GestureDetector gdt = new GestureDetector(new GestureListener());

  @Override
  public boolean onTouch(final View v, final MotionEvent event) {
     return gdt.onTouchEvent(event);
  }

  private final class GestureListener extends SimpleOnGestureListener {

     private static final int SWIPE_MIN_DISTANCE = 60;
     private static final int SWIPE_THRESHOLD_VELOCITY = 100;

     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
         if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
             onRightToLeft();
             return true;
          } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
             onLeftToRight();
             return true;
          }
          if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
             onBottomToTop();
             return true;
          } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
             onTopToBottom();
             return true;
          }
          return false;
     }
  }

  public abstract void onRightToLeft();

  public abstract void onLeftToRight();

  public abstract void onBottomToTop();

  public abstract void onTopToBottom();

}

Hi SimpleOnGestureListener dosen't work in my application this is how I'm implementing it. Maybe you can spot what is wrong. The thing is debugging shows the application never goes in to the onFling(...) methode and gdt.onTouchEvent(event); always returns false :/
Any ideas?? Thanks

my activity class

 public class SimpleActivity extends Activity{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simpleLayout);

    final ImageView imageView = (ImageView) findViewById(R.id.gggbbb);
     imageView.setOnTouchListener(new OnFlingGestureListener() {

        @Override
        public void onTopToBottom() {
           System.out.println("top");
        }

        @Override
        public void onRightToLeft() {
            System.out.println("right");
        }

        @Override
        public void onLeftToRight() {
            System.out.println("left");
        }

        @Override
        public void onBottomToTop() {
            System.out.println("bottom");
        }
     });



}


    }

my abstract Listener

 package com.dmd.client.detailsMenus;

 import android.view.GestureDetector;
 import android.view.GestureDetector.SimpleOnGestureListener;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnTouchListener;

 public abstract class OnFlingGestureListener implements OnTouchListener {

  private final GestureDetector gdt = new GestureDetector(new GestureListener());

  @Override
  public boolean onTouch(final View v, final MotionEvent event) {
     return gdt.onTouchEvent(event);
  }

  private final class GestureListener extends SimpleOnGestureListener {

     private static final int SWIPE_MIN_DISTANCE = 60;
     private static final int SWIPE_THRESHOLD_VELOCITY = 100;

     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
         if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
             onRightToLeft();
             return true;
          } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
             onLeftToRight();
             return true;
          }
          if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
             onBottomToTop();
             return true;
          } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
             onTopToBottom();
             return true;
          }
          return false;
     }
  }

  public abstract void onRightToLeft();

  public abstract void onLeftToRight();

  public abstract void onBottomToTop();

  public abstract void onTopToBottom();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2025-01-13 10:37:57

替换此事件:

@Override
public boolean onTouch(final View v, final MotionEvent event) {
  gdt.onTouchEvent(event);
  return true;
}

它应该有效

Replace this event:

@Override
public boolean onTouch(final View v, final MotionEvent event) {
  gdt.onTouchEvent(event);
  return true;
}

it should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文