How to implement Swipe To Refresh in WebView?

3

I want to implement Swipe To Refresh in my WebView, is it possible? I know I have Pull To Refresh but I can not download add-ons (dependencies) at the moment. 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 refreshes, he will always return to the url specified. How do you refresh the page regardless of which url you are?

Thank you very much, thank you in advance.

    
asked by anonymous 24.02.2016 / 03:01

2 answers

1

1) You can use the reload () method:

browser.reload();

But be careful, since 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 having to know the URL, just take the current URL that is in your WebView.

browser.loadUrl(browser.getUrl().toString());
    
26.02.2016 / 04:59
0

Using the above friend idea I solved my problem using a javascript line, I made an auxiliary int variable and passed it as a parameter. looks 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.br");
        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 does not have internet and in the swipe to refresh it remaps the check using the return. I hope I have helped the next ones who can find and solve this little bug.

    
31.07.2017 / 20:02