在我的Android应用程序中,我通过YouTubePlayerFragment
将Fragment
放在另一个FrameLayout
中,我只需将Youtubeplayer
添加到其中YouTubePlayerFragment
。
这一切都按预期工作。
现在,如果手机没有安装Youtube App,这是Youtube API运行所必需的,我不想通过FragmentTransaction
将boolean youtubeSuccess
添加到根片段。
我想到的只是创建一个onInitializationFailure
并将其设置为true,如果....
if (youtubeSuccess) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
}
return inflater.inflate(layoutResId, container, false);
.....
被调用,我将其设置为false。所以稍后我可以简单地使用它:
public class SampleYouTubeSlide extends Fragment {
private FragmentActivity myContext;
private static final String ARG_LAYOUT_RES_ID = "layoutResId";
private int layoutResId;
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "XXXXXXXXXXXXXXXXX";
private static final int RECOVERY_DIALOG_REQUEST = 1;
static boolean youtubeSuccess;
public SampleYouTubeSlide() {
}
@Override
public void onAttach(Activity activity) {
if (activity instanceof FragmentActivity) {
myContext = (FragmentActivity) activity;
}
super.onAttach(activity);
}
public static SampleYouTubeSlide newInstance(int layoutResId) {
SampleYouTubeSlide sampleSlide = new SampleYouTubeSlide();
Bundle args = new Bundle();
args.putInt(ARG_LAYOUT_RES_ID, layoutResId);
sampleSlide.setArguments(args);
return sampleSlide;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null && getArguments().containsKey(ARG_LAYOUT_RES_ID))
layoutResId = getArguments().getInt(ARG_LAYOUT_RES_ID);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
youTubePlayerFragment.initialize(YoutubeDeveloperKey, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
youtubeSuccess = true;
if (!wasRestored) {
YPlayer = player;
YPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
//YPlayer.setFullscreen(true);
YPlayer.cueVideo(getResources().getString(R.string.instructionsVideoId), 1);
//YPlayer.loadVideo(getResources().getString(R.string.instructionsVideoId));
//YPlayer.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
// Called when initialization of the player fails.
youtubeSuccess = false;
}
});
if (youtubeSuccess) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
}
return inflater.inflate(layoutResId, container, false);
}
}
现在我不知道我是否正确理解生命周期,或者我是否盲目找到明显的错误。如果,为什么youtubeSuccess永远不会被设置?
完整代码:
count = dataTable.DefaultView.ToTable(true, "employeeid").rows.count