将片段添加到片段中(嵌套片段)

6

我想动态地向已有的碎片中添加一个Youtube碎片。我使用的代码如下:

        // setting the Youtube Player Dynamically
private int setYoutubePlayer(String desc, View view, int prevID,
        Bundle input) {

    if (desc.indexOf("=") != -1) {
        desc = desc.substring(desc.indexOf("=") + "=".length());
    } else {
        return prevID;
    }

    final String url = desc;

    LinearLayout videoLayout = new LinearLayout(view.getContext());
    videoLayout.setOrientation(LinearLayout.VERTICAL);
    prevID++;
    videoLayout.setId(prevID);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    fragment.setVideoId(url);
    LinearLayout itemLayout = (LinearLayout) view.findViewById(R.id.items);
    itemLayout.addView(videoLayout);

    fragmentTransaction.add(itemLayout.getId(), fragment,
            "youtube fargment " + prevID);

    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

    return prevID;
}

我需要在适当的片段中获取youtube片段。当每次加载新片段(在片段之间滑动时)时,我检查新的内部片段需要是第一个加载的片段。
欢迎任何帮助。
已解决:谢谢Koby,你是对的。我必须用"getChildFragmentManager()"替换"getActivity().getSupportFragmentManager();"。问题显然是Sherlock库带有旧的android v4支持库。我不得不更新Sherlock中的支持库。这对我有用...
1个回答

5

谢谢您的回复。我正在使用(扩展的)SherlockFragment,但它不允许使用getChildFragmentManager(),提示未定义。我做错了什么吗? - Mohamed Sabri Rauf
并且getParentFragment()也显示未定义。您有任何想法为什么我会得到这个错误? - Mohamed Sabri Rauf
我其实不是很喜欢SherlockFragment,但让我们试一试吧。SherlockFragment是否包含support.v4?如果没有,你可以更改它以包含support.v4,然后你就能够获取childfragment了。(我在瞎猜,如果不行的话,请上传一些代码,也许我们可以找到其他解决方案..) - koby
如何在父片段上添加两个子嵌套片段 - Vishvendu Palawat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接