【问题标题】:Fragment replace not working when using ChildFragmentManager in Android在 Android 中使用 ChildFragmentManager 时片段替换​​不起作用
【发布时间】:2013-07-06 20:28:46
【问题描述】:

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

这是我的ChildFragmentManagerActivity.java
ChildFragment.java
ParentFragment.java
,其布局如下
activity_childfragmentmanager.xml
layout_parentfragment.xml
layout_childfragment.xml
>

我能够成功添加 ParentFragment。请查看下图

但是当我尝试添加 ChildFragment 时,它显示如下

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

【问题讨论】:

  • "我希望通过替换之前的布局将子片段添加为内容。"你能更清楚地解释一下吗?以前的布局是什么?
  • 您好 koso 感谢您的评论。红色部分(layout_parentfragment)是ParentFragment的布局。我正在向它添加一个子片段。蓝色部分(layout_childfragment)是ChildFragment的布局。我希望将蓝色替换为完全类似于片段替换方法的红色。但它只是添加 ChildFragment 而不是替换它。
  • 如果你在 parent_layout 的开头添加 frameLayout,height=match_parent 和 visiblity=gone。当您将片段添加到 frameLayout 时,使该 frameLayout 可见,因此它将覆盖整个父片段布局。我想你想做到这一点?
  • 是的,你是对的!如果我使用 FrameLayout 和 parent_layout 高度作为 match_parent 它正在工作。但它在 2 种情况下不起作用 \n 1)** 如果布局高度设置为“wrap_content”。 \n 2)如果父布局不是RelativeLayout或者FrameLayout**。\n我想解决以上两种情况

标签: android android-fragments fragment android-fragmentactivity


【解决方案1】:

事务不会删除将用于事务的容器中已存在的视图。要删除这些视图,您需要将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 块。

【讨论】:

  • 我已经使用以下代码实现了导航抽屉的代码。stackoverflow.com/questions/23678513/… 并且它的工作非常好,但是当您单击导航抽屉列表片段时出现问题。选项卡主机选项卡出现并且工作得很好,但是当返回另一个活动并返回选项卡主机活动时。选项卡正在出现,但选项卡主机的线性布局背景正在出现,如下图所示,单击一次和两次的选项卡主机。 i.stack.imgur.com/CzIsC.png i.stack.imgur.com/Dmxn3.png
  • @B.rohitNare 请发布一个新问题,提供您问题的所有详细信息。
  • stackoverflow.com/questions/26483069/… @Luksprog 嘿伙计点击链接
  • @Luksprog,但似乎在您的解决方案中, getChildFragmentManager().getBackStackEntryCount() 总是返回 0 ?有什么线索吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多