2014-01-22 57 views
3

我正在一個簡單的應用程序中,我已經把webview,但頁面加載非常緩慢(嘗試與許多網站),我也想添加一個進度條,所以它應該顯示該頁面正在加載。 這裏是我使用的WebView的代碼,但我不知道如何讓進度條就可以了:如何使WebView加載速度快並添加一個進度條到它

回答

3

有一個進度條。顯示相同。您可以自定義以下內容以滿足您的需求。加載速度取決於連接速度。

覆蓋onPageFinished並設置能見度在了webcount

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 

    WebView web; 
    ProgressBar progressBar; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     web = (WebView) findViewById(R.id.wv); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     web.setWebViewClient(new myWebClient()); 
     web.getSettings().setJavaScriptEnabled(true); 
     web.loadUrl("http://slashdot.org/"); 
    } 

    public class myWebClient extends WebViewClient 
    { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // TODO Auto-generated method stub 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // TODO Auto-generated method stub 

      view.loadUrl(url); 
      return true; 

     } 
     @Override 
     public void onReceivedError(WebView view, int errorCode, 
       String description, String failingUrl) { 
      // TODO Auto-generated method stub 
      Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      //super.onReceivedError(view, errorCode, description, failingUrl); 
     } 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 

      progressBar.setVisibility(View.GONE); 
     } 
    } 

    // To handle "Back" key press event for WebView to go back to previous screen. 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { 
      web.goBack(); 
      return true; 
     } 
     else 
     { 
      finish(); 
      return true; 
     } 
    } 
} 

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <WebView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/button1" 
     android:id="@+id/wv"/> 
    <ProgressBar 
     android:id="@+id/progressBar" 
     android:layout_width="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_height="wrap_content" 
     /> 


</RelativeLayout> 

捕捉

無需滾動您沒有看到整個頁面

enter image description here

隨着WebChromeClient()滾動右

enter image description here

+0

好的,謝謝你的工作。如果網站沒有響應,它是給一個固定的顯示器不滾動,如何添加滾動? – Karma5

+0

@ Karma5正如我所說,它取決於互聯網連接的速度。它仍在加載並需要時間來渲染。嘗試不同的網址只是爲了測試 – Raghunandan

+0

我在問關於沒有響應的網站,而不是網站的速度。它給出了一個固定的顯示,我無法左右移動頁面 – Karma5

1
public class MainActivity extends Activity { 

WebView wv; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    wv = (WebView) findViewById(R.id.webView1); 
    wv.loadUrl(" http://www.abc.com"); 
       //Show Progress layout 
       showProgressLayout(); 

     wv.setWebViewClient(new webCont()); 

} 
class webCont extends WebViewClient 
{ 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // TODO Auto-generated method stub 
     view.loadUrl(url); 

     return true; 

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

       //Hide Progress bar. 

       hideProgressLayout(); 
      } 

      @Override 
      public void onReceivedError(WebView view, int errorCode, 
        String description, String failingUrl) { 
       //If any error comes this method will handle for webview. 

       hideProgressLayout(); 

       if (view.canGoBack()) { 
        view.goBack(); 
       } 

       You can write your own Dialog or Toast message if you want to show error message 
      } 
} 
+0

將代碼提高速度? – Karma5

+0

它會改善 –

+0

@ Karma5的速度仍然取決於您的互聯網連接的速度,無論它的WiFi或移動數據。它也取決於你正在加載的網站。一個有很多圖片的網站會比只有文本數據的網站加載速度慢 – Raghunandan

3

設置的WebView和嘗試這個代碼

ProgressDialog progressDialog = new ProgressDialog(this); 
    progressDialog.setMessage("Loading Data..."); 

    webview.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 

      if (progress < 100) { 
       progressDialog.show(); 
      } 
      if (progress == 100) { 
       progressDialog.dismiss(); 
      } 
     } 
    });