ViewPager中的ProgressBar与WebView

时间:2013-02-02 20:30:14

标签: android webview progress-bar android-viewpager

我在ViewPager(v4支持)中有一个webview,我想在加载应用程序时放入一个进度条。到目前为止我所拥有的东西,但它不起作用。 基本上我把它指向我的WebVIew布局文件中的进度条但它没有加载任何东西......?

     private class MyPagerAdapter extends PagerAdapter {
        public int getCount() {
           return 3;
        }
        public Object instantiateItem(View collection, int position) {
            LayoutInflater inflater = (LayoutInflater)     collection.getContext()
               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           int resId = 0;
           switch (position) {
        case 0:
            resId = R.layout.web_view;
                        break;
        case 1:
            resId = R.layout.media;
            break;
         case 2:
            resId = R.layout.youtube;
             break;

         }
         View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
         web = (WebView) findViewById(R.id.web_view);
         final ProgressBar bar = (ProgressBar)     findViewById(R.id.loading);


         web.loadUrl(url);
         web.getSettings().setJavaScriptEnabled(true);



            web.setWebViewClient(new WebViewClient () {

            @Override
            public void onLoadResource(WebView view,      String url) {
                // TODO Auto-generated method stub
                web.loadUrl("javascript:var element =     document.getElementById('social-tabs'); element.parentElement.removeChild(element);");
            web.loadUrl("javascript:var element = document.getElementById('omc-top-banner'); element.parentElement.removeChild(element);");
            web.loadUrl("javascript:var element = document.getElementById('simplereach-   slide-element'); element.parentElement.removeChild(element);");

                            }

        @Override
        public void onPageStarted(WebView view, String  url,    
                Bitmap favicon) {
            // TODO Auto-generated method stub
            bar.setVisibility(View.VISIBLE);


                            }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            bar.setVisibility(View.GONE);


            }

           });

         return view;
       }

1 个答案:

答案 0 :(得分:2)

您需要在webview客户端中实现onProgressChanged方法以适当更新进度条:

      final Activity MyActivity = this;

      web.setWebViewClient(new WebViewClient () {

            .
            .
            .
            public void onProgressChanged(WebView view, int progress) {

                                MyActivity.setProgress(progress * 100);

                                ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar2);

//pb is equivalent to your "bar" progress bar variable

                                pb.setVisibility(View.VISIBLE);
                                pb.setProgress(progress);
                                if (progress == 100) {
                                    pb.setVisibility(View.GONE);



                                }
            }
        }