Using WebViewClient and/or the WebChromeClient you can get a listener for when the page has loaded, however this is sometimes called before the WebView has any content in it, before it has displayed anything.
What would be a efficient method for determining when the WebView has displayed it's content?
Edit: (Trying to be more clear)
When I load a page in a WebView, I want to set the scrol开发者_Python百科l to a specific position. It seems that the scroll position cannot be set until the page is loaded and it has an actual content height. So, I have tried two different approaches to determining when the page has finished loading, onPageFinished() from the WebViewClient and also onProgressChanged() from the WebChromeClient. Both of these tell me when the page has finished loading.
However, the problem is that sometimes it is called before the page has been displayed and therefore the page has no height and the scroll call does nothing.
I am trying to find a solid way to determine when the page is ready to be scrolled, ie. when it has its content height.
I imagine I could setup a checking loop after it finished loading to keep looking for when the height is available but that seemed like quite the hack. Hoping there is a cleaner way.
I successfully used Richard's answer with a PictureListener for a few years, but I no longer recommend this as the best solution.
This is for two reasons:
- webView.setPictureListener and PictureListener are both deprecated.
- Using this listener will cause the WebView to allocate a new Picture often. This allocation is expensive and this can have some significant performance impacts or even cause native crashes on JellyBean MR1.
Instead I recommend creating a subclass of WebView and overriding invalidate() like so:
@Override
public void invalidate() {
super.invalidate();
if (getContentHeight() > 0) {
// WebView has displayed some content and is scrollable.
}
}
If you still want to use the PictureListener method, you will get better performance if you setPictureListener back to null after you are done with it.
EDIT: Since I first posted this, this method has been deprecated in Android API 12. Thanks, Google! I will leave the original answer in tact:
Yes-- there IS a listener for knowing when the content has finished loading. I had to create one on my project so I could do a splash screen which stayed up until the webview had loaded the page.
It's called .setPictureListener.
For example:
mWebView.setPictureListener(new MyPictureListener());
//... and then later on....
class MyPictureListener implements PictureListener {
@Override
public void onNewPicture(WebView view, Picture arg1) {
// put code here that needs to run when the page has finished loading and
// a new "picture" is on the webview.
}
}
The suggested solutions here are hacky at best, while there is a perfectly clean solution, in accordance with the Android docs:
WebViewClient client = new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
Log.d("MYAPP", "Page loaded");
}
};
webView.setWebViewClient(client);
I read this discussion, and I'm facing the same problem too.
I searched a lot, but they're not the solution they promise to be.
And I don't want to use the deprecated `onNewPicture` callback.
In my own situation I only need to restore the `WebView` scroll position when the activity is started, and I think this is the most cases since when the activity task stack goes to background, Android automatically save the `WebView` state, so when it pops to the foreground, it will looks the same. If it gets killed when under background, I'm sure you'll manage to save the state in Activity lifecycle methods like `onPause`.
So, instead of extending `WebView` and overriding blah, blah, blah..., I use a Handler and post model.
private final Handler mHandler = new Handler();
// in onCreate
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mWebView.getContentHeight() > 0) {
mWebView.scrollTo(0, mLastPosition);
Log.d("scrolling", "true");
mHandler.removeCallbacks(this);
} else {
mHandler.postDelayed(this, 100);
}
}
}, 100);
This code block will peridically check if the WebView page can be manipulated, if so, it does the scrolling and remove the callback, otherwise it loops. I test this and find out it ususally won't take very long.
Hope this helps!
I created a custom view extends webview, which overrides the "onSizeChanged" method. The onSizeChanged happens after the webview loads everything in it.
EDIT: I no longer recommend this solution, but I'll leave it here for the sake of information.
In case this helps anyone else, the way I ended up doing this was by subclassing the WebView and overriding the computeVeritcalScrollExtent()
method.
@Override
protected int computeVerticalScrollExtent() {
// do you checking here
return super.computeVerticalScrollExtent();
}
This will be called anytime the WebView calculates its vertical scrollbar. However, the first time it is called where the getContentHeight() > 0
, then that will be the first time the content is displayed.
This is not a direct answer for the question asked, but you can alternatively use the WebChromeClient. I find it to me more reliable and I have it configured to display loading progress. I was having the same issue with WebView hiding the ProgressBar before the page was fully loaded and I replaced WebView with WebView client.
I use the following code
final WebView wvMax = (WebView) findViewById(R.id.webViewTnSmax);
final ProgressBar pbMax = (ProgressBar) findViewById(R.id.progressBarTnSmax);
wvMax.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
pbMax.setVisibility(View.VISIBLE);
pbMax.setProgress(progress);
if (progress == 100) {
pbMax.setVisibility(View.GONE); // Make the bar disappear after URL is loaded
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// do something
}
});
I Know its Old but it can HELP, the answers here are not perfectly working, the best that i found is to Override the onDraw on the Extended WebView
@Override
protected void onDraw(Canvas canvas) {
//do your math/stuff/magic/blackmagic/hackish here
super.onDraw(canvas);
}
so every scroll down/up every fontZoomChange
almost everything(didnt tested all) will call the onDraw
i use this to work with a SeekBar as Vertical ScrollBar
;)
its important to check if null, and add some others if to avoid useless method call
I've fixed this issue creating a Custom View which overrides onDraw method and calling my own listener. This is the code:
public class CustomWebView extends WebView{
public interface FinishedDrawListener{
void onDrawFinished(WebView view, String url);
}
private FinishedDrawListener drawListener;
public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomWebView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(drawListener != null){
drawListener.onDrawFinished(this, this.getUrl());
}
}
public void setOnFinishedDrawListener(FinishedDrawListener listener){
this.drawListener = listener;
}
I think that it's not the best way to do it but it works for my application.
onLoadResource(WebView view, String url)
Notify the host application that the WebView will load the resource specified by the given url.
Still not clear of what the objective is here, but you might try
public void doUpdateVisitedHistory (WebView view, String url, boolean isReload)
I would "assume" that the page has completed loading before it is added to history...
Its working fine on all devices :- Load Web View ProgressDialog
精彩评论