When to call ScrollTo on a WebView

2019-09-13 12:02发布

问题:

I want to open a website in a WebView and then have it start a certain pixel distance down the page.

I see the WebView has a scrollTo() method, but when is the soonest I can call this and how do I detect that?

I would like to have it appear to the user, that the page opens up at the scroll position. So I don't want to wait until the entire page is loaded. If it did wait, then the user may have already started scrolling and once it loaded, the scrollTo() would appear to grab control from them.

Thanks

回答1:

Hopefully there is another way to do this, but for now the method I am using is to start a checking loop when the load is fully loaded. It checks to see if the WebView has content height yet and when it does, then it scrolls. It works as I wanted, just seems a little hackish.

EDIT: I posted a very similar question and got this answer, which works perfectly.



回答2:

The soonest you can call scrollTo() on a View seems to be the onLayout() method.

So, for example do this:

 protected void onLayout(boolean changed, int l, int t, int r, int b) {
 // set initial scroll to
 scrollTo(yourXpos,yourYpos);
 super.onLayout(changed, l, t, r, b);
 }