Add 'Swipe to refresh' to Webview

Asked

Viewed 775 times

0

I’m relatively new to programming for Android and would like to know how to add functionality Swipe to refresh [pull up to update] in my app. I want you to pull up, the WebView be 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");


  }




}

1 answer

1

You can use the SwipeRefreshLayout and the NestedScrollView in its XML as set out in android.support.v4. See below how your XML would result:

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 method setOnRefreshListener with the method reload() in relation to the Webview. Behold:

Class Main:

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);
    }
}

Browser other questions tagged

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