Add 'swipe to refresh' to WebView

0

I'm relatively new to programming for Android and would like to know how to add the Swipe to refresh [pull up to update] functionality in my app. I want it to pull up, WebView is updated.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="br.com.gamersboard.agoravai.MainActivity">

                <WebView
                    android:id="@+id/webView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    tools:layout_editor_absoluteX="8dp"
                    tools:layout_editor_absoluteY="8dp" >
                </WebView>
</android.support.constraint.ConstraintLayout>

-

package br.com.gamersboard.agoravai;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    SwipeRefreshLayout swipe;

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

        getSupportActionBar().hide();


        WebView wv = (WebView) findViewById(R.id.webView1);

        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);


        wv.loadUrl("https://meusite.com");


  }




}
    
asked by anonymous 09.09.2017 / 00:30

1 answer

1

You can use SwipeRefreshLayout and NestedScrollView in your XML that is contained in android.support.v4 . Here's how your XML would look:

XML main :

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <WebView
                android:id="@+id/webView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </android.support.v4.widget.NestedScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>

To refresh the page, just use the setOnRefreshListener method with the reload() method in relation to Webview . See:

Main class:

private WebView wb;
private SwipeRefreshLayout mySwipeRefreshLayout;

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

    mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
    wb = (WebView) findViewById(R.id.webView1);

    WebSettings ws = wb.getSettings();
    ws.setJavaScriptEnabled(true);
    ws.setSupportZoom(false);
    wb.loadUrl("http://www.globo.com/");
    wb.setWebViewClient(new MyWebViewClient());

    mySwipeRefreshLayout.setOnRefreshListener(
        new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            wb.reload();
            }
        }
    );
}
private class MyWebViewClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
        mySwipeRefreshLayout.setRefreshing(false);
        super.onPageFinished(view, url);
    }
}
    
09.09.2017 / 02:30