0

I am trying to display different image on the action of fling. For this, I have used the SimpleOnGestureListener. But fling is not working. Can you guys help me to identify the reason. Here is the code

public class MainActivity extends Activity{
ImageView m_imageView;
GestureDetector m_gdt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.v("INFO", "Inside oncreate");

    m_gdt = new GestureDetector(this, new GestureListener());

    m_imageView = (ImageView)findViewById(R.id.imageView1);

    m_imageView.setOnTouchListener(new OnTouchListener() {
        @Override 
        public boolean onTouch(final View view, final MotionEvent event) {
            Log.v("INFO", "Inside onTouch");

            return m_gdt.onTouchEvent(event);
        }
    });

}
} 

public class GestureListener extends SimpleOnGestureListener{

private final int SWIPE_MIN_DISTANCE = 120;
private final int SWIPE_THRESHOLD_VELOCITY = 200;

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    Log.v("INFO", "Fling detected");
    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        // Right to left, your code here
        Log.v("INFO", "Image moving forward");
        return true;
    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) >  SWIPE_THRESHOLD_VELOCITY) {
        // Left to right, your code here
        Log.v("INFO", "Image moving backward");
        return true;
    }
    return false;
}
}

The log message 'Inside onTouch' is shown. But the method onFling never gets called. What am I missing here?

Raj
  • 2,368
  • 6
  • 34
  • 52
  • at first glance it doesn't appear that your onFling is actually attached in any way to the image that you've attached the touch event to. I believe you need to extend your activity for the onFling event to ever get called. – Yevgeny Simkin Oct 14 '12 at 02:52

1 Answers1

0

Use the third answer from this question (from Thomas Fankhauser) and it will work: Fling gesture detection on grid layout

Community
  • 1
  • 1
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236