How to implement Swipe To Refresh on Webview?

Asked

Viewed 667 times

3

I want to implement Swipe To Refresh on my Webview, is that possible? I know you have Pull To Refresh but I’m not able to download add-ons (dependencies) at the moment. I did a lot of research 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 refreshes, they will always return to the specified url. How do you refresh the page regardless of which url it is?

Thank you very much, thank you in advance.

  • 2

    webView.Reload(); doesn’t solve?

  • @Thiagoluizdomacoski Hello friend, thank you so much for answering. Sorry to ask, I am beginner in programming, in this case replace: browser.loadUrl("site"); by webView.Reload(); ?

  • Exact ! instead of calling the url again call the Reload inside the run!

  • @Thiagoluizdomacoski Ok friend, thank you very much. I will test now ;)

  • @Thiagoluizdomacoski Hello friend, I tested here and it didn’t work. The app gave 'force close' :/ . But thanks for the help.

2 answers

1

1) You can use the method Reload():

browser.reload();

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

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

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

3) Finally, you can use the example you described without knowing the URL, just take the current URL that is in your Webview.

browser.loadUrl(browser.getUrl().toString());

0

Using the idea of the friend above I solved my problem using a javascript line, I made a auxiliary int variable and passed it as parameter. was more or less like this:

public class MainActivity extends AppCompatActivity {
    WebView wv;
    SwipeRefreshLayout sp;
    int pegaUrl;

    //webview
    public void LoadWeb(){
        //start webview
        wv = (WebView) findViewById(R.id.wv);
        //Enable JS
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setFocusable(true);
        wv.setFocusableInTouchMode(true);
        //Set Render Priority High
        wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        wv.getSettings().setDomStorageEnabled(true);
        wv.getSettings().setDatabaseEnabled(true);
        wv.getSettings().setAppCacheEnabled(true);
        wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
        //Load URL
        wv.loadUrl("https://www.google.com");
        pegaUrl = 1;
        sp.setRefreshing(true);
        wv.setWebViewClient(new WebViewClient(){
            public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
                wv.loadUrl("file:///android_asset/error.html");
                pegaUrl = 2;
            }

            public void onPageFinished(WebView view, String url){
                //hide the swipe refresh layout
                sp.setRefreshing(false);
            }
        });
    }

    @Override
    public void onBackPressed(){
        if (wv.canGoBack()){
            wv.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sp = (SwipeRefreshLayout) findViewById(R.id.sp);
        sp.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {

                if (pegaUrl == 2) {
                    LoadWeb();
                    return;
                } else {
                    wv.loadUrl("javascript:window.location.reload(true)");
                    return;
                }
            }
        });
        LoadWeb();
    }
}

The code passes and tries to load the page, if it is not connected it sends me to an error page saying that the user has no internet and in Swipe to refresh it repeats the check using Return. I hope I’ve helped the next ones who might find and fix this little bug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.