开发者

When to call ScrollTo on a WebView

开发者 https://www.devze.com 2023-01-17 17:55 出处:网络
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 thi

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


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.


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);
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消