我想将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;
}
Run Code Online (Sandbox Code Playgroud)
我需要将YouTube片段放入适当的片段中。当我检查何时总是加载新片段时(在片段之间滑动时),新的内部片段需要是第一个加载的片段。
任何帮助都会很高兴地被接受。
求助:谢谢科比,你是对的。我不得不替换“ getActivity()。getSupportFragmentManager();” 与“ …
android youtube-api android-fragments android-viewpager android-nested-fragment