开发者

Android/WebView doesn't load javascript gallery images when navigated to

开发者 https://www.devze.com 2023-04-11 02:14 出处:网络
I have a website/web app built with jquery mobile that I am trying to package into a webview for android; I am able to load up the pages locally by putting the whole site into the assets fo开发者_开发

I have a website/web app built with jquery mobile that I am trying to package into a webview for android; I am able to load up the pages locally by putting the whole site into the assets fo开发者_开发百科lder and loading the url like so:

mWebView.loadUrl("file:///android_asset/www/btec/index.html");

One of the pages is a gallery/slideshow of images that flips through automatically using javascript. However, if I navigate to my gallery from index.html, the images dont load. If I load it directly (i.e. load it directly into the webview with:

mWebView.loadUrl("file:///android_asset/www/btec/gallery.html");

the images appear and scroll no problem! I'm not sure why this is happening. Is there a setting I need to enable/disable? I have the following settings enabled already:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.getSettings().setPluginsEnabled(true);


This a trick that probably solves your problem.

1) Create a WebViewClient class, overriding shouldOverrideUrlLoading method:

private class MyClient extends WebViewClient {
    private WebView mwv;

    public MyClient(WebView v) {
        mwv = v;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.equals("file:///android_asset/www/btec/gallery.html")) {
            mwv.loadUrl(url);
            return true;
        }
        return false;
    }
}

2) Associate this client to your webview:

mWebView.setWebViewClient(new MyClient(mWebView));

Probably not the best way; but I think it works... ;)

EDIT: for other details, you can see this: http://developer.android.com/reference/android/webkit/WebViewClient.html

0

精彩评论

暂无评论...
验证码 换一张
取 消