0

Sou relativamente novo na programação para Android e gostaria de saber como adicionar a funcionalidade Swipe to refresh [puxar para cima para atualizar] no meu app. Quero que ao puxar para cima, a WebView seja atualizada.

<?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 Resposta 1

1

Você pode usar o SwipeRefreshLayout e o NestedScrollView no seu XML que consta no android.support.v4. Veja abaixo como resultaria seu XML:

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>

Para atualizar a página, basta usar o método setOnRefreshListener com o método reload() em relação ao Webview. Veja:

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

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .