WebView活动中的Android左/右滑动手势(点击链接和垂直滚动)

t3irkdon  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(244)

SOLVED,解决方案在下面。
我想将Webview中的导航按钮替换为2个滑动手势-左和右滑动。这个Android: How to handle right to left swipe gestures链接确实帮助了我,但我遇到了问题-实现此代码后,我无法在Webview活动中点击链接/或滚动加载的网页(我已经使用了可能答案中的第一个答案)。
然后我发现在这个问题的另一个答案是类似的东西-如何在listview中滚动-https://stackoverflow.com/a/20005481/3272449,但我无法让它运行.在logcat中,我得到了以下调试信息:http://s27.postimg.org/5qfipgi1v/message.png
这是我的OnSwipeTouchListener:

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

public class OnSwipeTouchListener implements OnTouchListener {

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 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();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public GestureDetector getGestureDetector(){
    return  gestureDetector;
}
}

和我的webview活动(剥离):

public class WebViewActivity extends Activity {
private WebView browser;
    private OnSwipeTouchListener onSwipeTouchListener;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
            onSwipeTouchListener = new OnSwipeTouchListener() {
public void onSwipeRight() {
    Toast.makeText(WebViewActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
    Toast.makeText(WebViewActivity.this, "left", Toast.LENGTH_SHORT).show();
}
};

browser.setOnTouchListener(onSwipeTouchListener);
} // end of onCreate
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
    onSwipeTouchListener.getGestureDetector().onTouchEvent(ev); 
        return super.dispatchTouchEvent(ev);   
}
} // end of WebViewActivity

如何使左右滑动、垂直滚动和点击链接同时工作?
解决方案:同样的问题:Fling Gesture and Webview in Android

zf2sa74q

zf2sa74q1#

我对GestureDetector不够熟悉,无法可靠地诊断您的问题。您可以尝试克隆事件:

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
    onSwipeTouchListener.getGestureDetector().onTouchEvent(MotionEvent.obtain(ev)); 
    return super.dispatchTouchEvent(ev);   
}

如果无法做到这一点,另一种方法是使用onOverScrolled而不是GestureDetector

@Override
protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    if (scrollX > (getWidth() + THRESHOLD) && clampedX) {
        onSwipeLeft();
    }
    if (scrollX < - THRESHOLD && clampedX) {
        onSwipeRight();
    }
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}

这确实假设您的内容通常不能横向滚动。

相关问题