我正在尝试使用youtube播放列表的youtube缩略图进行回收查看,只需使用其播放列表。播放列表的每个缩略图都应位于该回收者视图的每一行中,当用户点击按钮上方的特定缩略图时,它将在YoutubePlayerView内的其他活动中打开该特定视频。 这是我的代码:
VideoListAdapter.java
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.VideoListViewHolder> {
private Context context;
private int numberOfVideosInPlaylist = 10;
private String videoId;
public VideoListAdapter(Context context) {
this.context = context;
}
public String getVideoId() {
return videoId;
}
public void setVideoId(String videoId) {
this.videoId = videoId;
}
@Override
public VideoListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tile_view, parent, false);
VideoListViewHolder holder = new VideoListViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final VideoListViewHolder holder, final int position) {
holder.playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, PlayVideoActivity.class);
i.putExtra("VideoId", getVideoId());
context.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return numberOfVideosInPlaylist;
}
class VideoListViewHolder extends RecyclerView.ViewHolder {
private YouTubeThumbnailView youTubeThumbnailView;
private ImageView playButton;
public VideoListViewHolder(View itemView) {
super(itemView);
youTubeThumbnailView = itemView.findViewById(R.id.tile);
playButton = itemView.findViewById(R.id.playbutton);
youTubeThumbnailView.initialize(DeveloperKeyAndPlayListId.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setPlaylist(DeveloperKeyAndPlayListId.PLAYLIST_ID);
YouTubeThumbnailLoader.OnThumbnailLoadedListener onThumbnailLoadedListener = new ThumbnailListener(youTubeThumbnailLoader);
youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(context, "Unable to load thumbnail", Toast.LENGTH_LONG).show();
}
});
}
}
private final class ThumbnailListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
private YouTubeThumbnailLoader youTubeThumbnailLoader;
public ThumbnailListener() {
super();
}
public ThumbnailListener(YouTubeThumbnailLoader youTubeThumbnailLoader) {
this();
this.youTubeThumbnailLoader = youTubeThumbnailLoader;
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
if (youTubeThumbnailLoader.hasNext()) {
youTubeThumbnailLoader.next();
}
setVideoId(s);
}
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
Toast.makeText(context, "Unable to load thumbnail", Toast.LENGTH_LONG).show();
}
}
}
PlayVideoActivity.java
public class PlayVideoActivity extends YouTubeBaseActivity {
private YouTubePlayerView youTubePlayerView;
private YouTubePlayer.OnInitializedListener onInitializedListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video);
youTubePlayerView = findViewById(R.id.youtube_player);
onInitializedListener = new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
Bundle bundle = getIntent().getExtras();
String videoId = bundle.getString("VideoId");
youTubePlayer.loadVideo(videoId);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(PlayVideoActivity.this, "Unable to play video", Toast.LENGTH_LONG).show();
}
};
youTubePlayerView.initialize(DeveloperKeyAndPlayListId.DEVELOPER_KEY, onInitializedListener);
}
}
这就是我想要的:
点击该播放按钮:
在我的代码中,我对播放列表中的视频数量进行了硬编码。如果你可以从播放列表中获取它,那将非常有帮助。