android - 刷新时在 webview 中重新加载相同的 URL

标签 android webview reload pull-to-refresh

我创建了一个小的 Android 应用程序来查看响应式网站。我已经实现了拉动刷新并且它有效,但是当拉动刷新时它重定向到我放在 LoadUrl 中的主页。如何通过重新加载用户正在访问的相同 url 来刷新?

我的java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            LoadWeb();
        }
    });
    LoadWeb();
}
public void LoadWeb(){

    webView = (WebView) findViewById(R.id.webView);
    webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setAllowFileAccess(true);
    webView.setWebViewClient(new Client());
    webView.setWebChromeClient(new ChromeClient());

    if (Build.VERSION.SDK_INT >= 19) {
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }
    else if(Build.VERSION.SDK_INT >=15 && Build.VERSION.SDK_INT < 19) {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    swipe.setRefreshing(true);
    webView.loadUrl("http://www.example.com"); 
    webView.setWebViewClient(new WebViewClient(){

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("file:///android_asset/error.html");
        }

        public  void  onPageFinished(WebView view, String url){
            //Hide the SwipeReefreshLayout
            swipe.setRefreshing(false);
        }

    });
}

主要 Activity :

<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="com.example.www.appexample.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp" />

</android.support.v4.widget.SwipeRefreshLayout>

提前致谢

最佳答案

每次用户导航时保存重定向 url 的一种方法。

 private String currentUrl="http://www.example.com";


    webview.setWebViewClient(new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            currentUrl=url;
            return super.shouldOverrideUrlLoading(view, url);
        }
    });

使您的 LoadWeb 方法参数化以接受字符串。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LoadWeb(currentUrl);
            }
        });
        LoadWeb("http://www.example.com");
    }

关于android - 刷新时在 webview 中重新加载相同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48973315/

相关文章:

java - 在 Android 中更改 Activity 时如何保持 ArrayList 值

ios - 如何从 WKWebView 获取服务器响应

javascript - 简单的 javascript 重新加载不起作用

grails - 如何启用Grails 3.x自动重新加载?

android - Eclipse 设备选择器

java - 打印 Unicode 字符 Android TextView

Android 使用线性布局 maxWidth,不适用于 fill_parent

Android:使用 Webview 从 Assets 中加载大图像

javascript - 网络应用 ZXing 条码扫描仪

loops - Nginx - 在 proxy_pass 中添加变量时无限重新加载