片段事务上的片段复制 [英] Fragment duplication on Fragment Transaction

查看:20
本文介绍了片段事务上的片段复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok, whenever I try to replace a fragment in my application, it only adds the fragment inside of the container the other fragment is, and leaves the current fragment. I've tried calling replace and referencing the view the contains the fragment, and by referencing the fragment itself. Neither of these work. I can add a fragment to a view with the fragment transaction manager, but even if I try to remove it after its been added, it doesn't work. Any help would be appreciated. Here are my files.

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();
}

这是布局

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

这是片段代码

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);
    }
}

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

Like I have said. I have tried referencing the fragments parent view to replace it, and the fragment itself (by id) and still, it only adds the new fragment, inside the containing view the original fragment is in.

推荐答案

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

I solved this by using a placeholder in my Layout and then attaching my Fragment to it at runtime.

像您一样,如果我在 xml 布局中实例化了 Fragment,那么在运行时将其替换为另一个 Fragment 后,内容将保持可见.

Like you, if I instantiated my Fragment within my xml layout then the contents would remain visible after replacing it with another Fragment at runtime.

这篇关于片段事务上的片段复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆