gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:56 26 4
gpt4 key购买 nike

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

我的 Activity 课

 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();

}

最佳答案

替换此事件:

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

它应该工作

关于android - SimpleOnGestureListener 永远不会进入 onFling(...) 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9313607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com