Note that there are some explanatory texts on larger screens.

plurals
  1. POOnFling MotionEvent e1 null?
    primarykey
    data
    text
    <p>Ok. This thing almost drives me crazy.</p> <p>Some days ago I could implement the code below and the toast was successfully called when I tried to swipe right or left. </p> <p>However, now it can't be called, because the e1 is always null! </p> <p>How could this happen? I try this code on the emulator, but a user reports me, that it also doesn't work on real device.</p> <pre><code>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)&lt;SWIPE_MAX_OFF_PATH &amp;&amp; Math.abs(velocityX)&gt;=SWIPE_THRESHOLD_VELOCITY &amp;&amp; Math.abs(dX)&gt;=SWIPE_MIN_DISTANCE ) { if (dX&gt;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; } } </code></pre> <p>The code that implement the gesture:</p> <pre><code>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; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload