1

谁能告诉我以下问题的解决方案。我创建了简单的聊天应用程序,我想在其中使用片段打开多个聊天窗口。我已经完成了这个,但现在的问题是我无法分离那些聊天片段,所有这些片段都相互重叠。请建议我解决方案,以便我可以使用最小化和最大化按钮将这些片段分开。如果您提供代码片段,它将对我有所帮助,因为我是新来的 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);
        }
    }
4

1 回答 1

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>
于 2012-08-10T09:57:09.863 回答