facebook twitter hatena line email

Android/webview/ダイアログ式loading

提供: 初心者エンジニアの簡易メモ
2015年5月20日 (水) 03:18時点における127.0.0.1 (トーク)による版 (ページの作成:「==サンプル== public class CustomWebView { private Context mContext; private WebView mWebView; private ProgressDialog mProgress; public CustomWebView(...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

サンプル

public class CustomWebView {
   private Context mContext;
   private WebView mWebView;
   private ProgressDialog mProgress;
   public CustomWebView(Context context) {
       mContext = context;
   }
   // WebView指定
   public void setWebView(WebView webView) {
       mWebView = webView;
       mWebView.setWebViewClient(new CustomWebViewClient());
       mWebView.setWebChromeClient(new CustomWebChromeClient());
   }
   // 解放
   public void release() {
       mWebView = null;
       mContext = null;
   }
   // NowLoadingクラス
   private class CustomWebViewClient extends WebViewClient {
       public CustomWebViewClient(){
           super();
           mProgress = null;
       }
       // ページ表示開始イベント
       public void onPageStarted(WebView view, String url, Bitmap favicon) {
         // 2重起動防止
         if (mProgress != null) return;
         mProgress = new ProgressDialog(view.getContext());
           // プログレスバースタイル
           mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         mProgress.setMessage("Now Loading...");
           mProgress.show();
       }
       // ページ表示終了イベント
       public void onPageFinished(WebView view, String url){
           if (mProgress != null && mProgress.isShowing()){
             mProgress.dismiss();
           }
           mProgress = null;
       }
       public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
           if (mProgress != null) {
             mProgress.dismiss();
           }
       }
   }
   private class CustomWebChromeClient extends WebChromeClient {
   @Override
   public void onProgressChanged(WebView view, int progress) {
     if (mProgress == null) return;
     mProgress.setProgress(progress);
     // 50%以上進んだら進捗ダイアログを閉じる(早めに閉じて見た目を高速化させる
     if (progress > 50) {
       if (mProgress != null && mProgress.isShowing()){
               mProgress.dismiss();
             }
             mProgress = null;
     }
   }
   }
}