2013-08-23 75 views
2

我正在開發一個android應用程序。它使用webview來呈現html頁面,但在頁面加載時它保持空白。我想在操作欄中添加一個加載微調器,以便用戶知道正在加載的內容並且不爲空。在WebView加載時在操作欄上添加加載微調器?

請任何人都可以指導我如何做到這一點。

我現有的代碼 -

package com.pranavsethi.dpsrkp; 

import android.annotation.TargetApi; 
import android.app.ActionBar; 
import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.webkit.DownloadListener; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 


public class Achievements extends Activity{ 

    private WebView mWebView; 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.webvew_client_layout); 
     ActionBar actionBar = getActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 


     //For web view 
     mWebView = (WebView) findViewById(R.id.webview); 
     mWebView.getSettings().setJavaScriptEnabled(true); //enable javascript 
     mWebView.getSettings().setBuiltInZoomControls(true); //enable zoom 
     mWebView.getSettings().setDisplayZoomControls(false); 
     mWebView.getSettings().setLoadWithOverviewMode(true); 
     mWebView.getSettings().setUseWideViewPort(true); 
     mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
     mWebView.setScrollbarFadingEnabled(true); 
     mWebView.setDownloadListener(new DownloadListener() { 
      @Override 
      public void onDownloadStart(String url, String userAgent, 
        String contentDisposition, String mimetype, 
        long contentLength) { 
       // handle download, here we use brower to download, also you can try other approach. 
       Uri uri = Uri.parse(url); 
       Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(intent); 
      } 
     }); 
     mWebView.loadUrl("http://pranavsethi.tk"); // name of website to load 
     mWebView.setWebViewClient(new HelloWebViewClient()); //removing loading progressbar 


} 


    private class HelloWebViewClient extends WebViewClient{ //our web client 
     @Override 
      public void onReceivedError(WebView view, int errorCode,    //Code for checking the internet connection and return the error if fails 
        String description, String failingUrl) { 
      Intent intent = new Intent(Achievements.this, MainActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
        return; 

      } 

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

     } 
    } 

    @Override 
    public boolean onKeyDown(int KeyCode, KeyEvent event) 
    { 
     if ((KeyCode)== KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) 
     { 
      mWebView.goBack(); 
      return true; 

     } 
     return super.onKeyDown(KeyCode, event); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     return true; 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 

    { 
     switch (item.getItemId()) 
     { 
     case android.R.id.home: 
      Intent intent = new Intent(this, MainActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent); 
      overridePendingTransition (R.anim.right_slide_in, R.anim.fade_out); 
      break; 







     default: 
      return super.onOptionsItemSelected(item); 


     } 
    return true; 


    } 

    @Override 
    public void onBackPressed() 
    { 
     this.finish(); 
     overridePendingTransition (0, R.anim.right_slide_out); 
     return; 
    } 
    } 
+0

向我們展示您迄今爲止編寫的代碼。 –

+0

看到這個http://stackoverflow.com/a/9060543/1944666 – kirankk

+0

謝謝你kirankk這是有幫助的! – pranavsethi96

回答

20

SOLUTION

添加以下 -

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
setProgressBarIndeterminateVisibility(true); 

後 -

super.onCreate(savedInstanceState); 

並且這在您的WebViewClient中

private class HelloWebViewClient extends WebViewClient{ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView webview, String url){ 
      webview.loadUrl(url); 
      setProgressBarIndeterminateVisibility(true); 
          return true; 

     } 
      @Override 
       public void onPageFinished(WebView webview, String url){ 
        super.onPageFinished(webview, url); 
        setProgressBarIndeterminateVisibility(false); 
       } 
    }