As we all know, in webview applications, you can use webviewclient to listen to webview content "load" events, such as onpagefinished,onpagestarted and so on. But even when the onpagefinished is triggered, the contents of WebView are not displayed. What do we do when we want to monitor the content of WebView to show the completion of the event?
First, I found the answer online.PictureListener,然后重写onNewPicture
方法(http://stackoverflow.com/questions/4065134/is-there-a-listener-for-when-the-webview-displays-its-content)。但是官方文档写的很清楚,这个类已经废弃了(http://developer.android.com/reference/android/webkit/WebView.PictureListener.html#onNewPicture%28android.webkit.WebView,%20android.graphics.Picture%29)。
The
must then use a new method. It is also suggested that you use the invalidate
method in the rewrite WebView, and in which you determine the Span class= "pun" >getcontentheight () > 0
To make sure the content has been shown (http://stackoverflow.com/questions/4065134/is-there-a-listener-for-when-the-webview-displays-its-content). This method is similar to the rewrite OnDraw () method. You need to define a listener interface in WebView, such as Onloadfinishlistener , which overrides its onloadfinish () method when it is in use. For example:
public class Articlewebview extends webview{private Onloadfinishlistener monloadfinishlistener; public interface onloadfinishlistener{public void Onloadfinish (); } private Boolean isrendered = false; Public Articlewebview (Context context) {super (context); Init (); } public Articlewebview (context context, AttributeSet Attrs) {Super (context, attrs); Init (); } public Articlewebview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstylea TTR); Init (); } private void Init () {} @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); if (!isrendered) {DebugLog.log ("Articlewebview", "Getcontentheight ():" +getcontentheight ()); isrendered = Getcontentheight () >0; if (monloadfinishlistener!= null) {monloadfinishlistener.onloadfinish (); }}} public void SetonloadfinishlisteNER (Onloadfinishlistener onloadfinishlistener) {this.monloadfinishlistener = Onloadfinishlistener; }}
This seems to be done, but the actual test found that there is still a problem with the wrong trigger time. After troubleshooting, it is found that calling Setonloadfinishlistener () is not the right time, and you must call Setonloadfinishlistener () in the Webviewclient onpagefinished () mentioned earlier. In this way, logic is smooth: The WebView content is loaded, the listener displays the event, and then the display event is triggered.
How to monitor WebView display complete event