2013-02-02 81 views
2

我在ViewPager(v4支持)中有一個webview,我想在應用程序加載時放置一個進度條。這是我迄今爲止的但它不起作用。 基本上我有它指向我的WebVIew佈局文件中的進度條,但它不會加載任何...?ViewPager和WebView中的ProgressBar

 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; 
     } 

回答

2

您需要實現onProgressChanged方法在你的WebView客戶端更新適當的進度條:

 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); 



           } 
      } 
     }