PageDown (true) always returns false in WebView Android

I have a WebView on Android that stores a conversation thread for my application.

Every 20 seconds, the application checks the server for any messages and updates the WebView.

Every time the WebView refreshes it, it scrolls back to the top of the view.

I wanted to stop this, so I tried using webView.pageDown (true); but this always seems to return false.

Any help would be really appreciated. Here is the code:

    webView.loadDataWithBaseURL("fake://not/needed", html, "text/html", "utf-8", "");
    boolean scrolled = webView.pageDown(true);
    System.out.println("Scrolled is: " + scrolled);

      

+3


source to share


3 answers


I had the same problem, so I researched it further and found that they are released here:

The only solution I am working with is to create a handler that will delay the call to webView.pageDown (true) for 100ms and then it will work. It may take less or more time, probably depends on the time the web view component needs to render the given html.

Here is the code to load and scroll:



webView.loadDataWithBaseURL("fake://not/needed", html, "text/html", "utf-8", "");
mWebViewScrollHandler.removeCallbacks(mScrollWebViewTask);
mWebViewScrollHandler.postDelayed(mScrollWebViewTask, 100);

      

Here's the code for the handler:

private final Handler mWebViewScrollHandler = new Handler();    

private final Runnable mScrollWebViewTask = new Runnable() {
    public void run() {
        webView.pageDown(true);
    }
};

      

+2


source


loadDataWithBaseURL

is asynchronous, so it pageDown

will run before the page has finished loading. This is why @ZoltanF said to wait a while before executing the code

You need to add a listener to the page load and then scroll:



webView.setWebViewClient(new WebViewClient() {
    @Override  
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        webView.pageDown(true);
    }  
});

      

Hope it helps

0


source


There are two approaches for this problem:

Instead of reloading, WebView

change the webpage on the server so that it refreshes with ajax. This will give you more control over behavior.

In another approach, use event listeners as Sean said:

webView.setWebViewClient(new WebViewClient() {
    @Override  
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        webView.pageDown(true);
    }  
});

      

0


source







All Articles