在片段的交易片段重复片段

2023-09-12 07:18:02 作者:隐身告白

好了,每当我试图取代片段在我的应用程序,它只是增加了碎片的容器中的其他片段内,而离开当前片段。香港专业教育学院试图调用替换和引用包含片段的观点,并通过引用片段本身。无论这些工作。我可以添加一个片段与片段事务管理器视图,但即使我尝试将其删除它被添加后,它不工作。任何帮助将是AP preciated。下面是我的文件。

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. Ive 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 doesnt 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>

这里是片段code

And here is the fragment code

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布局实例化我的片段则内容将在运行时与另一片段更换后仍然可见。

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.