shader2199
shader2199

Reputation: 35

webView scrollTo not working all the time

I am currently having a problem with Android webview scrollTo function. I have it working properly (sometimes), but as I am clicking on the links in webview, it sometimes resets the screen back to 0,0 position.

Heres my code.

        public void onPageFinished(WebView webview, String Url) {
            final WebView newView = webview;

            if (ScrollX > 0) { // <-- int variable containing users Scroll position
                newView.postDelayed(new Runnable() {
                    public void run() {
                        if (view.getScrollX() > 0) {
           // ScrollX and ScrollY sometimes get reset before scrollTo happens, Why?
                            newView.scrollTo(ScrollX, ScrollY);
                            pageloaded = true; 
                        } else {
                            newView.scrollTo(ScrollX, ScrollY);
                            newView.post(this);
                        }
                    }
                }, 10);
            }
        }

So here is whats going on. On my webviews onPageFinished, I am changing my webviews scroll position from the default 0,0 to whatever the user has their scroll position set to. I use the webviews postDelayed to delay things until the webview is completely loaded. Every 10ms my postDelayed checks to see if the webviews getScrollX() returns something > 0 (meaning webview has loaded). If it does, it goes into changing the webview scroll position section of the code and just finalizes that the webview has been loaded. If it doesn't, the code gets re ran after attempting to set the webviews scroll position. That way the next go around, my if statement can evaluate to true and avoid the rerun part of the code.

Now here is the problem, when I use view.getScrollX() > 0 to determine if webview has been loaded, it works fine. But sometimes, view.getScrollX() > 0 will evaluate to true. But when I use the webview scrollTo function, either my ScrollX and ScrollY variables are getting reset before scrollTo, or during scrollTo use and the webview just goes back to 0,0. Whats going on here?

Upvotes: 0

Views: 1547

Answers (1)

shader2199
shader2199

Reputation: 35

Well, I realized what is going on. The webview sometimes isn't completely loaded even when webview.getScrollX() officially starts to have a value. So with a little bit of tweaking, I came up with this...

      public void onPageFinished(WebView webview, String Url) {
        final WebView newView = webview;

        if (ScrollX > 0) { // <-- int variable containing users Scroll position
            newView.postDelayed(new Runnable() {
                public void run() {
                    if (newView.getProgress() == 100) {
                        newView.postDelayed(new Runnable() {
                            public void run() {
                            newView.scrollTo(ScrollX, ScrollY);
                            pageloaded = true; 
                            }
                        }, 10);
                    } else {
                        newView.post(this);
                    }
                }
            }, 10);
        }
    }

Now that bit of code is a beauty. Soooo, I am checking to see if webview progress has reached 100percent, if not, I loop back into the runnable. Then when my webview is loaded 100 percent of the way, I add a postDelayed because even when the webview is loaded 100percent, it still has a small chance of not being completely loaded lol. So the extra 10ms after the webview is at 100 percent should do the final trick. Since implementing this update, I have not had my scroll position reset once. And I do not recieve no where near as much of a flicker as before. Its as fast as its going to get without having the webview load up with customized default values.

Upvotes: 1

Related Questions