Detecting Swipe and Tap gestures

心已入冬 提交于 2019-12-04 17:15:27

Here is what you can do,found out on this link In onCreate of activity, add this.

final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
calendar.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(final View view, final MotionEvent event) {
   if (gestureDetector.onTouchEvent(event)) {
      return false;
    }
    return true;
  }
});

And here is GestureDetector Class

class GestureListener extends SimpleOnGestureListener {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
    // Trigger the touch event on the calendar
      calendar.onTouchEvent(event);
      return super.onSingleTapUp(event);
    }

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    ViewConfiguration viewConfiguration = ViewConfiguration.get(EventsActivity.this);
    int minSwipeDistance = viewConfiguration.getScaledPagingTouchSlop();
    int minSwipeVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
    int maxSwipeOffPath = viewConfiguration.getScaledTouchSlop();
    if (Math.abs(e1.getY() - e2.getY()) > maxSwipeOffPath) {
        return false;
    }

   if (Math.abs(velocityX) > minSwipeVelocity) {
   // Right to left swipe
      if (e1.getX() - e2.getX() > minSwipeDistance) {
      calendar.nextMonth();
   }

   // Left to right
   else if (e2.getX() - e1.getX() > minSwipeDistance) {
        calendar.previousMonth();
   }

    // Call some app-related functions to update the display
    displayDate(calendar.getMonth(), calendar.getYear());
  }
  return false;
 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!