Android的碎片不更换正确碎片、正确、Android

2023-09-07 13:21:47 作者:幸福迷了路

我想建立3.0使用碎片的应用程序。关于应用程序的左侧有一个静态的片段,并在右侧有一个动态。我的每一个在动态部分片段有一个标题。每当我去代替最初的片段,第一次的标题上面第一的标题仍显示。连续更换更换下部,但最初的标题仍显示(抱歉,我不能发表图片还)。

I am trying to build an app for 3.0 using fragments. On the left side of the application there is a static fragment, and on the right side there is a dynamic one. Each of my fragments in the dynamic portion have a title. Whenever I go to replace the initial fragment, The title of the first is still shown above the title of the first. Successive replacements replace the lower portion but the initial title is still shown (sorry I cannot post images yet).

尽我所能为图像做的:

开始的标题

新标题

内容(显示细腻)

main.xml中:

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <fragment
        android:id="@+id/static_fragment"
        android:name="ruleout.android.History_Fragment"
        android:layout_width="500dp"
        android:layout_height="match_parent" />
    <fragment
        android:id="@+id/dynamic_fragment"
        android:name="ruleout.android.New_Screen_Fragment"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />
</LinearLayout>

用于更改片段部分MainActivity

Partial MainActivity used to change fragments

private void open(Fragment toShow)
{
    FragmentTransaction fragmentTransaction = FragMag.beginTransaction();
    fragmentTransaction.replace(R.id.dynamic_fragment, toShow);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

请让我知道如果你需要事先的任何其他信息,并感谢您的帮助。

Please let me know if you need any additional information and thanks in advance for your help.

推荐答案

如果你想动态改变的片段,你不能宣布他们在你的布局。 有做正是我想你所描述的,在那里的布局有一个例子碎片文件在它宣布连同一个的FrameLayout静态片段用于保持将被动态添加和替换一个片段。

If you want to dynamically change fragments, you can't declare them in your layout. The Fragment documentation has an example of doing just what I think you are describing, where a layout has a static fragment declared in it along with a FrameLayout for holding a Fragment that will be dynamically added and replaced.