【问题标题】:viewpager + webview, how to stop webview loadingviewpager + webview,如何停止 webview 加载
【发布时间】:2012-07-10 14:13:52
【问题描述】:

嗨,

我创建了一个使用 ViewPager 的应用程序,并且所有页面都包含 WebViews。 每个 Webview 都包含服务器图像、视频、mp3 ...

WebView 设置是

      public Object instantiateItem(View collection, int position) {
            .
            .
            .

            final RelativeLayout imageLayout  = (RelativeLayout)         
            inflater.inflate(R.layout.omb_webview_layout, null);
            WebView webview = (WebView) imageLayout.findViewById(R.id.video_view);
            ProgressBar pbImage = (ProgressBar) imageLayout.findViewById(R.id.progressBar1);

            webview.requestFocus(View.FOCUS_DOWN);
            webview.getSettings().setBuiltInZoomControls(false);
            webview.getSettings().setSupportZoom(true);
            webview.getSettings().setUseWideViewPort(true);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setLoadWithOverviewMode(true);
            webview.getSettings().setUseWideViewPort(true);
            webview.setWebViewClient(new mWebViewClient());
            webview.getSettings().setPluginsEnabled(true);
            webview.clearCache(true);
            webview.clearHistory();
            if(Pbar != null) {
                webview.setWebChromeClient(new WebChromeClient() {
                 public  void onProgressChanged(WebView view, int progress)     {
                  Log.i(TAG, "webview loading progress : "+ progress);
                    if(progress == 100) {
                        try {
                            Pbar.setVisibility(ProgressBar.GONE);
                        } catch (Exception e) {
                        Log.e(TAG, "timer progress GONE : "+ progress,e);
                        }
                         }
            });
            }

问题是当页面被滑动时,这一行也是如此

 Log.i(TAG, "webview loading progress : "+ progress); 

正在执行,这意味着 webviews 正在后台加载。 如果我滑动 4 个或更多页面,所有 webview 都在加载。

     destroyItem(View collection, int position, Object o) {
    Log.d("DESTROY", "destroying view at position sheetal " + position);

            RelativeLayout view = (RelativeLayout)o;

                    WebView tmp = webviewDictionary.get(position);;

            tmp.getSettings().setJavaScriptEnabled(false);
                            tmp.stopLoading();
        if(tmp != null){
                        Log.i(TAG, "sheetal webview is destroyed " +position );
                        try {
                            tmp.onPause();

                            tmp.loadDataWithBaseURL("about:blank", "<html></html>","text/html", "UTF-8", null);
                            tmp=null;
                            //  tmp.destroy();
                        }
                        catch (Exception e) {
                            Log.i(TAG, "webview destroyed Exception 0" ,e);
                        }
                    } 

如果页面不可见,如何停止后台加载? setOffscreenPageLimit(1)(表示上一页、当前页、下一页),

在 Viewpager 中,2 个页面(表示 1 个位置页面)未正确加载。显示一个空白页,如何解决?

提前致谢

【问题讨论】:

  • 我使用过 stopLoading(),但它不会停止后台加载,每当调用 destroyItem(...) 时 webview 停止加载对吗?但不会停止加载。
  • 不完全确定,但您不需要将 OffscreenPageLimit 设置为 0 吗?
  • 我试过 pager.OffscreenPageLimit(0) 但不起作用。
  • 那么,您在内存中卡住了 3 页?这意味着上一页和下一页不会被破坏并继续加载。在这种情况下,我建议您跟踪上一页和下一页并自己调用这些页面的 stopLoading()。
  • 是的,我被困在内存中的 3 页,我在字典中存储了活动的 3 页,每当任何页面超出范围时,我都会为特定页面或活动调用 stopLoading() 完成我我正在为所有页面调用 stopLoading();但它不会停止加载...

标签: android webview android-viewpager


【解决方案1】:

这解决了我的问题。

我创建了一个字典,它包含 3 个以位置为键的 webview。

这里我在字典中添加 webviews

public Object instantiateItem(View collection, int position) {
    .
    .
    .
    webviewDictionary.put(position, webView);
    .
    .
    .
}

清理网页视图来自字典

public void cleanAllWebViews() {

    try {

        if(webviewDictionary != null){

            Iterator<Integer> webViewIterator = webviewDictionary.keySet().iterator();

            while(webViewIterator.hasNext()) {

                Integer key=(Integer)webViewIterator.next();

                WebView webView = (WebView) webviewDictionary.get(key);


                webView.stopLoading();
                //                  webView.pauseTimers();

                webView.setWebChromeClient(null);
                webView.setWebViewClient(null);


                webView.loadDataWithBaseURL("about:blank", "<html></html>","text/html", "UTF-8", null);
                webViewIterator.remove();

            }
        }

        webviewDictionary = null;


    }catch (Exception e) {
        Log.e(TAG, "ombook last call onPauseAllWebviews  Exception",e  );
    }
}

    and 

    @Override
public void onBackPressed() {

    cleanAllWebViews();

    System.gc();

    super.onBackPressed();
}

【讨论】:

    【解决方案2】:

    您是否尝试过将“OffscreenPageLimit”设置为 1 ?根据这篇文章,0 是无效值: ViewPager.setOffscreenPageLimit(0) doesn't work as expected

    另外,你有没有尝试过处理一些页面切换的事件,让每次切换页面时,当前页面的webview都会停止加载,或者开始加载一个空的html文件?

    【讨论】:

    • 是的,在导航页面时我暂停了 webview (webview.onPause())
    • 成功了吗?如果可以,能否请您发布相关代码?
    猜你喜欢
    • 2014-07-21
    • 2013-09-27
    • 1970-01-01
    • 2018-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多