2016-06-18 64 views
0

我有一個recyclerview這需要加載youtube一個視頻下方的其他類似的列表,下面是代碼。我用這個youtube apiYoutubeview與RecyclerView裝載

與下面的代碼是,只有最後一個視頻被加載的問題,傳遞的值是正確的,工作。列表中的其他視頻無法加載,並且屏幕上出現黑屏。

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(); 
    } 
} 
+0

你的意思'YouTube的videos'的縮略圖沒有加載? –

+0

我只使用我的YouTube視圖,而不是thumbnailview ..會發生什麼,只有最後一個帶有按鈕的視頻是可見的,可以播放.. –

+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. 我使用YouTube內置SearchResponse - > SearchListResponse響應= query.execute();

4.將所有的響應存儲在建在Pojo類中的Youtube上 List listResult = response.getItems();

5.I循環全部來自listResult項目並將其存儲在自己的POJO類VideoItem。

6.I將我所有的VideoItem都存儲在 List videoItem = new ArrayList <>();