これを正しく機能させるのに問題があります。私は3つのWebビューを備えたViewPagerを持っていますが、現在、ケース0(私の最初のWebビュー)のみが実際にプログレスバーを正しく使用している(進行状況が100になると停止するなど)という問題が発生しています。ただし、他の2つのWebビュープログレスバー(ケース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);
}
}
}