Stojan Kukrika
Stojan Kukrika

Reputation: 312

Android swipe left/right to pagging and up/down to scroll in webView

I create 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 put into html file from assets. After that I put on it Touch Listener:

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

        }

I implement onSwipeLeft and onSwipeUp correctly, to load other html-s but do not work Swipe up and Swipe down, to scroll up and down in selected html, so I have problem to show complete html.

How to implement SwipeDown and SwipeUp to vertical scroll work?

Upvotes: 3

Views: 1156

Answers (1)

Stojan Kukrika
Stojan Kukrika

Reputation: 312

I change into ItemActivity to this override:

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

and works correctly. Maybe, someone would be helpful.

It solution is OK but not work perfectly, I need a other solution for this.

Upvotes: 1

Related Questions