如何动态更改片段的类

tug*_*gce 7 android android-layout android-fragments

嗨我有一个包含两个片段的linearLayout,我在这个布局中添加了代码选项卡.我想要的是当我点击tab1时可以从指定的类中填充自己的片段,但是在tab2中我想将这个类更改为另一个类.谢谢

<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" />
Run Code Online (Sandbox Code Playgroud)

paw*_*eba 25

<fragment/>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" />
Run Code Online (Sandbox Code Playgroud)

并以编程方式添加片段:

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

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.replace(R.id.frag_content, fragment);
fragmentTransaction.commit();
Run Code Online (Sandbox Code Playgroud)

但是一开始看这个.