「Android/webview/ダイアログ式loading」の版間の差分
提供: 初心者エンジニアの簡易メモ
行3: | 行3: | ||
import android.content.Context; | import android.content.Context; | ||
import android.webkit.WebChromeClient; | import android.webkit.WebChromeClient; | ||
+ | import android.webkit.WebResourceRequest; | ||
import android.webkit.WebView; | import android.webkit.WebView; | ||
import android.webkit.WebViewClient; | import android.webkit.WebViewClient; | ||
+ | import android.webkit.WebResourceError; | ||
import android.graphics.Bitmap; | import android.graphics.Bitmap; | ||
public class CustomWebView { | public class CustomWebView { | ||
行47: | 行49: | ||
mProgress = null; | mProgress = null; | ||
} | } | ||
− | public void onReceivedError(WebView view, | + | public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { |
if (mProgress != null) { | if (mProgress != null) { | ||
mProgress.dismiss(); | mProgress.dismiss(); |
2018年8月23日 (木) 11:44時点における最新版
サンプル
import android.app.ProgressDialog; import android.content.Context; import android.webkit.WebChromeClient; import android.webkit.WebResourceRequest; import android.webkit.WebView; import android.webkit.WebViewClient; import android.webkit.WebResourceError; import android.graphics.Bitmap; 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, WebResourceRequest request, WebResourceError error) { 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; } } } }