0

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.

1
  • You could use a SwipeRefreshLayout wrap a WebView.
    – Leo Zhu
    Aug 19, 2020 at 3:54

1 Answer 1

0

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>
1
  • thanks for such a complete, comprehensive work example. This is exactly what I wanted. Very much appreciated!! Aug 19, 2020 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.