Ok. To coś doprowadza mnie do szału.OnFling MotionEvent e1 null?
Kilka dni temu mogłem zaimplementować poniższy kod i toast zadzwonił z powodzeniem, gdy próbowałem przesuwać palcem w prawo lub w lewo.
Jednak teraz nie można go nazwać, ponieważ e1 ma zawsze wartość zerową!
Jak to się mogło stać? Próbuję tego kodu na emulatorze, ale użytkownik zgłasza mi, że nie działa również na prawdziwym urządzeniu.
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;
}
}
kod realizacji gest:
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;
}
});
może ktoś podać prawidłowe rozwiązanie tego jestem też w obliczu tego samego problemu?. – Gibs