编辑cjohn捕获了丢失的部分。我忘记在我的YTPlayerFragment上调用init()
。
Android开发人员的新手。我正在尝试将YouTube视频放入嵌套在另一个片段内的YouTubePlayerFragment内。 Logcat没有显示任何错误,但是我的播放器片段仅显示一个黑框。将日志语句放在我的OnInitializedListener内,似乎甚至没有被调用。
包含YouTubePlayerFragment的ContentFragment是ViewPager中的一个选项卡。我的代码如下。 MainActivity ContentFragment,ContentFragment包含YouTubePlayerFragment。
MainActivity.java:
public class MainActivity extends FragmentActivity{
TabsPagerAdapter mAdapter;
ViewPager mViewPager;
Boolean mLoggedIn;
String[] mTabs = {"Library", "Content", "Quotes"};
@Override
protected void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mViewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
for (String tab : mTabs) {
actionBar.addTab(actionBar.newTab().setText(tab).setTabListener(tabListener));
}
}
}
TabsPagerAdapter.java:
public class TabsPagerAdapter extends FragmentPagerAdapter{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new LibraryFragment();
case 1:
return new ContentFragment();
case 2:
return new QuotesFragment();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
ContentFragment.java:
public class ContentFragment extends Fragment {
public ContentFragment() {
// Required empty public constructor
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
View v = inflater.inflate(R.layout.fragment_content, container, false);
YTPlayerFragment video = YTPlayerFragment.newInstance("nCgQDjiotG0");
getFragmentManager().beginTransaction().replace(R.id.youtubeFragment, video).commit();
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
YTPlayerFragment.java:
public class YTPlayerFragment extends YouTubePlayerSupportFragment {
private YouTubePlayer activePlayer;
private final static String DEV_KEY = "KEY";
public static YTPlayerFragment newInstance(String url) {
YTPlayerFragment player = new YTPlayerFragment();
Bundle bundle = new Bundle();
bundle.putString("url", url);
player.setArguments(bundle);
return player;
}
private void init() {
initialize(DEV_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
activePlayer = youTubePlayer;
Log.d("YOUTUBE", "Successfully initialized");
activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
if (!b) {
activePlayer.cueVideo(getArguments().getString("url"));
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.e("YOUTUBE", youTubeInitializationResult.toString());
}
});
}
}
fragment_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"
android:orientation="vertical" >
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/youtubeFragment" />
</LinearLayout>
最佳答案
在您的ContentFragment
中,您试图将另一个Fragment作为孩子添加。为此,您需要通过调用getChildFragmentManager()
获得的子片段管理器:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
View v = inflater.inflate(R.layout.fragment_content, container, false);
YTPlayerFragment video = YTPlayerFragment.newInstance("nCgQDjiotG0");
getChildFragmentManager().beginTransaction().replace(R.id.youtubeFragment, video).commit();
return v;
}