2012-11-10 16 views
9

Il mio requisito è abbastanza semplice: ho un pulsante che dovrebbe sostituire un FragmentA di FragmentB.FragmentTransaction.remove non ha effetto

Sembra facile e quasi funziona, il grosso problema è che il vecchio frammento non viene rimosso e il nuovo posto sul davanti di quello vecchio e stanno "vivendo" insieme nel mio layout.

enter image description here

Il Codice:

FragmentManager fragMgr = a.getSupportFragmentManager(); 
Fragment currentFragment = (Fragment) fragMgr.findFragmentById(R.id.fragmentitself); 

if(currentFragment!=null){ 

    FragmentTransaction fragTrans = fragMgr.beginTransaction(); 
    fragTrans.remove(currentFragment); 

    FragmentB newFragment = new FragmentB(); 
    fragTrans.replace(R.id.fragmentcontainer, newFragment); 
    // I have also tried with R.id.fragmentitself 
    fragTrans.addToBackStack(null); 
    fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    fragTrans.commit(); 
} 

il layout:

<FrameLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer"> 

    <fragment 
     android:id="@+id/fragmentitself" 
     android:name="com.WazaBe.MyApp.FragmentA" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</FrameLayout> 

risposta

14

Soluzione

In primo luogo, è necessario rimuovere il frammento di XML e tenere solo contenitore vuoto c'è:

<FrameLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer" /> 

allora avete bisogno di aggiungere il com.WazaBe.MyApp.FragmentA frammento dal codice, vale a dire in onCreate() della vostra attività genitore.

Spiegazione

è perché con le transazioni di manipolare ViewGroup come FrameLayouts che è contenitore per frammenti. Ma quando si posiziona il frammento direttamente nel layout XML, diventa permanente parte della gerarchia View e poiché è permanente, non può essere rimosso dal codice.

Una volta ottenuto il layout fisso, la chiamata remove() non è più necessario - sarà sufficiente fare proprio replace()

+0

di lavoro! Grazie. –

5

Se si desidera inserire un frammento in una vista del contenitore (come un Framelayout), è necessario assicurati che il tuo contenitore sia vuoto (solo questo puoi inserire un frammento in esso). Non puoi sostituire un frammento scritto in un file XML, dovresti aggiungere A nel contenitore con il codice JAVA, e quando non lo usi, può sostituirlo con B;

in un primo momento, il vostro contenitore è empyt:

<FrameLayout 
android:layout_width="0dp" 
android:layout_height="match_parent" 
android:layout_weight="2" 
android:id="@+id/fragmentcontainer"> 
</FrameLayout> 

OK, si mette Fragmenta in esso:

FragmentTransaction fragTrans = fragMgr.beginTransaction(); 
fragTrans.remove(currentFragment); 
FragmentA fragA= new FragmentA(); 
fragTrans.add(R.id.fragmentcontainer, fragA).commit(); 

Ora, se si desidera sostituire:

FragmentTransaction fragTrans = fragMgr.beginTransaction(); 
FragmentB newFragment = new FragmentB(); 
fragTrans.replace(R.id.fragmentcontainer, newFragment); 
// I have also tried with R.id.fragmentitself 
fragTrans.addToBackStack(null); 
fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
fragTrans.commit(); 
+0

Scusate, l'altro ragazzo era più veloce, grazie mille per la risposta. –

Problemi correlati