我有两个片段和一个活动。 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中编写什么代码。 我无法在操作栏中的标签之间切换。 任何人都可以帮忙解决这个问题。
答案 0 :(得分:1)
事情实际上很简单(我正在使用API级别14)并且记住它是我称为监听器类SimpleTabListener
。
public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
fragmentTransaction.Add(Resource.Id.fragment_container, fragment, typeof(T).Name);
}
public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
fragmentTransaction.Remove(fragment);
}
我一直对您的问题感兴趣,因为我认为选项卡切换不能像Android教程演示那样正常工作。实际上,我在更新选项卡视图时犯了一个错误,并且在错误的方向上搜索解决方案几个小时。 我在stackoverflow上看到了关于标签的其他问题,我认为你处于类似情况;-)。
编辑:事实上,似乎从android 3.2附加/分离是一个更好的想法切换标签,因为片段保存在FragmentManager中并删除视图。 以前的方法是:
public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
// Allows the view to be refreshed
fragmentTransaction.Detach(fragment);
fragmentTransaction.Attach(fragment);
}
public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
if (fragment == null)
{
fragment = Fragment.Instantiate(context, typeof(T).Namespace.ToLower() + "." + typeof(T).Name);
fragmentTransaction.Add(Resource.Id.fragment_container, fragment);
}
else
{
fragmentTransaction.Attach(fragment);
}
}
public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
if (fragment != null)
{
fragmentTransaction.Detach(fragment);
}
}
This blog post对这个主题非常有趣。