任何人都可以告诉我以下问题的解决方案。 我创建了简单的聊天应用程序,其中我想使用片段打开多个聊天窗口。 我已经完成了这个,但现在的问题是我无法分离聊天片段,所有这些都会相互重叠。 请建议我解决方案,以便我可以使用最小化和最大化按钮分离这些片段。 如果你提供代码片段对我有用,因为我是adroid developpement的新手。
谢谢& ragards, Anand Jagtap。
我已经完成了使用标签监听器并将聊天记录存储在静态变量中。
/// <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) { 尝试 { 用户user = GetUserByName(userName); ChatFragment chatFragment = Global.ChatWindows.Find(chatWindow =&gt; chatWindow.User.Name == user.Name); var ft = this.activity.FragmentManager.BeginTransaction(); if(chatFragment == null) { chatFragment = new ChatFragment(); chatFragment.User = 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);
}
}
答案 0 :(得分:0)
在Android中,不支持最小化或最大化屏幕,而是可以将警报框设置为在单独窗口中聊天的活动。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@android:color/white"
android:orientation="vertical" android:padding="20dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/welcomeToIM"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/scrollView1"
android:textColor="#000000"
android:textSize="14dp" />
</LinearLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/edittext1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="25dp"
android:background="@android:drawable/editbox_background_normal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/textMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="14dp" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btn2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@android:drawable/editbox_background_normal"
android:text="Contacts"
android:textColor="#000000" />
<Button
android:id="@+id/btn1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@android:drawable/editbox_background_normal"
android:text="Send"
android:textColor="#000000" />
<EditText
android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn2"
android:layout_alignParentLeft="true"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Type a Message"
android:maxLines="5"
android:textColor="#000000" >
<requestFocus />
</EditText>
</RelativeLayout>