【发布时间】: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