3

Goal - I want to scrollTo(0,60) once a webview loads a webpage. At a minimum I want to prevent the flicker that I see when the webpage finishes loading. Flicker - As a webview loads a url (for example wiki) a user can scroll through it to read text. However the webview automatically scrolls to the top when it finishes loading. This does not happen on the standard browser. I am targeting API 8. (Old but people are still using that version).

Here's some code:

private class WebBrowserWebViewClient extends WebViewClient 
{   
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    {
        Log.d(TAG, "Load URL");
        view.loadUrl(url);                                 
        return true;                
    }

    @Override
    public void onPageFinished(WebView view, String url) 
    {
        super.onPageFinished(view, url);

        Log.d(TAG, "Page Finished Loading");

        //Attempt scrollTo here does not work.
        view.scrollTo(0, 60);
    }            
}

I also created a picture listener.

mWebView.setPictureListener(new PictureListener()
{               
        @Override
        public void onNewPicture(WebView view, Picture picture) 
        {
            Log.d(TAG, "On New Picture called....Y is " + view.getScrollY());

                //Does not work because it is called multiple times         
                view.scrollTo(0, 60);
            }       
});

I also keep on Progress

mWebView.setWebChromeClient(new WebChromeClient()
{
       @Override
       public void onProgressChanged(WebView view, int newProgress) 
       {
            super.onProgressChanged(view, newProgress);

            Log.d(TAG,"On PRogress Changed. New Prgoress = " + newProgress);
       }
});

So what I've noticed with the Log debug was that onPageFinished() is called before the webview is finished loading the URL. The onNewPicture() is called multiple times depending on the number of pictures in the URL and is called after the onPageFinsiehd() is called. The onProgressChanged() newProgress always gets to 100 then stops. After the onProgressChanged() reaches 100 and onNewPicture fires, the webview scrolls back to the top.

Question - How to prevent the webview from scrolling back to the top?

Note - I have seen other posts regarding similar issues but none of them resolved the issue.

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
LEO
  • 2,572
  • 1
  • 26
  • 31

0 Answers0