以编程方式创建 YouTubePlayerSupportFragment (Android)
Create YouTubePlayerSupportFragment programmatically (Android)
我需要动态创建 YouTubePlayer,我不能 在 YoutubeBaseActivity 上扩展。所以我只需要创建 YouTubePlayerSupportFragment。我试过这样做:
container_more_info_item - 这是我的 linearLayout
//create video
for(int j = 0; j < 5; j++) {
final YouTubePlayerSupportFragment ysF = new YouTubePlayerSupportFragment();
ysF.initialize("AIzbSyAT5C-xs5YG1HzG69Fn__PHq9DHXBuKpws", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo("IjUlQU6yifk");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
//this line error, because ViewGroup can not be applied com.google.android.youtube.player.YouTubePlayerSupportFragment
container_more_info_item.addView(ysF);
}
现在我卡住了,想不出点什么
更新:
我使用 FragmentManager,但我收到以下消息:
Cannot resolve method 'add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)
//create video
for(int j = 0; j < 5; j++) {
final YouTubePlayerSupportFragment ysF = new YouTubePlayerSupportFragment();
ysF.initialize("AIzaSyAT5C-xs5YG1HzG69Fn__PHq9DHXBuKpws", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo("IjUlQU6yifk");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(j);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
//Cannot resolve method 'add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)
fragTransaction.add(ll.getId(), ysF);
fragTransaction.commit();
container_more_info_item.addView(ll);
}
YouTubePlayerSupportFragment
不是 View
,而是 Fragment
。
添加片段的过程与添加视图有点不同。
添加片段:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.your_fragment_container, ysF);
ft.commit();
https://developer.android.com/guide/components/fragments.html#Transactions
我需要动态创建 YouTubePlayer,我不能 在 YoutubeBaseActivity 上扩展。所以我只需要创建 YouTubePlayerSupportFragment。我试过这样做:
container_more_info_item - 这是我的 linearLayout
//create video
for(int j = 0; j < 5; j++) {
final YouTubePlayerSupportFragment ysF = new YouTubePlayerSupportFragment();
ysF.initialize("AIzbSyAT5C-xs5YG1HzG69Fn__PHq9DHXBuKpws", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo("IjUlQU6yifk");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
//this line error, because ViewGroup can not be applied com.google.android.youtube.player.YouTubePlayerSupportFragment
container_more_info_item.addView(ysF);
}
现在我卡住了,想不出点什么
更新:
我使用 FragmentManager,但我收到以下消息:
Cannot resolve method 'add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)
//create video
for(int j = 0; j < 5; j++) {
final YouTubePlayerSupportFragment ysF = new YouTubePlayerSupportFragment();
ysF.initialize("AIzaSyAT5C-xs5YG1HzG69Fn__PHq9DHXBuKpws", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo("IjUlQU6yifk");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(j);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
//Cannot resolve method 'add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)
fragTransaction.add(ll.getId(), ysF);
fragTransaction.commit();
container_more_info_item.addView(ll);
}
YouTubePlayerSupportFragment
不是 View
,而是 Fragment
。
添加片段的过程与添加视图有点不同。
添加片段:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.your_fragment_container, ysF);
ft.commit();
https://developer.android.com/guide/components/fragments.html#Transactions