WebViewClient第二次没有调用

时间:2014-07-01 08:23:52

标签: javascript android webview android-webview webviewclient

我有WebView我用来在我的应用中本地加载一些html内容。它首先加载内容,然后调用JavaScript函数,然后将WebView滚动到特定位置。

以下代码说明了我如何执行此操作:

public class MyActivity extends Activity {

private WebView web1;
private int ID;
private MyWebViewClient webViewClient1;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        // Get the ID of the law to be loaded.
        ID = getIntent().getIntExtra("element_id", 1);

        web1 = (WebView) findViewById(R.id.web1);
        web1.getSettings().setJavaScriptEnabled(true);

        // Initialize the webViewClients.
        webViewClient1 = new MyWebViewClient(true);

        web1.setWebViewClient(webViewClient1);

        displayArticle(web1);
    }

private void displayArticle (WebView wv) {

StringBuilder sb = new StringBuilder();

// Code to build the HTML String.

String finalHtml = sb.toString();

            wv.loadDataWithBaseURL("file:///android_asset/html/", finalHtml, "text/html", "UTF-8", null);

}

private class MyWebViewClient extends WebViewClient {
        String urlToLoad;

        MyWebViewClient (boolean setUrlToLoad) {
            if (setUrlToLoad) {
                setUrlToLoad();
            }           
        }

        public void setUrlToLoad () {
            this.urlToLoad = "javascript:(function () {" +
                    "var elem = document.getElementById('e"+ID+"');" +
                    "var x = 0;" +
                    "var y = 0;" +
                    "while (elem != null) {" +
                    "x += elem.offsetLeft;" +
                    "y += elem.offsetTop;" +
                    "elem = elem.offsetParent;" +
                    "}" +
                    "window.scrollTo(x, y);" +
                    "})()";
        }

        @Override 
        public void onPageStarted (WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.d("Pages", "Page loading started");
        }

        @Override
        public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            Log.d("Pages", "Webview content load error");
        }

        @Override
        public void onPageFinished (WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d("Pages", "Page loading finished");

            if (urlToLoad != null) {
                // Scroll to the position.
                view.loadUrl(urlToLoad);
                urlToLoad = null;
            }
        }
    }
}

在上面的代码中,MyWebViewClient类中的回调函数是使用wv.loadDataWithBaseURL函数中的displayArticle(WebView wv)为第一个请求调用的,但是当请求完成时{{1}调用时,onPageFinished调用不会调用来自view.loadUrl(urlToLoad);的另一组回调。我不太清楚为什么,因为它必须与我最初使用的MyWebViewClient相同,并且它应该具有相同的WebView集实例。

此外,我使用相同的MyWebViewClient进行了其他loadUrl次调用,此行为仍然存在。

如果有人能解释为什么会这样,我真的很感激。

1 个答案:

答案 0 :(得分:0)

loadUrl("javascript:...")有点特殊情况:它会在当前页面的上下文中评估JavaScript代码(就像<a href='javascript:...'>clicky</a>那样),因此你不会得到onPageStarted / { {1}}回调。

相关问题