Android scroll left / right to drag pages and up / down to scroll in webView

I am creating an OnSwipeTouchListener class:

public class OnSwipeTouchListener implements OnTouchListener {
private GestureDetector gestureDetector;
public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, 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;
    }
    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @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) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeUp() {
}
public void onSwipeDown() {
}
}

      

And in ItemActivity I have a webView (mWebView) and placed in an html file from assets. After that, I put it on the Touch Listener:

mWebView.setOnTouchListener(new OnSwipeTouchListener(this) {
        @Override
        public void onSwipeDown() {
        }
        @Override
        public void onSwipeUp() {

        }

      

I am implementing onSwipeLeft and onSwipeUp correctly, loading other html's, but not working Scrolling up and scrolling down to scroll up and down in the selected html, so I have a problem to show the full html.

How do I implement SwipeDown and SwipeUp to work with vertical scrolling?

+3


source to share


1 answer


I go to ItemActivity to this override:

mWebView.setOnTouchListener(new OnSwipeTouchListener(this) {
        @Override
        public void onSwipeDown() {
            mWebView.pageUp(false);
        }
        @Override
        public void onSwipeUp() {
            mWebView.pageDown(false);
        }

      



and it works correctly. Maybe someone would be helpful.

This solution is ok but doesn't work perfectly, I need another solution for this.

0


source







All Articles