好的。这件事几乎让我发疯。
几天前,我可以实现下面的代码,当我尝试向右或向左滑动时,toast 被成功调用。
但是,现在不能调用了,因为e1一直是null!
怎么会这样?我在模拟器上尝试了这段代码,但一位用户向我报告说,它在真实设备上也不起作用。
public class SwipeGestureListener extends GestureDetector.SimpleOnGestureListener{
private static final int SWIPE_MIN_DISTANCE = 150;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
private final Activity activity;
protected MotionEvent mLastOnDownEvent = null;
public SwipeGestureListener(Activity activity) {
this.activity = activity;
}
@Override
public boolean onDown(MotionEvent e) {
mLastOnDownEvent = e;
System.out.println(e);
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
System.out.println(e1 + " " + e2);
if (e1==null)
e1 = mLastOnDownEvent;
if (e1==null || e2==null)
return false;
float dX = e2.getX()-e1.getX();
float dY = e2.getY()-e1.getY();
if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {
if (dX>0) {
Toast.makeText(activity.getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
activity.fetchPrevious();
} else {
Toast.makeText(activity.getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
activity.fetchNext();
}
return true;
}
return false;
}
}
实现手势的代码:
final GestureDetector gdt = new GestureDetector(this, new SwipeGestureListener(this));
listview.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
gdt.onTouchEvent(event);
return false;
}
});
最佳答案
我尝试了您的代码,但确实没有用,然后我将 GestureDetector 更改为较新/更新的 API“GestureDetectorCompat”,并进一步更改了一些内容: 使用 GestureDetector.OnGestureListener 而不是 OnSimpleGestureListener, onDown() 应该返回 true 然后它起作用了。我无法向您解释,确切的问题是什么。但是变更线似乎有问题。也许有人可以解释潜在的问题。查看正在运行的代码:
public class MainActivity extends Activity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.imageView1);
final GestureDetectorCompat gdt = new GestureDetectorCompat(this, new SwipeGestureListener(this));
imageView.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
return gdt.onTouchEvent(event);
}
});
}
public class SwipeGestureListener implements GestureDetector.OnGestureListener{
private static final int SWIPE_MIN_DISTANCE = 150;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
private final Activity activity;
protected MotionEvent mLastOnDownEvent = null;
public SwipeGestureListener(Activity activity) {
this.activity = activity;
}
@Override
public boolean onDown(MotionEvent e) {
mLastOnDownEvent = e;
System.err.println("ondown");
//System.out.println(e);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
System.err.println("onFling");
System.out.println(e1 + " " + e2);
if (e1==null)
e1 = mLastOnDownEvent;
if (e1==null || e2==null)
return false;
float dX = e2.getX()-e1.getX();
float dY = e2.getY()-e1.getY();
if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {
if (dX>0) {
Toast.makeText(activity.getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
// activity.fetchPrevious();
} else {
Toast.makeText(activity.getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
// activity.fetchNext();
}
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
}
请试试吧!
关于android - OnFling MotionEvent e1 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390873/