gpt4 book ai didi

android - viewpager + webview,如何停止webview加载

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:24 25 4
gpt4 key购买 nike

你好,

我创建了一个使用 ViewPager 的应用,所有页面都包含 WebView。每个 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); 

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

     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 个位置的页面)未正确加载。显示空白页面,如何解决?

提前致谢

最佳答案

这解决了我的问题。

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

这里我在字典中添加网页 View

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

清理网页 View 来自字典

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

关于android - viewpager + webview,如何停止webview加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9988628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com