2011-09-15 10 views
6

Witam Mam linearLayout zawierający dwa fragmenty i dodaję karty z kodem do tego układu. To, czego chcę, to po kliknięciu tab1, że można wypełnić fragmenty ze wskazanej klasy, ale w tab2 chcę zmienić tę klasę na inną. Dziękujęjak dynamicznie zmieniać klasę fragmentu

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/frags"> 

    <fragment class="com.tugce.MitsActionBar.DoktorlarFragment" 
      android:id="@+id/frag_title" 
      android:visibility="gone" 
      android:layout_marginTop="?android:attr/actionBarSize" 
      android:layout_width="@dimen/titles_size" 
      android:layout_height="match_parent" /> 

    <fragment class="com.tugce.MitsActionBar.ContentFragment" 
      android:id="@+id/frag_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

Odpowiedz

24

Zmień <fragment/> w układzie XML <FrameLayout/>

<FrameLayout 
     android:id="@+id/frag_title" 
     android:visibility="gone" 
     android:layout_marginTop="?android:attr/actionBarSize" 
     android:layout_width="@dimen/titles_size" 
     android:layout_height="match_parent" /> 

<FrameLayout 
     android:id="@+id/frag_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

i dodać fragmenty programowo:

FragmentManager fragmentManager = getFragmentManager() 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

ExampleFragment fragment = new ExampleFragment(); 
fragmentTransaction.replace(R.id.frag_content, fragment); 
fragmentTransaction.commit(); 

Ale najpierw przeczytać this.

+1

Dziękuję za odpowiedź po prostu zastąpić frameLayout z moim fragmentu = DoktorlarFragment() brak błędów i nic na ekranie nie masz pojęcia, dlaczego? – tugce

3

Najkrótsza wersja nazywając fragment

getFragmentManager().beginTransaction().replace(R.id.splash_container, new ExampleFragment()).addToBackStack(null).commit(); 

addToBackStack(null) jest opcjonalny, jeśli chcesz zapisać fragment na stosie lub nie ..

Powiązane problemy