java - Android Webview "swiperefreshlayout"一直旋转,如何停止?

标签 java android swiperefreshlayout

我正在尝试使用 SwipeRefreshLayout 刷新 webview。我不知道如何停止刷新循环。我什至不知道它是否刷新网页 View 。

这是 content_main xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.logitex.webviewandrefresh.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/swipeRefresh">

    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

这是MainActivity.java

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout swipeRefreshLayout;

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

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);
        swipeRefreshLayout.setOnRefreshListener(this);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://www.facebook.com");
    }

    @Override
    public void onRefresh() {
        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://www.facebook.com");
    }
}

最佳答案

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

  swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);
  swipeRefreshLayout.setOnRefreshListener(this);

  WebView myWebView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new MyWebViewClient());
    myWebView.loadUrl("http://www.facebook.com");
  }


private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        webView.setWebChromeClient(new MyWebChromeClient());
        if(Uri.parse(url).getHost().length() == 0) {
            return false;
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
         swipeRefreshLayout.setRefreshing(false);
        super.onPageFinished(view, url);
    }
}

@Override
public void onRefresh() {
      myWebView.loadUrl("http://www.facebook.com");
      swipeRefreshLayout.setRefreshing(true);
}

关于java - Android Webview "swiperefreshlayout"一直旋转,如何停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39817383/

相关文章:

我的 for 循环中出现 java.lang.OutOfMemoryError

java - 如何将 pom.xml 添加到现有的 Eclipse 项目?

Android如何实现基于箭头的布局?

java - Android RecyclerView 在应用程序重新启动之前不会加载 firebase 项目

带有数据绑定(bind)的 AndroidX SwipeRefreshLayout

java - AWS Java SDK 传输管理器仅下载文件夹结构

android - SNS Mobile 推送通知极度困惑

java - Google API - getLastKnownLocation 的 NULL 指针

android - SwipeRefreshLayout setRefreshing() 最初不显示指示器

java - Spring Boot 中的 ACL 安全性