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

问题描述 投票:5回答:1

我想动态地将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个回答
4
投票

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

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

从父片段调用getChildFragmentManager(),并在父片段中执行事务以将子元素嵌套在内部。

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

告诉我你是否需要更多帮助...

© www.soinside.com 2019 - 2024. All rights reserved.