2017-09-30 72 views
0

我嘗試顯示網頁的加載過程中的WebView加載進度條,並儘快在網頁加載時我想的進度條消失 我曾嘗試在網上提供很多例子但他們不爲我工作 這裏是我的代碼進度條在頁面加載過程中的WebView

import android.app.Activity; 
 
import android.graphics.Bitmap; 
 
import android.support.v7.app.AppCompatActivity; 
 
import android.os.Bundle; 
 
import android.view.View; 
 
import android.webkit.WebView; 
 
import android.webkit.WebViewClient; 
 

 
public class MainActivity extends Activity { 
 

 
    private WebView webView; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 

 
     super.onCreate(savedInstanceState); 
 

 
     setContentView(R.layout.activity_main); 
 

 

 

 

 
     webView = (WebView) findViewById(R.id.webView); 
 
     webView.setWebViewClient(new myWebClient()); 
 
     webView.getSettings().setJavaScriptEnabled(true); 
 
     webView.loadUrl("http://www.notherstore.in"); 
 

 
    } 
 

 
    public class myWebClient extends WebViewClient { 
 
     @Override 
 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
 
      super.onPageStarted(view, url, favicon); 
 
     } 
 

 
     @Override 
 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
 

 
      view.loadUrl(url); 
 
      return true; 
 

 
     } 
 
    } 
 

 
    @Override 
 
    // This method is used to detect back button 
 
    public void onBackPressed() { 
 
     if (webView.canGoBack()) { 
 
      webView.goBack(); 
 
     } else { 
 
      // Let the system handle the back button 
 
      super.onBackPressed(); 
 
     } 
 
    } 
 
}

這裏是我的XML

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:id="@+id/activity_main" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 

 
    tools:context="in.notherstore.notherstore.MainActivity"> 
 

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

 
    <ProgressBar 
 

 
     android:id="@+id/progressBar1" 
 
     style="?android:attr/progressBarStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="match_parent" 
 
     android:indeterminate="false" 
 
     android:layout_gravity="center" /> 
 
</RelativeLayout>

任何幫助,將不勝感激

+0

是你想找的'onPageFinished'? –

回答

0

你可以試試onPageFinished方法。該方法運行時,你的頁面加載完全。

public void onPageFinished(WebView view, String url) { 
      try{ 
       if (progressDialog.isShowing()) { 
        progressDialog.dismiss(); 
        progressDialog = null; 
       } 

      }catch(Exception exception){ 
       exception.printStackTrace(); 
      } 
     } 
0
webView.setWebViewClient(new WebViewClient() { 

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

      @Override 
      public void onPageFinished(WebView view, String url) { 
       loadingBar.setVisibility(View.GONE); 
      } 
     });