Android Studio中的Youtube播放列表到Listview

时间:2016-05-16 12:19:57

标签: android listview youtube playlist

我在我的项目中实现了Youtube lib,我想要一个Listview来显示播放列表中的视频。

我发现了这个: Parsing Youtube playlist to Listview

并且: Integrating youtube playlists in android

但没有任何帮助我。 感谢

2 个答案:

答案 0 :(得分:3)

public class MainActivity extends AppCompatActivity implements YouTubeThumbnailView.OnInitializedListener, YouTubeThumbnailLoader.OnThumbnailLoadedListener, YouTubePlayer.OnInitializedListener {


    YouTubePlayerFragment playerFragment;
    YouTubePlayer Player;
    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.activity_main);
        thumbnailViews = new ArrayList<>();
        VideoList = (RecyclerView) findViewById(R.id.VideoList);
        RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
        VideoList.setLayoutManager(layoutManager);
        adapter=new VideoListAdapter();
        VideoList.setAdapter(adapter);
        VideoId = new ArrayList<>();
        thumbnailView = new YouTubeThumbnailView(this);
        thumbnailView.initialize("API KEY", this);
        playerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.VideoFragment);
        playerFragment.initialize("API KEY", this);
    }


    @Override
    public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
        thumbnailLoader = youTubeThumbnailLoader;
        youTubeThumbnailLoader.setOnThumbnailLoadedListener(MainActivity.this);
        thumbnailLoader.setPlaylist("PLAYLIST ID");
    }

    @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) {
        Player=youTubePlayer;
        Player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
            @Override
            public void onFullscreen(boolean b) {
                VideoList.setVisibility(b?View.GONE:View.VISIBLE);
            }
        });
    }

    @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;
        public MyView(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.thumbnailView);
        }

    }

    @Override
    public VideoListAdapter.MyView onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_row, parent, false);
        return new MyView(itemView);
    }

    @Override
    public void onBindViewHolder(VideoListAdapter.MyView holder, final int position) {
        holder.imageView.setImageDrawable(thumbnailViews.get(position));
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Player.cueVideo(VideoId.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return thumbnailViews.size();
    }
}
}

activity_main & video_row

答案 1 :(得分:1)

您可以使用recyclerView作为列表。我用Youtube api休息电话完成了这个。您需要从Google开发者控制台获取浏览器密钥。我在这里的答案中解释了这一点:

https://stackoverflow.com/a/41201084/3689744