ViewPager + WebView,如何停止WebView加载

5

你好,

我创建了一个应用程序,使用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); 

正在执行,这意味着Web视图在后台加载。 如果我滑动4页或更多页面,则所有Web视图都会加载。

     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)(表示3个页面prev、cur、next)。

在Viewpager中,2页(指1个位置页面)无法正确加载。它显示一个空白白色页面,我该怎么解决?

提前感谢。


我已经使用了stopLoading(),但它并没有停止后台加载,每当调用destroyItem(...)时,webview会停止加载,对吗?但它不会停止加载。 - Sheetal Suryan
不是很确定,但你需要将OffscreenPageLimit设置为0吗? - THelper
我尝试了 pager.OffscreenPageLimit(0) 但是没有起作用。 - Sheetal Suryan
那么,你的内存中只能容纳3页?这意味着前一页和后一页不会被销毁并继续加载。在这种情况下,我建议你跟踪前一页和后一页,并自己调用stopLoading()函数来停止它们的加载。 - THelper
是的,我卡在了内存中的3个页面上。我已经在一个字典中存储了3个活动页面,并且每当任何页面超出范围时,我都会为特定页面调用stopLoading(),或者当所有页面的活动完成时,我都会调用stopLoading()。但它并没有停止加载... - Sheetal Suryan
2个回答

2

这解决了我的问题。

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

在这里我正在向字典中添加webviews。

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

清理Web视图是从字典中进行的。

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();
}

1

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

另外,您是否尝试处理一些页面切换事件,以便每次切换页面时,当前页面的Webview将停止加载,或开始加载空的HTML文件?


1
是的,在浏览页面时,我已经暂停了WebView(WebView.onPause())。 - Sheetal Suryan
1
它是否有效?如果是,请发布相关代码。 - android developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接