【问题标题】:Add fragment into a fragment (nested fragment)将片段添加到片段中(嵌套片段)
【发布时间】:2013-08-14 04:26:16
【问题描述】:

我想动态地将一个 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 你是对的。我不得不替换“getActivity().getSupportFragmentManager();”使用“getChildFragmentManager()”。问题显然是 Sherlock 库附带了一个旧的 android v4 支持库。我不得不更新 Sherlock 中的支持库。它对我有用.....

【问题讨论】:

    标签: android android-fragments youtube-api android-viewpager android-nested-fragment


    【解决方案1】:

    要在片段中创建嵌套片段,您应该使用:

    http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()

    从父片段调用 getChildFragmentManager(), 并在父级中进行事务以将子级嵌套在其中。

    https://stackoverflow.com/a/13381825/1693674

    如果您需要更多帮助,请告诉我...

    【讨论】:

    • 感谢您的回复。我正在使用(扩展的)SherlockFragment,它不允许 getChildFragmentManager(),说未定义。确实做错了什么......
    • 还有 getParentFragment() 也说 undefine。你知道我为什么会得到这个 koby
    • 我其实不是 SherlockFragment 的忠实粉丝,但让我们试一试吧.. SherlockFragment 是否包含 support.v4 ?如果没有,您可以将其更改为这样做,那么您将能够获得 childfragment ..(我在黑暗中拍摄,所以如果它不起作用 - 上传一些代码,也许我们可以找到其他解决方案..)跨度>
    • 如何在该父片段上添加两个子嵌套片段
    猜你喜欢
    • 2012-11-18
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多