带有Webview的ViewPager中的ProgressBar

时间:2013-03-18 23:34:07

标签: android progress-bar android-webview actionbarsherlock android-viewpager

我无法让它正常工作。我有一个带有3个webview的ViewPager,目前遇到的问题是只有0(我的第一个webview)实际上正在使用进度条,(当进度为100时停止等)。我的其他2个webview进度条(案例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);
}
}
}

1 个答案:

答案 0 :(得分:1)

  

任何建议?

我猜你在ProgressBar中使用的三种布局中都有一个ViewPager(具有不同的ID?)。问题是 progressBar 变量将始终指向第一个布局中的ProgressBar,因为findViewById返回具有该id的小部件的第一个匹配项(将来自第一个布局,总是可用的)。解决方案是根据当前ProgressBars

分隔WebView的行为
//...
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");
        progressBar = (ProgressBar) layout.findViewById(R.id.progressBar1);
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");
            progressBar = (ProgressBar) layout.findViewById(R.id.progressBar2);
break;
case 2:
       layout = mInflater.inflate(R.layout.activity_support, null);
           web = (WebView) layout.findViewById(R.id.webView3);
           web.loadUrl("http://www.samsung.com");
           progressBar = (ProgressBar) layout.findViewById(R.id.progressBar3);
break;
}
// ....
web.setWebViewClient(new SpecialWebView(progressBar));
web.setWebChromeClient(new SpecialWebChromeClient(progressBar));
((ViewPager) collection).addView((View) layout);

其中SpecialWebViewSpecialWebChromeClient是:

public static class SpecialWebView extends WebViewClient {

    private ProgressBar progressBar;

    public SpecialWebView(ProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {         
        progressBar.setVisibility(View.VISIBLE);
        progressBar.setProgress(0);
        return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
    }
}


 public static class SpecialWebChromeClient extends WebChromeClient {

    ProgressBar progressBar;

    public SpecialWebChromeClient(ProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    public void onProgressChanged(WebView view, int progress) {
        // MainActivityViewPager.this.setProgress(progress * 100);
        progressBar.setProgress(progress);
    }
}