Shruti Sharma
Shruti Sharma

Reputation: 177

Implementing horizontal swipe and vertical scroll simultaneously on webview

I have implemented webview in my app.It loads new webpage on horizontal swipe.But the after implementing swipe it does not allow vertical scroll of webpage.Is there any way to implement horizontal swipe mainting vertical scroll??I am new to android kindly suggest, here is my code:

TestActivity.java

     w1 = (WebView) findViewById(R.id.webView1);

    w1.setOnTouchListener(new OnSwipeTouchScreen() {

        public void onSwipeRight() {

            //functioning on right swipe
        }
        public void onSwipeLeft() {
           //functioning on left swipe
        }
      }
    });

OnSwipeTouchScreen.java

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchScreen implements OnTouchListener {

@SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(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 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;
    }
 }
 public void onSwipeRight() {
 }

 public void onSwipeLeft() {
 }
}

Upvotes: 3

Views: 1334

Answers (2)

Enes Altınkaya
Enes Altınkaya

Reputation: 124

Here is how i solved it.

If "y difference" is bigger than "x difference" return false, meaning dont swallow the touch event.

Otherwise trigger swipe left/right and return true.

 public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
            float yDiff = Math.abs(Math.abs(event1.getRawY()) - Math.abs(event2.getRawY()));
            float xDiff = Math.abs(Math.abs(event1.getRawX()) - Math.abs(event2.getRawX()));
            if (yDiff > 250 || yDiff > xDiff) {
                return false;
            }
            if (event1.getRawX() > event2.getRawX()) {
                loadUrl("javascript:window.swipeLeft()");
            } else {
                loadUrl("javascript:window.swipeRight()");
            }
            return true;
        }

Upvotes: 1

Autocrab
Autocrab

Reputation: 3757

If there is no any "LeftToRight" or "RightToLeft" or other touch events, that you need to handle by yourself, then call webView.onTouchEvent(someMotionEvent);

Upvotes: 0

Related Questions