我想动态地将一个 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 片段。当我检查何时总是加载新片段时(在片段之间滑动时),新的内部片段需要是第一个加载的片段。
任何帮助都将被欣然接受。
已解决:谢谢科比,你是对的。我不得不替换“getActivity().getSupportFragmentManager();” 使用“getChildFragmentManager()”。问题显然是 Sherlock 库附带了一个旧的 android v4 支持库。我不得不更新 Sherlock 中的支持库。它对我有用......