I have previously written an article about how to capture the webview event of Phoengap. It is mainly used to judge the webview loading event in onMessage Based on the message name of the first parameter in the Activity that implements CordovaInterface. However, in Fragment, if an Activity loads multiple fragment containing WebView, it cannot determine the WebView loading event in onMessage.
After trying, we found that we can still use the setWebViewClient method to rewrite the corresponding onPageStart and onPageFinish methods of WebViewClient to handle loading events.
[Java]
CordovaWebView webView = (CordovaWebView) findViewById (R. id. webView );
CordovaWebView webView = (CordovaWebView) findViewById (R. id. webView); [java] view plaincopyprint? CordovaInterface cordovaInterface = (CordovaInterface) context;
CordovaWebViewClient cordovaWebViewClient = new CordovaWebViewClient (cordovaInterface, webView ){
@ Override
Public void onPageStarted (WebView view, String url, Bitmap favicon ){
LogUtil. debug ("onPageStarted" + url );
Super. onPageStarted (view, url, favicon );
ProgressBar. setVisibility (View. VISIBLE );
}
@ Override
Public void onPageFinished (WebView view, String url ){
LogUtil. debug ("onPageFinished" + url );
Super. onPageFinished (view, url );
ProgressBar. setVisibility (View. GONE );
}
@ Override
Public void onReceivedError (WebView view, int errorCode, String description, String failingUrl ){
LogUtil. debug ("onReceivedError" + url );
Super. onReceivedError (view, errorCode, description, failingUrl );
ProgressBar. setVisibility (View. GONE );
ErrorView. setVisibility (View. VISIBLE );
}
};
WebView. setWebViewClient (cordovaWebViewClient );
WebView. loadUrl (url );
CordovaInterface cordovaInterface = (CordovaInterface) context;
CordovaWebViewClient cordovaWebViewClient = new CordovaWebViewClient (cordovaInterface, webView ){
@ Override
Public void onPageStarted (WebView view, String url, Bitmap favicon ){
LogUtil. debug ("onPageStarted" + url );
Super. onPageStarted (view, url, favicon );
ProgressBar. setVisibility (View. VISIBLE );
}
@ Override
Public void onPageFinished (WebView view, String url ){
LogUtil. debug ("onPageFinished" + url );
Super. onPageFinished (view, url );
ProgressBar. setVisibility (View. GONE );
}
@ Override
Public void onReceivedError (WebView view, int errorCode, String description, String failingUrl ){
LogUtil. debug ("onReceivedError" + url );
Super. onReceivedError (view, errorCode, description, failingUrl );
ProgressBar. setVisibility (View. GONE );
ErrorView. setVisibility (View. VISIBLE );
}
};
WebView. setWebViewClient (cordovaWebViewClient );
WebView. loadUrl (url );