我的应用程序在家庭活动中具有三个选项卡。在第一个标签中,我想要一个recyclerView和一个recyclerView下面的youtube视频播放器。在HomeActivity代码下方
public class HomeActivity extends AppCompatActivity {
private DatabaseReference databaseReference;
private FirebaseUser mUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
databaseReference = FirebaseDatabase.getInstance().getReference();
mUser = FirebaseAuth.getInstance().getCurrentUser();
final Toolbar toolbar = findViewById(R.id.home_toolbar_id);
final ViewPager mViewPager = findViewById(R.id.home_view_pager_id);
final TabLayout mTabLayout = findViewById(R.id.home_tab_lyt_id);
setSupportActionBar(toolbar);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
}
/*-------------------------------------------------------------------------------*/
class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new HomeFragment();
case 1:
return new QuestionFragment();
case 2:
return new CoursesFragment();
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Home";
case 1:
return "Questions";
case 2:
return "Courses";
}
return null;
}
}
/*--------------------------------------------------------*/
}
下面是HomeFragment,我想要一个recyclerView和Youtube播放器视图。
public class HomeFragment extends Fragment {
YouTubePlayerView mYouTubePlayerView;
YouTubePlayer.OnInitializedListener mOnInitializatedListener;
private String API_KEY = "My-Key";
public HomeClassesFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_classes, container, false);
mYouTubePlayerView = view.findViewById(R.id.frag_home_classes_player_id);
mYouTubePlayerView.initialize(API_KEY, mOnInitializatedListener);
mOnInitializatedListener = new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo("h_i2jDbMGwA");
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
};
return view;
}
}
但这会产生以下错误-
android.view.InflateException: Binary XML file line #16: Error inflating class com.google.android.youtube.player.YouTubePlayerView
如何在此片段本身中播放youtube视频。
下面是该片段的XML布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".general_fragments.HomeClassesFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>