Fragment Transaction上的片段重复

Sha*_*aun 45 android transactions android-fragments

好的,每当我尝试替换我的应用程序中的片段时,它只会将容器内部的片段添加到另一个片段中,并保留当前片段.我试过调用replace并引用包含片段的视图,并引用片段本身.这些都不奏效.我可以使用片段事务管理器将一个片段添加到视图中,但即使我尝试在添加它之后将其删除,它也不起作用.任何帮助,将不胜感激.这是我的文件.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Setup the actionabar. Use setDrawableBackground to set a background image for the actionbar.
    final ActionBar actionbar = getActionBar();
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayUseLogoEnabled(true);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.addTab(actionbar.newTab().setText(R.string.home_tab_text).setTabListener(this),true);
    actionbar.addTab(actionbar.newTab().setText(R.string.insert_tab_text).setTabListener(this));

    Fragment fragment = new insert_button_frag();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.button_fragment, fragment);
    transaction.addToBackStack(null);

    transaction.commit();
}
Run Code Online (Sandbox Code Playgroud)

这是布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/button_fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <fragment
            android:name="com.bv.silveredittab.home_button_frag"
            android:id="@+id/button_fragment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <fragment
            android:name="com.bv.silveredittab.quick_insert_frag"
            android:id="@+id/quick_insert_frag"
            android:layout_width="350dip"
            android:layout_height="fill_parent" />

        <fragment
            android:name="com.bv.silveredittab.editor_frag"
            android:id="@+id/editor_frag"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是片段代码

public class insert_button_frag extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        return inflater.inflate(R.layout.insert_buttons,container, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

就像我说的那样.我已经尝试引用片段父视图来替换它,片段本身(通过id)仍然只是在原始片段所在的包含视图中添加新片段.

Dam*_*ian 34

我通过在布局中使用占位符然后在运行时将我的片段附加到它来解决这个问题.

和你一样,如果我在我的xml布局中实例化我的Fragment,那么在运行时用另一个Fragment替换它之后内容将保持可见.

  • 这就是我最终要做的事情.有点迟钝不是吗? (4认同)

Bri*_*ley 18

问题是这一行:

transaction.replace(R.id.button_fragment, fragment);
Run Code Online (Sandbox Code Playgroud)

replace有两个重载形式.在它们中,第一个参数是要替换的Fragment 的容器,而不是Fragment本身.所以,在你的情况下,你需要打电话

transaction.replace(R.id.button_fragment_container, fragment);
Run Code Online (Sandbox Code Playgroud)

编辑:我在你的问题中看到你们都试过了.我已经测试并验证了这种行为.这似乎是FragmentTransaction API中的一个错误.

编辑2:毕竟不是一个错误.您根本无法替换布局文件中静态添加的片段.您只能替换以编程方式添加的内容(Android:不能将一个片段替换为另一个片段)