我有一个活动,其中一个片段中有2个或3个片段我想在回收站视图中显示Youtube视频我试过的是
1
当我尝试访问处于recyclerview项目布局中的YouTubePlayerFragment时 我收到了这个错误
"Type parameter T has incompatible upper bounds: View YouTubePlayerFragment"
public static class MyVideoViewHolder extends RecyclerView.ViewHolder{
YouTubePlayerFragment youTubePlayerFragment;
public MyVideoViewHolder(View itemView) {
super(itemView);
youTubePlayerFragment=itemView.findViewById(R.id.youtube_player_fragment);
}
}
我无法使用findViewById访问片段而且无法在此处获取上下文我不知道如何在此处调用findFragmentById。
2。
我尝试的另一个解决方案是在recyclelerview项目布局中使用frameLayout,然后替换该framelayout中的片段,但这也无法正常工作
"No view found for id 0x7f070096 for fragment YouTubePlayerFragment"
public void onBindViewHolder(@NonNull MyVideoViewHolder holder, final int position) {
YouTubePlayerFragment youTubePlayerFragment=YouTubePlayerFragment.newInstance();
((Activity)mContext).getFragmentManager().beginTransaction().replace(holder.youtubePlayerFrameLayout.getId(),youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(YoutubePlayerConfig.API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo(videoListDataArrayList.get(position));
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.d(TAG,"initilization fail");
}
});
答案 0 :(得分:1)
您可以使用Android-YouTube-Player轻松完成此操作。
只需创建一个包含YouTubePlayerView
的ViewHolder即可。
static class ViewHolder extends RecyclerView.ViewHolder {
private YouTubePlayerView youTubePlayerView;
private YouTubePlayer youTubePlayer;
private String currentVideoId;
ViewHolder(YouTubePlayerView playerView) {
super(playerView);
youTubePlayerView = playerView;
youTubePlayerView.initialize(initializedYouTubePlayer ->
initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady() {
youTubePlayer = initializedYouTubePlayer;
youTubePlayer.cueVideo(currentVideoId, 0);
}
}), true
);
}
void cueVideo(String videoId) {
currentVideoId = videoId;
if(youTubePlayer == null)
return;
youTubePlayer.cueVideo(videoId, 0);
}
}
}
其余代码只是您通常的回收站视图代码。
您可以看到完整的示例here。您可以在sample app中尝试。