2013-10-12 91 views
0

问题是我必须几乎严格地水平滑动才能执行滑动操作。滑动时我想要更多的自由。我想在对角滑动时滑动也可以工作。我应该如何更改此代码?我应该如何更改此代码以便能够更自由地向左/向右滑动?

这里是代码

public class OnSwipeTouchListener implements OnTouchListener { 

Context context = null; 

private final GestureDetector gestureDetector = new GestureDetector(context, new GestureListener()); 

public boolean onTouch(final View view, final MotionEvent motionEvent) { 
    return gestureDetector.onTouchEvent(motionEvent); 
} 



private final class GestureListener extends SimpleOnGestureListener { 

    private static final int SWIPE_THRESHOLD = 100; 
    private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

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

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean result = false; 
     try { 
      float diffY = e2.getY() - e1.getY(); 
      float diffX = e2.getX() - e1.getX(); 
      if (Math.abs(diffX) > Math.abs(diffY)) { 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
         onSwipeRight(); 
        } else { 
         onSwipeLeft(); 
        } 
       } 
      } else { 
       if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffY > 0) { 
         onSwipeBottom(); 
        } else { 
         onSwipeTop(); 
        } 
       } 
      } 
     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
     return result; 
    } 
} 
+0

看看我的答案,也应该制定出适合你 – superuser

+0

亦请打小接受的答案复选标记下的upvote它这个答案适用于你:)这样我们都获得了一定的声誉。 – superuser

+0

谢谢你的答案,我还没有尝试过。我会检查它是否有效。 –

回答

1

更改此:

@Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean result = false; 
     try { 
      float diffY = e2.getY() - e1.getY(); 
      float diffX = e2.getX() - e1.getX(); 
      ------> if (Math.abs(diffX) > Math.abs(diffY)) { <------ 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
         onSwipeRight(); 
        } else { 
         onSwipeLeft(); 
        } 
       } 
      } else {..... 

这样:

@Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean result = false; 
     try { 
      float diffY = e2.getY() - e1.getY(); 
      float diffX = e2.getX() - e1.getX(); 
      ------> if (Math.abs(diffX) > Math.abs(diffY) - 50) { <------ 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
         onSwipeRight(); 
        } else { 
         onSwipeLeft(); 
        } 
       } 
      } else {..... 

这将让你在小莫刷卡而不是对角线,并且仍然可以作为一侧到另一侧的滑动。

这也会让你不得不直接向上或向下滑动。

如果要向上挥动回应,你可以这样做:

@Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean result = false; 
     try { 
      float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
         onSwipeRight(); 
        } else { 
         onSwipeLeft(); 
        } 
       } 
     } catch (Exception exception) { 
     exception.printStackTrace(); 
    } 
    return result; 
+0

嗨试过你的答案。刷卡工作,但没有改善。 –

相关问题