java – How to implement Swipe To Refresh in WebView?

Question:

I want to implement Swipe To Refresh in my WebView, is this possible? I know I have Pull To Refresh but I'm not able to download add-ons (dependencies) right now. I researched a lot on the internet and found something like this:

browser.loadUrl("http://urlaqui");
swipeView.setColorScheme(android.R.color.holo_blue_dark,android.R.color.holo_blue_light, android.R.color.holo_green_light,android.R.color.holo_green_dark);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() 
{
@Override
public void onRefresh() 
{
        swipeView.setRefreshing(true);
             ( new Handler()).postDelayed(new Runnable() 
{
@Override
public void run() 
{
                         swipeView.setRefreshing(false);
browser.loadUrl("http://não-atualizar-somente-esta-url");
}
}, 4000);
}
});
}

In this case, when the user updates, it will always go back to the specified url. How do you update the page regardless of what url is?

Thank you very much, thank you in advance.

Answer:

1) You can use the reload() method :

browser.reload();

But be careful, as the reload only works if the page request is via GET, this method does not work with POST.

2) You can also use javascript to update the page:

browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl("javascript:window.location.reload(true)"); 

3) Finally, you can use the example you described without needing to know the URL, just get the current URL that is in your WebView.

browser.loadUrl(browser.getUrl().toString());
Scroll to Top
AllEscort