简体   繁体   English

如何将SwipeRefreshLayout添加到WebView

[英]How to add SwipeRefreshLayout to WebView

I need to pull from the top to refresh my web view I found this on Android Developer site but i don't know how to use it 我需要从顶部拉动以刷新我的Web视图,该视图是在Android Developer网站上找到的,但我不知道如何使用它

xml Code xml代码

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Java code Java代码

mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");

        // This method performs the actual data-refresh operation.
        // The method calls setRefreshing(false) when it's finished.
        myUpdateOperation();
    }
}

); );

You gotta put your WebView inside SwipeRefreshLayout: 您必须将WebView放入SwipeRefreshLayout中:

public class MainActivity extends AppCompatActivity {

    WebView webView;
    SwipeRefreshLayout swipeRefreshLayout;
    String currentUrl = "https://news.ycombinator.com/";

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

        webView = (WebView) findViewById(R.id.webView);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);

        webView.loadUrl(currentUrl);
        webView.setWebViewClient(new MyWebViewClient());

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.loadUrl(currentUrl);
            }
        });


    }

    public class MyWebViewClient extends WebViewClient{

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


}

swipeRefreshLayout.setRefreshing(false) stops the animation. swipeRefreshLayout.setRefreshing(false)停止动画。

To be able to refresh the page with the same URL running 为了能够使用相同的URL刷新页面

  1. you gotta save your link in ISharedPreferences when page first loaded 您必须在页面首次加载时将链接保存在ISharedPreferences中

     public override void OnPageFinished(WebView view, string url) { ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); ISharedPreferencesEditor editor = prefs.Edit(); editor.PutString("MyURL", url); editor.Commit(); } 
  2. when you load the URL for reffresh use the saved URL 当您加载URL刷新时,请使用保存的URL

     string SaveURL = prefs.GetString("MyURL", ""); webView.loadUrl(SaveURL); 

    -- Other Solution is Webview.Reload(); -其他解决方案是Webview.Reload(); // This code Refreshes the current page loaded //此代码刷新当前加载的页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM