嗨,
我已经创建了一个使用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); 正在执行,这意味着are视图是在后台加载的。如果我滑动4个或更多的页面,所有的网页视图都会加载。
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页上一页、当前页、下一页),
在Viewpager中,2页(表示1个位置页)未正确加载。它显示一个空白页面,我如何解决它?
提前感谢
发布于 2012-07-10 14:13:52
这解决了我的问题。
我已经创建了一个字典,它包含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();
}发布于 2012-04-22 06:45:43
你有没有试过将"OffscreenPageLimit“设置为1?根据这篇文章:ViewPager.setOffscreenPageLimit(0) doesn't work as expected,0是一个无效值
另外,您是否尝试过处理一些页面之间的切换事件,使得每次切换页面时,当前页面的webview将停止加载,或者开始加载一个空的html文件?
https://stackoverflow.com/questions/9988628
复制相似问题