Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleOnGestureListener never goes in to the onFling(...) method

Hi SimpleOnGestureListener dosen't work in my application this is how I'm implementing it. Maybe you can spot what is wrong. The thing is debugging shows the application never goes in to the onFling(...) methode and gdt.onTouchEvent(event); always returns false :/ Any ideas?? Thanks

my activity class

 public class SimpleActivity extends Activity{


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

    final ImageView imageView = (ImageView) findViewById(R.id.gggbbb);
     imageView.setOnTouchListener(new OnFlingGestureListener() {

        @Override
        public void onTopToBottom() {
           System.out.println("top");
        }

        @Override
        public void onRightToLeft() {
            System.out.println("right");
        }

        @Override
        public void onLeftToRight() {
            System.out.println("left");
        }

        @Override
        public void onBottomToTop() {
            System.out.println("bottom");
        }
     });



}


    }

my abstract Listener

 package com.dmd.client.detailsMenus;

 import android.view.GestureDetector;
 import android.view.GestureDetector.SimpleOnGestureListener;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnTouchListener;

 public abstract class OnFlingGestureListener implements OnTouchListener {

  private final GestureDetector gdt = new GestureDetector(new GestureListener());

  @Override
  public boolean onTouch(final View v, final MotionEvent event) {
     return gdt.onTouchEvent(event);
  }

  private final class GestureListener extends SimpleOnGestureListener {

     private static final int SWIPE_MIN_DISTANCE = 60;
     private static final int SWIPE_THRESHOLD_VELOCITY = 100;

     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
         if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
             onRightToLeft();
             return true;
          } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
             onLeftToRight();
             return true;
          }
          if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
             onBottomToTop();
             return true;
          } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
             onTopToBottom();
             return true;
          }
          return false;
     }
  }

  public abstract void onRightToLeft();

  public abstract void onLeftToRight();

  public abstract void onBottomToTop();

  public abstract void onTopToBottom();

}
like image 748
user1048282 Avatar asked Feb 16 '12 15:02

user1048282


1 Answers

Replace this event:

@Override
public boolean onTouch(final View v, final MotionEvent event) {
  gdt.onTouchEvent(event);
  return true;
}

it should work

like image 157
Stachu Avatar answered Oct 24 '22 01:10

Stachu