Youtubeview与RecyclerView加载

时间:2016-06-18 06:35:43

标签: android android-recyclerview android-youtube-api

我有recyclerview需要像列表一样加载youtube个视频,下面是代码。我已经使用了youtube api

以下代码的问题是只加载了最后一个视频,传递的值是正确且有效的。列表中的其他视频无法加载,并且有黑屏。

public class YoutubeAdapter extends RecyclerView.Adapter<YoutubeAdapter.MyViewHolder> {
    private String developer_key = "My Developer Key";
    private Context context;
    private ArrayList<String> video_id;
    private LayoutInflater inflater;

    public YoutubeAdapter(Context context, ArrayList<String> video_id) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.video_id = video_id;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        YouTubePlayerView youtubeView;
        public MyViewHolder(View view) {
            super(view);
            youtubeView = (YouTubePlayerView) view.findViewById(R.id.youtubeView);
        }
    }

    @Override
    public YoutubeAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.video_content,parent,false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final String video_string = video_id.get(position);
        holder.youtubeView.initialize(developer_key, new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                if (!b) {
                    youTubePlayer.cueVideo(video_string);
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                Toast.makeText(context, "Failed to load video", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

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

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,所以我尝试使用POJO类存储所有项目。然后在recyclerviewOnBindHolder中使用getter。它奏效了。

  private YouTube youTube;
    private YouTube.Search.List query;
    public static final String DEVELOPER_KEY="YOUR KEY";

    public YoutubeConnector(Context context) {

        youTube= new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest httpRequest) throws IOException {

            }
        }).setApplicationName(context.getString(R.string.app_name)).build();

        try {

            //this is to filter the query
            query= youTube.search().list("id,snippet");
            query.setKey(DEVELOPER_KEY);
            query.setChannelId(CHANNEL_ID);
            query.setType("video");
            query.setMaxResults((long) 50);
            query.setFields("items(id/videoId,snippet/title,snippet/description,snippet/thumbnails/default/url)");


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public List<VideoItem> queryResult(){

        try {
            // this is youtube built in SearchResponse
            SearchListResponse response= query.execute();

           List<SearchResult> listResult= response.getItems();

            List<VideoItem> videoItem= new ArrayList<>();

            for (SearchResult result:listResult){

                VideoItem item= new VideoItem();

                item.setTITLE(result.getSnippet().getTitle());
                item.setID(result.getId().getVideoId());
                item.setDESCRIPTION(result.getSnippet().getDescription());
                item.setURL(result.getSnippet().getThumbnails().getDefault().getUrl());

                videoItem.add(item);
            }

            return videoItem;

        } catch (IOException e) {
            e.printStackTrace();

            return null;
        }

    }

所以我做的是:

  1. 我创建了一个处理网络部分的类,如上所示。
  2. 我创建了一个名为VideoItem的POJO类。
  3. 我使用了SearchResponse内置的youtube - &gt; SearchListResponse response = query.execute();
  4. 4.存储在Pojo类中构建的Youtube中的所有响应  列表listResult = response.getItems();

    5.I循环listResult中的所有项目并将其存储在我自己的POJO类VideoItem中。

    6.我将所有的VideoItem存储在里面 列出videoItem = new ArrayList&lt;&gt;();