2013-07-02 6 views
5

So che ci sono varie domande pubblicate su SO relative alla domanda ma nessuna di esse fornisce una soluzione per me. Ho 4 schede in fragment activity e ciascuna scheda ha il proprio fragment. Sto entrando in un altro fragment al suo interno. Quando passo alle schede a volte si sovrappone ed entrambi sono visibili.Frammento che si sovrappone su un altro frammento

Ecco il codice (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); 

} 

} 

Calling frammento all'interno nuovo frammento

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

As I Am c Alling frammento da un altro frammento ho bisogno di aggiungere addToBackStack in new fragment metodo Tab_Activity

Overlapped screenshot

Si prega di aiutare i ragazzi

+0

L'immagine potrebbe essere utile per mostrare la sovrapposizione overlapp – dymmeh

+0

può essere causato dal layout. se usi 'RelativeLayout' senza specificare l'ordine in cui appaiono le viste o' FrameLayout', questo comportamento imprevedibile può verificarsi –

+0

@dymmeh Screenshot allegato – moDev

risposta

0

Da tutto quello che ho visto su frammenti, vale a dire Google IO, StackOverflow, ecc ., questo non è il modo di gestirlo. L'attività dovrebbe controllare quali frammenti sono attivi. Il frammento dovrebbe rimandare all'attività per il controllo dei frammenti. Potrebbe essere necessario passare i dati tra i frammenti all'attività. Gli sviluppatori di Google scoraggiano fortemente la comunicazione tra frammenti e frammenti. I frammenti sono pensati per essere un sottoinsieme di uno schermo.

L'overlay ha l'aspetto di due frammenti attivi allo stesso tempo.

FYI. È meglio utilizzare Transazioni frammento per manipolare più frammenti all'interno di un'attività. L'avvio di un'attività che avvia un frammento da xml non può essere realmente manipolato da Fragment Transactions.

Spero che questo aiuti.

0

Nella modifica AndroidMainifest.xml:

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

uso questo per rimuovere vista precedente