在Android中使用ChildFragmentManager时片段替换不起作用

在Android中使用ChildFragmentManager时片段替换不起作用,android,android-fragments,fragment,android-fragmentactivity,Android,Android Fragments,Fragment,Android Fragmentactivity,我想将一个片段作为子片段添加到另一个片段 我正在使用ChildFragmentManager 这是我的 其布局如下所示 我成功地添加了父片段。请查看下面的图片 但是当我尝试添加子片段时,它显示如下 我希望通过替换以前的布局将子片段添加为内容 提前感谢事务不会删除将用于事务的容器中已经存在的视图。要删除这些视图,您需要将ParentFragment的初始内容包装为一个片段,并将其替换为子片段(使用replace事务,而不是add事务)。我对您的代码做了一些更改,请在下面查看: 父片段:

我想将一个片段作为子片段添加到另一个片段
我正在使用ChildFragmentManager

这是我的

其布局如下所示



我成功地添加了父片段。请查看下面的图片

但是当我尝试添加子片段时,它显示如下


我希望通过替换以前的布局将子片段添加为内容

提前感谢

事务不会删除将用于事务的容器中已经存在的视图。要删除这些视图,您需要将
ParentFragment
的初始内容包装为一个片段,并将其替换为子片段(使用
replace
事务,而不是
add
事务)。我对您的代码做了一些更改,请在下面查看:

父片段:

public class ParentFragment extends Fragment {

private static final int CONTAINER_ID = 0x2222;
private static final String INITIAL_FRAG = "initial_fragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    FrameLayout wrapper = new FrameLayout(getActivity());
    wrapper.setId(CONTAINER_ID);
    // look for our two possible fragments, if we don't find the
    // InitialContentFragment add it
    if (getChildFragmentManager().findFragmentByTag(INITIAL_FRAG) == null) {
        InitialContentFragment initContent = new InitialContentFragment();
        Bundle args = new Bundle();
        args.putString("text",
                "I'm the initial content fragment in the parent fragment");
        initContent.setArguments(args);
        getChildFragmentManager().beginTransaction()
                .add(CONTAINER_ID, initContent, INITIAL_FRAG).commit();
    }
    return wrapper;
}

public void requestFragmentTransaction() {
    FragmentTransaction fragmentTransaction = getChildFragmentManager()
            .beginTransaction();
    ChildFragment childFragment = new ChildFragment();
    Bundle args = new Bundle();
    args.putString("text", "Hi I am Child Fragment");
    childFragment.setArguments(args);
    fragmentTransaction.replace(CONTAINER_ID, childFragment, "ChildFragment");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}
}

其中
InitialContentFragment
为:

public static class InitialContentFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // inflate the layout file that would normally be in the
        // ParentFragment at start
        View view = inflater.inflate(R.layout.layout_parentfragment,
                container, false);
        Bundle bundle = getArguments();
        final String text = bundle.getString("text");
        TextView textView = (TextView) view.findViewById(R.id.textView1);
        textView.setText(text);
        Button button = (Button) view.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ParentFragment parent = (ParentFragment) InitialContentFragment.this
                        .getParentFragment();
                parent.requestFragmentTransaction();
            }
        });
        return view;
    }
}

请注意,不要像您那样忽略try-catch块。

“我希望通过替换以前的布局将子片段添加为内容。”您能更清楚地解释一下吗?之前的布局是什么?Hi koso感谢您的评论。红色部分(layout_parentfragment)是parentfragment的布局。我正在给它添加一个子片段。蓝色部分(layout_childfragment)是childfragment的布局。我希望蓝色被红色替换,这与片段替换方法完全相似。但是它只是添加了子片段,而不是替换它。如果在父级布局的开头添加高度=匹配父级且visiblity=消失的frameLayout呢。当您将片段添加到frameLayout时,请使该frameLayout可见,这样它将覆盖整个父片段布局。我想你想把这件事做好?是的,你是对的!如果我使用FrameLayout和parent_layout height作为match_parent,它将起作用。但在2种情况下它不起作用\n 1)**如果版面高度设置为“包裹内容”\n 2)如果父布局不是RelativeLayout或FrameLayout**。\n我想解决上述两个问题,请使用以下代码实现导航抽屉的代码。它的工作非常好,但是当你点击导航抽屉列表片段时问题就来了。“选项卡主机”选项卡正在显示并正常工作,但当返回到另一个活动并返回到“选项卡主机”活动时。选项卡正在显示,但选项卡主体的线性布局背景正在显示,如下面单击tabhost的图像所示。i、 stack.imgur.com/CzIsC.png i.stack.imgur.com/Dmxn3。png@B.rohitNare请发布一个新问题,提供问题的所有细节@Luksprog Hey mate跟随link@Luksprog,但在您的解决方案中,getChildFragmentManager()总是返回0?有线索吗?