在webview上同时实现水平滑动和垂直滚动

时间:2013-12-24 16:52:52

标签: android swipe

我已经在我的应用程序中实现了webview。它在水平滑动上加载新的网页。但是实现滑动后它不允许垂直滚动网页。有没有办法实现水平滑动维护垂直滚动?我是新手安慰你好,这是我的代码:

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() {
 }
}

2 个答案:

答案 0 :(得分:1)

以下是我如何解决它。

如果“y difference”大于“x difference”则返回false,意味着不要吞下触摸事件。

否则触发向左/向右滑动并返回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;
        }

答案 1 :(得分:0)

如果没有任何" LeftToRight"或" RightToLeft"或者您需要自己处理的其他触摸事件,然后拨打webView.onTouchEvent(someMotionEvent);