2012-08-10 72 views
1

誰能告訴我下面的問題的解決方案。 我已經創建了簡單的聊天應用程序,我想用碎片打開多個聊天窗口。 我已經完成了這個,但現在的問題是我不能分開聊天片段,所有都互相重疊。 請建議我的解決方案,以便我可以使這些片段與最小化和最大化按鈕分開。 如果你提供的代碼片段,它會對我有幫助,因爲我是新的adroid開發。片段重疊,需要解決方案來最大化和最小化片段

謝謝& 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(); 
    } 
} 

私人無效爲startChat(用戶名字符串,布爾selectedFromList) { 嘗試 { 用戶的用戶= GetUserByName(用戶名); 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 = 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("%@[email protected]%", "\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

在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> 
+0

非常感謝Ramesh的回覆。我只有一個可以管理消息發送和接收的活動。和兩個片段類一個用於顯示用戶列表和另一個用於聊天。從用戶列表片段中,我打開了許多聊天片斷。是否有可能提供用於在新框架中打開片段運行時的代碼段並在它們之間進行交換。謝謝 – 2012-08-10 11:03:31

+0

我已經使用操作欄選項卡和片段完成了此操作,但現在的挑戰是如何在不丟失之前狀態的情況下在選項卡之間切換。我曾經使用過tablistner,但不能保存片段的prevoius狀態。 – 2012-08-15 07:27:37