2011-06-04 7 views
3

J'ai une mise en page demise à jour du fragment avec un nouveau fragment Android

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" > 
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content"> 
     <LinearLayout android:id="@+id/linearLayout13" android:layout_height="wrap_content" android:paddingRight="70dp" android:orientation="vertical" android:paddingLeft="70dp" android:layout_width="wrap_content"> 
      <ImageView android:id="@+id/baby_icon" android:layout_height="wrap_content" android:src="@drawable/baby" android:clickable="true" android:layout_width="wrap_content"></ImageView> 
     </LinearLayout> 
    </LinearLayout> 
    <fragment 
     android:name="com.nicu.health.FragAFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/yellow_cardlist_fragment" 
     android:layout_weight="1" 
     > 
    </fragment> 
</LinearLayout> 

Au démarrage cela ne montre le fragment correct (FragAFragment). Maintenant sur le bouton cliquez sur baby_icon J'essaye de supprimer le fragment actuel et d'en ajouter un nouveau (FragBFragment) qui a une disposition entièrement différente.

Bien que je vois que la méthode onCreateView est appelée et qu'elle renvoie une vue non null mais l'interface utilisateur sur l'écran du nouveau fragment n'est pas mise à jour. J'utilise le code ci-dessous pour mettre à jour le fragment. J'avais essayé toutes les combinaisons d'enlever, remplacer et ajouter pour obtenir le fragment de travail, mais en vain!

également d'autres j'ai essayé avec le code comme

<fragment 
    android:name="com.nicu.health.FragBFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/yellow_cardlist_fragment" 
    android:layout_weight="1" 
    > 

et cela ne fonctionne pour montrer le second fragment au démarrage !!!!

L'aide sera vraiment appréciée.

Merci,

Répondre

0

Ok, ma première estimation: Vous avez oublié de dire ft.commit();

Ma deuxième hypothèse:

Disons que u ont une activité AB qui contrôle Fragment A et B. Fragment

Vous avez également 3 mises en page. Layout_ActivityAB, Layout_FragA, Layout_FragB.

Ainsi setContentView (R.layout.Layout_ActivityAB) est appelé dans Activity AB. Ce sera la disposition dans laquelle vous dites à android où placer le fragment dans l'écran.

intérieur Fragment A ou B Fragment:

@Override 
    //onCreateView is called when an instance of this class is first created. 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.Layout_FragA, container, false); return rootView; 
    } 

R.layout.Layout_FragA est appelé à l'intérieur du fragment. Layout_FragA est le contenu actuel du fragment.

Questions connexes