1

I am using webview to display HTML file. In webview add swipe left and right. I am using below class to swipe functionality.

/**
 * Detects left and right swipes across a view.
 */
public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public void onSwipeLeft() {
    }

    public void onSwipeRight() {
    }

    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_DISTANCE_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) {
            float distanceX = e2.getX() - e1.getX();
            float distanceY = e2.getY() - e1.getY();
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                    onSwipeRight();
                else
                    onSwipeLeft();
                return true;
            }
            return false;
        }
    }
}

copy from this post.

My code is:

    webview.setOnTouchListener(new OnSwipeTouchListener(this){
        @Override
        public void onSwipeLeft() {
            super.onSwipeLeft();
            onNextClick();
        }

        @Override
        public void onSwipeRight() {
            super.onSwipeRight();
            onPrevClick();
        }

    });

Working fine in swipe left and swipe right.

My problem is:

Webview internal click not working.

Thanks.

Mitesh Vanaliya
  • 2,491
  • 24
  • 39
  • Similar to https://stackoverflow.com/questions/19538747/how-to-use-both-ontouch-and-onclick-for-an-imagebutton ?? – Pankaj Kumar Nov 02 '18 at 05:51
  • @PankajKumar I already added gestureDetector.onTouchEvent(event) in OnTouch event. – Mitesh Vanaliya Nov 02 '18 at 06:01
  • you are always returning `true` in `onDown`, which likely means that you have consumed the finger down event, and it won't be passed onward in the system, which means that click event will never happen, as a click is touch down + touch up. – Vladyslav Matviienko Nov 02 '18 at 06:04
  • @VladyslavMatviienko If I return false or super.onDown(e) in onDown then webview not loaded – Mitesh Vanaliya Nov 02 '18 at 06:07

0 Answers0