0

これを正しく機能させるのに問題があります。私は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);
}
}
}
4

1 に答える 1

1

推奨事項はありますか?

ProgressBarで使用する 3 つのレイアウトのそれぞれに があると思います( ViewPagerID が異なりますか?)。問題は、progressBarProgressBar変数が常に最初のレイアウトの を指し、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 に答える