android - 如何在项目类型“Android App (Xamarin)”中使用 RefreshView
问题描述
我在 VS2019 中创建了一个类型为“Android App (Xamarin)”的项目,其中包含以下 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>
我的代码使用管道从三星平板电脑上的内置 NFC 阅读器到加载到 WebView 中的网页的值,一切运行良好。
但是,如果页面加载停止或用户由于某种原因在网页中出现错误,则无法刷新页面。我想添加一个下拉刷新,而且从左到右的拉回也很酷,但在这篇文章中将重点介绍拉刷新。
我找到了一个 Xamarin Forms 示例,说明如何使用 RefreshView 来实现这一点,但它似乎不适用于仅限 Android 的项目。
如何在我的“Android App (Xamarin)”项目中使用 RefreshView,或者有更好的方法吗?
我的目标是 Android 9.0。
谢谢你的时间。
解决方案
一个简单的示例如下:
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;
}
}
}
}
xml:
<?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>
推荐阅读
- postgresql - 如何正确停止 postgres 统计收集器?
- mapbox-gl-js - 刷新源以更新可视化数据
- c# - C# OAuth2 SMTPclient.Authenticate() 异常
- python-3.x - 如何将 Selenium Webdriver 浏览器放入 Tkinter GUI?
- docker - Kubernetes 中 Docker 容器中的内存映射文件是否与 Linux 中的常规进程一样工作?
- amazon-web-services - Terraform 将路由附加到路由表
- c# - 连接到数据库时 SQL Express 连接错误
- cmake - 为什么不能从 cmake 脚本调用 wsl?
- c++ - CPP 或 H 中的助手
- orocommerce - 如何在我的自定义主题中包含自定义 js 文件?