将OnFling和OnClick方法添加到ImageView

时间:2013-04-25 21:39:55

标签: android

我有一个ImageView,它需要在fling / slide上更改图像,并在onclicks时执行其他操作,但是我无法管理get onclick以进行触发,这是一个很好的想法

这是听众的课程

    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();

            }
         });

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:2)

我设法让它使用方法onDown和onFling需要返回true并且我错过了onOingleTapConfirmed方法的@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();


}