Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried your code and indeed it didn't work, then I changed the GestureDetector to the newer/updated API "GestureDetectorCompat" and changed a few further things: Used the GestureDetector.OnGestureListener instead of the OnSimpleGestureListener, onDown() should return true and then it worked. I can't explain it to you, what was the exact problem. But the change lines seemed to be problematic. Maybe someone can explain the underlying problem. Have a look at the code, which is working: </p> <pre><code>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)&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; } @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; } } } </code></pre> <p>Please try it!</p>
    singulars
    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.
    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