我使用以下代码在YouTube播放列表中填充列表视图
它可以很好地显示图像,但是我将如何检索视频标题?
它使用YouTubeThumbnailLoader加载图像,但是标题是等效的。我可以轻松获取该网址,因此另一种可能性就是以某种方式获取标题。
public class YoutubePlaylist extends AppCompatActivity implements YouTubeThumbnailView.OnInitializedListener, YouTubeThumbnailLoader.OnThumbnailLoadedListener, YouTubePlayer.OnInitializedListener {
YouTubeThumbnailView thumbnailView;
YouTubeThumbnailLoader thumbnailLoader;
RecyclerView VideoList;
RecyclerView.Adapter adapter;
List<Drawable> thumbnailViews;
List<String> VideoId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youtube_playlist_layout);
thumbnailViews = new ArrayList<>();
VideoList = (RecyclerView) findViewById(R.id.VideoListPlaylist);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
VideoList.setLayoutManager(layoutManager);
adapter=new VideoListAdapter();
VideoList.setAdapter(adapter);
VideoId = new ArrayList<>();
thumbnailView = new YouTubeThumbnailView(this);
thumbnailView.initialize("XXXXX", this);
//playerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.VideoFragmentPlaylist);
//playerFragment.initialize("XXXXX", this);
}
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
thumbnailLoader = youTubeThumbnailLoader;
youTubeThumbnailLoader.setOnThumbnailLoadedListener(YoutubePlaylist.this);
thumbnailLoader.setPlaylist("XXXXX");
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}
public void add() {
adapter.notifyDataSetChanged();
if (thumbnailLoader.hasNext())
thumbnailLoader.next();
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
thumbnailViews.add(youTubeThumbnailView.getDrawable());
VideoId.add(s);
add();
}
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.MyView>{
public class MyView extends RecyclerView.ViewHolder{
ImageView imageView;
protected TextView videosTitleTextView;
public MyView(View itemView) {
super(itemView);
imageView= (ImageView) itemView.findViewById(R.id.thumbnailView);
videosTitleTextView = (TextView) itemView.findViewById(R.id.title);
}
}
@Override
public VideoListAdapter.MyView onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_row2, parent, false);
return new MyView(itemView);
}
@Override
public void onBindViewHolder(VideoListAdapter.MyView holder, final int position) {
Toast.makeText(YoutubePlaylist.this, VideoId.get(position).toString(), Toast.LENGTH_SHORT).show();
holder.imageView.setImageDrawable(thumbnailViews.get(position));
holder.videosTitleTextView.setText("1234567890");
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Player.cueVideo(VideoId.get(position));Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(YoutubePlaylist.this, WebViewActivity.class);
myIntent.putExtra("id", VideoId.get(position));
YoutubePlaylist.this.startActivity(myIntent);
}
});
}
@Override
public int getItemCount() {
return thumbnailViews.size();
}
}
}