0

我在让它正常工作时遇到了一些麻烦。我有一个带有 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);
}
}
}
4

1 回答 1

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);
    }
}
于 2013-03-22T17:58:01.413 回答