从 WebView 中的 onFling 调用时,LoadURL/goBack 不起作用 [英] LoadURL/goBack Not Working When Called From onFling in WebView

查看:34
本文介绍了从 WebView 中的 onFling 调用时,LoadURL/goBack 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过查看此处的其他帖子,我已经能够让我的 WebView 响应滑动 - 但存在一些问题.我想要完成的是从右向左滑动,就像我活动中的后退按钮一样.这意味着有时调用 webview.goBack 或 LoadURL 以转到上一页,或者如果我在顶部网页调用 super.onBackPressed 以关闭当前的 webview.

By looking at other posts here I've been able to get my WebView to respond to swipes - but with some problems. What I'm trying to accomplish is make a right-to-left swipe act just like the back button in my activity. This means sometimes calling webview.goBack or LoadURL to go to a previous page, or if I'm at the top webpage calling super.onBackPressed to close the current webview.

后一种情况完美运行,但浏览器控件仅部分起作用.如果我打开 webview 并深入两页,然后向后滑动一次,我看不到任何变化.如果我再次向后滑动,webview 就会关闭.这意味着上一个网页是在第一次向后滑动时加载的,但由于某种原因没有显示在屏幕上.如果我使用设备的后退按钮(它调用的功能与我的 onFling 完全相同),则显示会按预期刷新.有谁知道是什么导致了这种行为?它是否与覆盖某处动作的 webview 运动事件有关?

The latter case works perfectly, but the browser controls are only partially working. If I open the webview and go two pages deep, then swipe back once, I see no change. If I swipe back again, the webview gets closed. This means the previous webpage was loaded on the first back swipe, but it wasn't displayed on the screen for some reason. If I use the device's back button (which calls exactly the same function as my onFling), the display refreshes as expected. Does anyone know what could be causing this behavior? Does it have something to do with the webview motion events overriding an action somewhere?

相关代码片段如下.感谢观看.

The relevant pieces of code are below. Thanks for taking a look.

公共类 WebBrowser 扩展 Activity ... {

public class WebBrowser extends Activity ... {

    ...

GestureDetector gestureDetector;

SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY && Math.abs(e2.getX() - e1.getX()) > SWIPE_MIN_DISTANCE) {
            goBack();
            return true;
        }
        return false;
    }
};

public void onCreate(Bundle savedInstanceState) {
        ...
    gestureDetector = new GestureDetector(gestureListener);
    wv.setOnTouchListener(
            new View.OnTouchListener() {
                public boolean onTouch(View wv, MotionEvent event) {
                if(gestureDetector.onTouchEvent(event)) return true;
                return false;
            }
    }
    );

private void goBack() {
    if (backURL.equals("close")) {
        super.onBackPressed();
    } else if (!backURL.equals("")) {
        wv.loadUrl(backURL);
    } else if (wv.canGoBack()) {
        wv.goBack();
    } else {
        super.onBackPressed();
    }
}

}

推荐答案

我遇到了同样的问题.试试:

I had the same problem. Try:

public void onCreate(Bundle savedInstanceState) {
    ...
gestureDetector = new GestureDetector(gestureListener);
wv.setOnTouchListener(
        new View.OnTouchListener() {
            public boolean onTouch(View wv, MotionEvent event) {
                gestureDetector.onTouchEvent(event);
                return false;
            }
        }
);

所以总是返回false,尽管检测器返回true.我知道这不是它应该的样子,但它对我有用.

So return always false, although the detector returned true. I know it's not the way it is supposed to be, but it worked for me.

这篇关于从 WebView 中的 onFling 调用时,LoadURL/goBack 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆