我想动态地向已有的碎片中添加一个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中的支持库。这对我有用...