我有两个片段和一个活动。ChatFragment 和 UserListFragment。ChatActivity 是启动活动,我在其中编写了愚蠢的代码
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.QChatLayout);
var newFragment = new UserFragment(this, this);
var ft = FragmentManager.BeginTransaction();
ft.Add(Resource.Id.Frame_container, newFragment);
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
var homeTab = ActionBar.NewTab();
homeTab.SetTabListener(
new TabListener<UserFragment>(newFragment));
homeTab.SetText("Users");
// Add the new tab to the action bar
ActionBar.AddTab(homeTab);
ft.Commit();
}
它使用 UserFragment 显示可用用户列表当我单击其中一个用户时,chatt 窗口会在新选项卡中打开,这只是 ChatFragment 要打开新窗口,我在 ActionBar 中添加选项卡
User user = GetUserByName(userName);
ChatFragment chatFragment = null;
Fragment fragment = FragmentManager.FindFragmentByTag(user.Name);
var ft = FragmentManager.BeginTransaction();
if (fragment == null)
{
chatFragment = new ChatFragment(context, activity);
chatFragment.User = user;
chatFragment.CurrentUser = Global.Host.Username;
Global.ChatWindows.Add(chatFragment);
ft.SetBreadCrumbTitle(user.Name);
ft.Add(Resource.Id.Frame_container, chatFragment, user.Name);
var homeTab = this.activity.ActionBar.NewTab();
homeTab.SetTabListener(
new TabListener<ChatFragment>(fragment));
homeTab.SetIcon(Resource.Drawable.QChatOffline);
homeTab.SetText(userName);
// Add the new tab to the action bar
this.activity.ActionBar.AddTab(homeTab); }
else
{
chatFragment = (ChatFragment)fragment;
}
ft.Commit();
chatFragment.ShowMessage(String.Format("{0} is {1}.", user.Name, m_chatWindowManager.GetStatusText(user.Status)), MessageType.Status);
我用过 tablistner 类:
public class TabListener<T> : Java.Lang.Object, ActionBar.ITabListener
where T : Fragment, new()
{
private T _fragment;
/// <summary>
/// initializes a new instance of the tab listener
/// </summary>
public TabListener()
{
_fragment = new T();
}
/// <summary>
/// Initializes a new instance of the tab listener
/// </summary>
/// <param name="fragment"></param>
public TabListener(T fragment)
{
_fragment = fragment;
}
/// <summary>
/// Handles the reselection of the tab
/// </summary>
/// <param name="tab"></param>
/// <param name="ft"></param>
public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
}
/// <summary>
/// Adds the fragment when the tab was selected
/// </summary>
/// <param name="tab"></param>
/// <param name="ft"></param>
public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
//ft.Add(Resource.Id.Frame_container, _fragment, typeof(T).FullName);
//int index = Global.ChatWindows.FindIndex(c => c.User.Name == tab.Tag.ToString());
//Activity.ActionBar.SelectTab(Activity.ActionBar.GetTabAt(index + 1));
//ft.Show(_fragment);
//tab.Select();
//tab.Notify();
}
/// <summary>
/// Removes the fragment when the tab was deselected
/// </summary>
/// <param name="tab"></param>
/// <param name="ft"></param>
public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{
//ft.Remove(_fragment);
//ft.AddToBackStack(tab.Tag.ToString());
}
}
我应该假设在 OnTabSelected 和 Deselected 中编写什么代码。我无法在操作栏中的选项卡之间切换。任何人都可以帮助解决这个问题。