当用户使用不推荐使用的Actionbar和TabHost重新选择使用Xamarin在Android编码中选择的选项卡时,我正在尝试刷新WebView。
我有此代码
public void OnTabReselected(WhatsOnActivity tab, FragmentTransaction ft)
{
WebView whatsOnWebView = FindViewById<WebView>(Resource.Id.webViewWhatsOn);
//tell webview to reload
whatsOnWebView.Reload();
}
并且我试图将该代码同时放入MainActivity和WhatsOnActivity内部
它不会使应用程序崩溃,但也不会重新加载选项卡。在Xamarin中,我可以使用“ WhatsOnActivity”作为方法中的选项卡吗?我有一种感觉,那就是我做错了..但是,如果我尝试使用选项卡ID,IDE不会识别它们。谁能给我指导我做错了什么?
我的完整代码可以在这里找到:
https://github.com/hexag0d/BitChute_Mobile_Android_a2/blob/2.68/MainActivity.cs
如果您想了解上下文。
答案 0 :(得分:0)
更新
尝试在第一个标签上设置OnClicklistener
。添加标签后,设置监听器。
TabHost.TabWidget.GetChildAt(0).SetOnClickListener(new MyOnClickListener(tabHost));
听众:
public class MyOnClickListener : Java.Lang.Object, IOnClickListener
{
TabHost tabHost;
public MyOnClickListener(TabHost tabHost)
{
this.tabHost = tabHost;
}
public void OnClick(View v)
{
var parentView = ((ViewGroup)((ViewGroup)tabHost.GetChildAt(0)).GetChildAt(1)).GetChildAt(0);
WebView whatsOnWebView = parentView.FindViewById<WebView>(Resource.Id.webViewWhatsOn);
whatsOnWebView.Reload();
tabHost.CurrentTab = 0;
}
}
结果是:
原始答案
请尝试将OnTabChangeListener
添加到您的TabHost
。
例如,在您的MainActivity OnCreate()
中:
TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
tabHost.SetOnTabChangedListener(new MyOnTabChangedListener(tabHost));
还有MyOnTabChangedListener
:
public class MyOnTabChangedListener : Java.Lang.Object, IOnTabChangeListener
{
TabHost tabHost;
public MyOnTabChangedListener(TabHost tabHost)
{
this.tabHost = tabHost;
}
public void OnTabChanged(string tabId)
{
if(tabId == "whats_on")
{
var parentView = ((ViewGroup)((ViewGroup)tabHost.GetChildAt(0)).GetChildAt(1)).GetChildAt(0);
WebView whatsOnWebView = parentView.FindViewById<WebView>(Resource.Id.webViewWhatsOn);
whatsOnWebView.Reload();
}
}
}
答案 1 :(得分:0)
首先感谢@Billy Liu-MSFT !!!花花公子是救生员。他知道他的Xamarin。好的,这就是您可以刷新标签OnClick的方法
//程序集
using System;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Widget;
using Android.Webkit;
using static Android.Widget.TabHost;
using static Android.Views.View;
using Android.Views;
//this assembly is your Activities class, which should contain your class instances
using com.xamarin.example.BitChute.Activities;
编辑:将这些单击侦听器放入MainActivity.cs中。GetChildAt()中的int表示侦听器将响应的选项卡。 int 0 =最左边的制表符,它们上升0 .. 1 .. 2 ..等等.. 1意味着制表符从左数第二。每个侦听器将需要它自己的侦听器类实例。在下一个块中,我将显示两个示例。
TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
tabHost.Setup();
tabHost.TabWidget.GetChildAt(0).SetOnClickListener(new WhatsOnClickListener(tabHost));
tabHost.TabWidget.GetChildAt(1).SetOnClickListener(new SubsClickListener(tabHost));
tabHost.TabWidget.GetChildAt(2).SetOnClickListener(new DiscoverClickListener(tabHost));
tabHost.TabWidget.GetChildAt(3).SetOnClickListener(new MyChannelClickListener(tabHost));
tabHost.TabWidget.GetChildAt(4).SetOnClickListener(new SettingsClickListener(tabHost));
编辑:创建一个单独的ClickActivity.cs文件,然后将这些侦听器添加到其中。您将需要为每个标签创建一个OnClickListener实例。这个例子只是两个侦听器。但是如果您使用的是模板,则需要使用以下示例创建另外3个侦听器。
public class WhatsOnClickListener : Java.Lang.Object, IOnClickListener
{
TabHost tabHost;
//this int will tell the click listener whether to reload the webview or pop 2 root
static int tbC = 0;
public WhatsOnClickListener(TabHost tabHost)
{
//tell the clicklistener which tabhost to use
this.tabHost = tabHost;
}
//this class handles the click event
public void OnClick(View v)
{
//declare the webview and tell our object where to find the XAML resource
WebView webViewWhatsOn = tabHost.CurrentView.FindViewById<WebView>(Resource.Id.webViewWhatsOn);
//...if the CurrentTab != 0 ... we won't fire the Reload() or LoadUrl()
//..without this logic, the app will crash because our WebViews
//.aren't set to an instance of an object
if (tabHost.CurrentTab == 0)
{
//if tbC int is 0, we will reload the page
if (tbC == 0)
{
//tell whatsOnWebView to Reload
webViewWhatsOn.Reload();
//set the int to one so next time webview will pop to root
tbC = 1;
}
//else if the int is 1, we will pop to root on tab 0
else if (tbC == 1)
{
//tell whatsOnWebView to pop to root
webViewWhatsOn.LoadUrl(@"https://bitchute.com/");
//set the tbC int so that next time webview will reload
tbC = 0;
}
}
//if our current tab isn't zero, we need to set CurrentTab to 0
//this line is critical or our tabs won't work when not selected
tabHost.CurrentTab = 0;
}
}
public class SubsClickListener : Java.Lang.Object, IOnClickListener
{
TabHost tabHost1;
static int tbC = 0;
public SubsClickListener(TabHost tabHost1)
{
this.tabHost1 = tabHost1;
}
public void OnClick(View v)
{
if (tabHost1.CurrentTab == 1)
{
WebView subWebView = tabHost1.CurrentView.FindViewById<WebView>(Resource.Id.webViewSubs);
if (tbC == 0)
{
subWebView.Reload();
tbC = 1;
}
else if (tbC == 1)
{
subWebView.LoadUrl(@"https://bitchute.com/subscriptions/");
tbC = 0;
}
}
tabHost1.CurrentTab = 1;
}
}
希望有帮助!我已经尝试了这一分钟。