2013-07-02 68 views
5

我知道有關於這個問題的SO上發佈了各種問題,但他們都沒有爲我提供解決方案。我在fragment activity中有4個選項卡,每個選項卡都有自己的fragment。我將進入另一個fragment裏面。當我切換到標籤有時會變得重疊,都是可見的。片段在另一個片段上重疊

下面是代碼(XML):

<Button 
    android:id="@+id/checkBal" 
    android:layout_width="fill_parent" 
    android:layout_height="45dp" 
    android:clickable="false" 
    android:gravity="left|center_vertical" 
    android:typeface="serif" /> 

<View 
    android:layout_width="fill_parent" 
    android:layout_height="2dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_marginTop="2dp" 
    android:background="@color/green" /> 

<FrameLayout 
    android:id="@+id/realtabcontent" 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" /> 

<android.support.v4.app.FragmentTabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0" /> 
</android.support.v4.app.FragmentTabHost> 

public class Tab_Activity extends FragmentActivity { 

FragmentTabHost mHost; 
Button balance; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabs); 

    balance = (Button) findViewById(R.id.checkBal); 
    mHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
    mHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 

    mHost.addTab(mHost.newTabSpec("Home").setIndicator("Home"), 
      Home_Fragment.class, null); 
    mHost.addTab(mHost.newTabSpec("Number").setIndicator("Send to number"), 
      Number_Fragment.class, null); 
    mHost.addTab(
      mHost.newTabSpec("Contact").setIndicator("Send to contacts"), 
      Contact_Fragment.class, null); 
    mHost.addTab(mHost.newTabSpec("Group").setIndicator("Send to groups"), 
      Group_Fragment.class, null); 

    mHost.setOnTabChangedListener(new OnTabChangeListener() { 

     @Override 
     public void onTabChanged(String tabId) { 
      // TODO Auto-generated method stub 

      if (tabId.equalsIgnoreCase("Home")) { 

       Home_Fragment home = new Home_Fragment(); 

       FragmentManager manager = getSupportFragmentManager(); 
       FragmentTransaction ft = manager.beginTransaction(); 
       ft.replace(R.id.realtabcontent, home, "Home"); 
       ft.commit(); 

      } 

      if (tabId.equalsIgnoreCase("Number")) { 

       Number_Fragment number = new Number_Fragment(); 

       FragmentManager manager = getSupportFragmentManager(); 
       FragmentTransaction ft = manager.beginTransaction(); 
       ft.replace(R.id.realtabcontent, number, "Number"); 
       ft.commit(); 

      } 
      if (tabId.equalsIgnoreCase("Contact")) { 

       Contact_Fragment contact = new Contact_Fragment(); 

       FragmentManager manager = getSupportFragmentManager(); 
       FragmentTransaction ft = manager.beginTransaction(); 
       ft.replace(R.id.realtabcontent, contact, "Contact"); 
       ft.commit(); 
      } 

      if (tabId.equalsIgnoreCase("Group")) { 

       Group_Fragment group = new Group_Fragment(); 

       FragmentManager manager = getSupportFragmentManager(); 
       FragmentTransaction ft = manager.beginTransaction(); 

       ft.replace(R.id.realtabcontent, group, "Group"); 
       ft.commit(); 
      } 

     } 
    }); 

} 




public void newFragment(Fragment fragment, String tag) { 

    FragmentManager manager = getSupportFragmentManager(); 
    FragmentTransaction ft = manager.beginTransaction(); 
    // 
    if (tag.equalsIgnoreCase("Group")) 
     ft.remove(new Group_Fragment()); 
    else 
     ft.remove(new Contact_Fragment()); 
    ft.replace(R.id.realtabcontent, fragment); 
    ft.addToBackStack(null); 
    ft.commit(); 

} 

public void updateContacts(ArrayList<String> cList) { 

    FragmentManager manager = getSupportFragmentManager(); 

    Contact_Fragment contactFrag = (Contact_Fragment) manager 
      .findFragmentByTag("Contact"); 
    contactFrag.updateData(cList); 

} 

} 

內新片段

mActivity.newFragment(new Select_Group_Fragment(), "Group"); 

由於我C調用片段從另一個片段阿靈片段,我需要在new fragment方法Tab_Activity

Overlapped screenshot

請幫傢伙

+0

的圖像可能是有用的,以顯示該重疊 – dymmeh

+0

overlapp可由佈局引起的。如果使用'RelativeLayout'而不指定視圖將出現的順序或'FrameLayout',這種不可預知的行爲可能發生 –

+0

@dotroph截圖附帶 – moDev

回答

0

從一切,我已經看到了片段,即谷歌IO,StackOverflow上,等加addToBackStack這不是處理它的方法。該活動應控制哪些片段處於活動狀態。片段應該遵循片段控制的活動。您可能必須將片段之間的數據傳遞給活動。谷歌開發人員強烈阻止片段與片段之間的溝通。碎片意味着成爲屏幕的一個子集。

您的疊加看起來像有兩個片段同時處於活動狀態。

僅供參考。最好使用Fragment Transactions來操作一個活動中的多個片段。啓動一個從xml啓動片段的活動並不能真正被Fragment Transactions處理。

希望這會有所幫助。

0

在AndroidMainifest.xml變化:

android:name=".activities.youactivityname" 
android:configChanges="screenSize|orientation" 
0
layout.removeAllViews(); 

使用這種可移除之前的視圖