2011-09-15 11 views
6

こんにちは私は2つのフラグメントを含むlinearLayoutを持っており、このレイアウトにコード付きのタブを追加します。私が欲しいのは、tab1をクリックすると、指示されたクラスからそれ自身を断片化することは大丈夫ですが、tab2ではこのクラスを別のクラスに変更したいと思います。 <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" /> 

にXMLレイアウトであなたに

<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" /> 

答えて

24

変更<fragment/>に感謝し、プログラムの断片を追加します。

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

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

をしかし、最初にthisをお読みください。

+1

は、私は単に私のフラグメント= DoktorlarFragmentとでframeLayoutを置き換えるあなたの答えをありがとう()エラーも画面にも何も表示されません。 – tugce

3

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

addToBackStack(null)フラグメントを呼び出すの最短バージョンがスタック上かどうかの断片を保存したい場合は、オプションます。..

関連する問題