我在让它正常工作时遇到了一些麻烦。我有一个带有 3 个 webview 的 ViewPager,目前遇到的问题是只有 case 0(我的第一个 webview)实际上正确使用了进度条,(当进度为 100 时停止,等等)。然而,我的其他 2 个 webviews 进度条(案例 1 和 2)没有做任何事情。有什么建议吗?
public class AwesomePagerActivity extends Activity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 3;
private Context context;
private AwesomePagerAdapter awesomeAdapter;
LayoutInflater inflater;
ProgressBar progressBar;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.pager);
awesomePager.setOffscreenPageLimit(3);
awesomePager.setAdapter(awesomeAdapter);
}
private class AwesomePagerAdapter extends PagerAdapter {
public int getCount() {
return NUM_AWESOME_VIEWS;
}
public Object instantiateItem(View collection, int position) {
View layout = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
WebView web = null;
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
switch (position) {
case 0:
layout = mInflater.inflate(R.layout.activity_main, null);
web = (WebView) layout.findViewById(R.id.webView1);
web.loadUrl("http://developer.android.com");
break;
case 1:
layout = mInflater.inflate(R.layout.activity_community, null);
web = (WebView) layout.findViewById(R.id.webView2);
web.loadUrl("http://www.xda-developers.com");
break;
case 2:
layout = mInflater.inflate(R.layout.activity_support, null);
web = (WebView) layout.findViewById(R.id.webView3);
web.loadUrl("http://www.samsung.com");
break;
}
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(position == 0 ? false : true);
websettings.setUseWideViewPort(position == 0 ? true : false);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
web.requestFocusFromTouch();
websettings.setSavePassword(true);
web.setWebViewClient(new WebViewClient(){
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
progressBar.setVisibility(View.VISIBLE);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
web.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress) {
//MainActivityViewPager.this.setProgress(progress * 100);
progressBar.setProgress(progress * 100);
}
});
((ViewPager) collection).addView((View) layout);
return layout;
}
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
}
}