谁能告诉我以下问题的解决方案。我创建了简单的聊天应用程序,我想在其中使用片段打开多个聊天窗口。我已经完成了这个,但现在的问题是我无法分离那些聊天片段,所有这些片段都相互重叠。请建议我解决方案,以便我可以使用最小化和最大化按钮将这些片段分开。如果您提供代码片段,它将对我有所帮助,因为我是新来的 android 开发。
谢谢,阿南德·贾格塔普。
我已经通过使用选项卡侦听器并将聊天记录存储在静态变量中来完成。
/// <summary>
/// Listener that handles the selection of a tab in the user interface
/// </summary>
/// <typeparam name="T"></typeparam>
public class TabListener<T> : Java.Lang.Object, ActionBar.ITabListener
where T : Fragment
{
private T _fragment;
/// <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)
{
Fragment fragment = null;
if (tab.Text == "Users")
{
fragment = Global.UserFragment;
}
else
{
fragment = Global.ChatWindows.FirstOrDefault(chat => chat.User.Name == tab.Text);
tab.SetIcon(Resource.Drawable.message);
}
Global.CurrentFragment = fragment;
FragmentManager fm = Global.QActivity.FragmentManager;
fm.BeginTransaction().Add(Resource.Id.Frame_container, fragment, tab.Text).Commit();
}
/// <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)
{
Fragment fragment = null;
if (tab.Text == "Users")
{
fragment = Global.UserFragment;
}
else
{
fragment = Global.ChatWindows.FirstOrDefault(chat => chat.User.Name == tab.Text);
tab.SetIcon(Resource.Drawable.QChatOffline);
}
FragmentManager fm = Global.QActivity.FragmentManager;
fm.BeginTransaction().Remove(fragment).Commit();
}
}
private void StartChat(string userName, bool selectedFromList) { try { User user = GetUserByName(userName); ChatFragment chatFragment = Global.ChatWindows.Find(chatWindow => chatWindow.User.Name == user.Name); var ft = this.activity.FragmentManager.BeginTransaction(); if (chatFragment == null) { chatFragment = new ChatFragment(); chatFragment.User = 用户;chatFragment.CurrentUser = Global.Host.Username;
var chatTab = this.activity.ActionBar.NewTab();
chatFragment.TabListener = new TabListener<ChatFragment>(chatFragment);
chatTab.SetTabListener(chatFragment.TabListener);
chatTab.SetIcon(Resource.Drawable.message);
chatTab.SetText(userName);
// Add the new tab to the action bar
this.activity.ActionBar.AddTab(chatTab);
ft.Add(Resource.Id.Frame_container, chatFragment, user.Name);
Global.ChatWindows.Add(chatFragment);
this.activity.ActionBar.SelectTab(chatTab);
Global.CurrentFragment = chatFragment;
ft.Commit();
}
else
{
int index = Global.ChatWindows.FindIndex(chat => chat.User.Name == chatFragment.User.Name);
ActionBar.Tab tab = this.activity.ActionBar.GetTabAt(index + 1);
tab.SetIcon(Resource.Drawable.message);
if (selectedFromList)
{
ft.Remove(chatFragment);
ft.Commit();
Global.CurrentFragment = chatFragment;
try
{
var fragmentTransaction = this.activity.FragmentManager.BeginTransaction();
this.activity.ActionBar.SelectTab(tab);
fragmentTransaction.Commit();
}
catch (Exception ex)
{
}
}
else
{
ft.Remove(chatFragment);
ft.Add(Resource.Id.Frame_container, chatFragment, user.Name);
ft.Commit();
}
}
chatFragment.ChatHistory.Add(new ChatHistory()
{
FirstPartColor = Android.Graphics.Color.Red,
FirstPartMessage = DateTime.Now.ToString("HH:mm:ss"),
SecondPartMessage = String.Format(" {0} is {1}.", user.Name, m_chatWindowManager.GetStatusText(user.Status).Replace("%@RN@%", "\r\n" + "\r\n")),
SecondPartColor = Android.Graphics.Color.Red
});
Global.ChatStarted = true;
}
catch (Exception exception)
{
((QChatActivity)this.activity).ShowMessage("Failed StartChat(string userName) for user " + userName + " Exception : " + exception.Message + exception.StackTrace);
}
}