1

我有一个 ImageView 需要在投掷/幻灯片上更改图像并在 onclicks 时执行其他操作但我无法管理获取 onclick 以触发,投掷工作很好

这是 Listener 的课程

    public abstract class OnFlingGestureListener implements OnTouchListener {


      private Context context;
    private final GestureDetector gdt = new GestureDetector(this.context, new GestureListener());

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

      private final class GestureListener extends SimpleOnGestureListener {

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

         @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 boolean onDown(MotionEvent e){    
          return false;
      }

      public boolean onSingleTapConfirmed(MotionEvent e) {
          // ---Call it directly---
          click();
          return true;
      }

      public abstract void onRightToLeft();

      public abstract void onLeftToRight();

      public abstract void onBottomToTop();

      public abstract void onTopToBottom();

      public abstract void click();

    }

然后在我的活动 onCreate 我这样做

ImageView btn = (ImageView) findViewById(R.id.imageButton1);        
        btn.setOnTouchListener(new OnFlingGestureListener() {

            @Override
            public void onTopToBottom() {
               //Your code here
            }

            @Override
            public void onRightToLeft() {
                NextCall();
            }

            @Override
            public void onLeftToRight() {               
                PrevCall();
            }

            @Override
            public void onBottomToTop() {
               //Your code here
            }

            @Override
            public void click() {
                ClickCode();

            }
         });

任何想法我做错了什么?

4

1 回答 1

2

我设法让它工作的方法 onDown 和 onFling 需要返回 true 并且我缺少方法 onSingleTapConfirmed 的 @Override

类的最终代码

public abstract class OnFlingGestureListener implements OnTouchListener {

    private final GestureDetector gdt = new GestureDetector(this.context, new GestureListener());

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

  private Context context;  
  private static final String tag = "MyActivity";

  private final class GestureListener extends SimpleOnGestureListener {

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


     public boolean onDown(MotionEvent evt){         
            return true;
        }


     @Override
      public boolean onSingleTapConfirmed(MotionEvent e) {
          click();
          return true;
      } 


     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
         Log.d(tag , "fling");
        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 true;
     }
  }



  public abstract void onRightToLeft();

  public abstract void onLeftToRight();

  public abstract void onBottomToTop();

  public abstract void onTopToBottom();

  public abstract void click();


}
于 2013-04-26T20:54:30.850 回答