How to use RefreshView in project type “Android App (Xamarin)”

Issue

I have created a project of type “Android App (Xamarin)” in VS2019 which has the following activity_main.xml:

<?xml version"1.0" encoding"utf-8"?>

<LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"
    android:orientation"vertical"
    android:layout_width"fill_parent"
    android:layout_height"fill_parent"
    >
<WebView  
    android:id"@+id/web"
    android:layout_width"fill_parent" 
    android:layout_height"fill_parent"

    />
</LinearLayout>

I have code that uses the pipes a value from the built in NFC reader on Samsung tablet to the web page loaded into the WebView, and all is working well.

However, if a page load stalls or the user gets an error in the web page for some reason, there is no way to refresh the page. I wanted to add a pull down to refresh, and also a pull from left to right to go back would be pretty cool as well, but will focus on pull to refresh in this post.

I found a Xamarin Forms example of how to use the RefreshView to achieve this, but it doesn’t seem to work in the Android only project.

How can I use the RefreshView in my “Android App (Xamarin)” project, or is there a better way to do this?

I am targeting Android 9.0.

Thanks for your time.

Solution

A simple sample like below:

public class RefreshWebView : Activity,SwipeRefreshLayout.IOnRefreshListener,SwipeRefreshLayout.IOnChildScrollUpCallback
{
    private WebView webView;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.refresh_webview);
        SwipeRefreshLayout swipeRefreshLayout  FindViewById<SwipeRefreshLayout>(Resource.Id.swipe_fresh);
        webView  FindViewById<WebView>(Resource.Id.web);
        webView.LoadUrl("https://www.google.com");
        webView.SetWebViewClient(new MyWebClient(swipeRefreshLayout));
        swipeRefreshLayout.SetOnRefreshListener(this);
        swipeRefreshLayout.SetOnChildScrollUpCallback(this);

    }
    public void OnRefresh()
    {
         webView.LoadUrl(webView.Url.ToString());
    }

    public bool CanChildScrollUp(SwipeRefreshLayout parent, View child)
    {
        return webView.ScrollY > 0;
    }

    class MyWebClient : WebViewClient
    {
        SwipeRefreshLayout swipeRefresh;
        public MyWebClient(SwipeRefreshLayout swipeRefreshLayout)
        {
            swipeRefresh  swipeRefreshLayout;
        }

        public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
        {
            view.LoadUrl(request.Url.ToString());
            return true;
        }

        public override void OnPageFinished(WebView view, string url)
        {
            base.OnPageFinished(view, url);
            if (swipeRefresh.Refreshing)
            {
                swipeRefresh.Refreshing  false;
            }
        }
    }     
}

xaml:

<?xml version"1.0" encoding"utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android"http://schemas.android.com/apk/res/android"
    android:id"@+id/swipe_fresh"
    android:layout_width"match_parent"
    android:layout_height"match_parent"
    >

  <WebView  
    android:id"@+id/web"
    android:layout_width"fill_parent" 
    android:layout_height"fill_parent"/>

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

Answered By – Leo Zhu – MSFT

Leave a Comment