2017-06-15 85 views
-1

我正在使用WebViewClient加載網頁,我想爲我的代碼顯示進度條。我的代碼沒有逐漸加載進度。它只顯示100%的進展。如何根據網頁加載時間設置進度?使用ProgressBar顯示WebViewClient的進度

爲了我的目的,我只需在WebView上加載「https://www.google.com」。請幫忙。謝謝。代碼如下所示:

活動文件

public class MainActivity extends Activity { 

private WebView webView; 
private ProgressBar progressBar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    int vc = Color.parseColor("#feca01"); 


    progressBar = (ProgressBar) findViewById(R.id.determinateBar); 
    progressBar.getProgressDrawable().setColorFilter(
      vc, android.graphics.PorterDuff.Mode.SRC_IN); 
    webView = (WebView) findViewById(R.id.webView); 
    webView.setWebViewClient(new MyBrowser()); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("http://deelay.me/5000/http://www.google.com"); 

} 

private class MyBrowser extends WebViewClient { 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) 
    { 
     super.onPageStarted(view, url, favicon); 
     progressBar.setVisibility(View.VISIBLE); 
     progressBar.incrementProgressBy(10); 

    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 

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

}

XML文件

`

<ProgressBar 
    android:id="@+id/determinateBar" 
    style="@android:style/Widget.ProgressBar.Horizontal" 
    android:layout_width="400dp" 
    android:padding="0dp" 
    android:layout_height="2dp" /> 

<WebView 
    android:layout_height="match_parent" 
    android:id="@+id/webView" 
    android:layout_weight="0.91" 
    android:layout_width="match_parent" 
    android:padding="0dp" 
    /> 

`

+0

參見[這](https://stackoverflow.com/questions/16849347 /添加-a-progress-dialog-in-a-webview)答案。 –

+0

請參閱鏈接: https://stackoverflow.com/questions/25911185/set-progress-bar-progress-color-programatically並檢查它是否適合您的要求 –

+0

@KunalChawla,它沒有工作。 : - \ – sairajreddy

回答

1

試試這個我的朋友

WebView webView; 
ProgressBar progressBar; 

webView = (WebView) findViewById(R.id.web); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setSupportZoom(true); 
webView.getSettings().setBuiltInZoomControls(true); 
webView.loadUrl(url); 

progressBar = (ProgressBar) findViewById(R.id.progressbar); 
progressBar.setMax(100); 
progressBar.setProgress(1); 

webView.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
      progressBar.setProgress(progress); 
     } 
    }); 
     webView.setWebViewClient(new WebViewClient() { 

     @Override 
     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); 
      return true; 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 

      progressBar.setVisibility(View.GONE); 
     } 
    }); 

<WebView 
    android:id="@+id/web" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ProgressBar 
    android:layout_marginTop="-7dp" 
    android:id="@+id/progressbar" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:background="@android:color/transparent" 
    android:indeterminate="false" 
    android:max="100" 
    android:progress="1" 
    android:theme="@style/ProgressbarTheme" /> 
+0

這是完美的!非常感謝:) – sairajreddy

+0

最受歡迎@sairajreddy –

0

嘗試使用FinestWebView

使用這個你只需要通過調用加載的網頁視圖 -

new FinestWebView.Builder(this).titleDefault("The Finest Artist") 
       .show("http://thefinestartist.com"); 

enter image description here

+0

真的很有意思。現在就試試吧!謝謝:) – sairajreddy

+0

這是一個很好的選擇,它的工作。非常感謝:) – sairajreddy