0

我在我的應用程序中隨FirebaseRecyclerAdapter和RecyclerView一起使用了Youtube API,出於某些原因,我的YouTube縮略圖未顯示在我的應用程序中,儘管我已將它添加到我的CardView中, FirebaseRecyclerAdapter有訪問CardView的代碼,但是當我啓動我的應用程序並轉到該活動時,那裏沒有顯示任何縮略圖。如果沒有Firebase適配器,我已將其實施爲硬編碼。CardView未在Android中顯示Youtube縮略圖

步驟我都試過:

  1. 新增佈局管理器

  2. 調整cardView和thumbnailView

    的高度和寬度

我使用過Android Studio

YouTube的類文件:

public class YoutubeVideos extends YouTubeBaseActivity { 

    private RecyclerView YouRecycler; 
    private DatabaseReference YDatabaseReference; 


    @Override 
    protected void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     setContentView(R.layout.youtube_videos); 
     YDatabaseReference = FirebaseDatabase.getInstance().getReference().child("youtubed"); 
     YouRecycler = (RecyclerView)findViewById(R.id.Youtube_recycler); 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

    FirebaseRecyclerAdapter<post2youtube, YoutubeViewHolder> YfirebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post2youtube, YoutubeViewHolder>(

      post2youtube.class, 
      R.layout.youtube_videos_card, 
      YoutubeViewHolder.class, 
      YDatabaseReference 
    ) { 
     @Override 
     protected void populateViewHolder(YoutubeViewHolder viewHolder, post2youtube model, int position) { 
      viewHolder.setYoutube(model.getYoutube()); 
     } 

    }; 

      YouRecycler.setAdapter(YfirebaseRecyclerAdapter); 
    }  


    public static class YoutubeViewHolder extends RecyclerView.ViewHolder { 
     View yView; 

     public YoutubeViewHolder(View YitemView) { 
      super(YitemView); 
      yView = YitemView; 
     } 
     public void setYoutube(final String youtube){ 
      final YouTubePlayerView youPlay = (YouTubePlayerView) yView.findViewById(R.id.youtuber); 
      youPlay.initialize("Some key", 
        new YouTubePlayer.OnInitializedListener() { 
         @Override 
         public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { 
          youTubePlayer.loadVideo(youtube); 
         } 

         @Override 
         public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 

         } 

        }); 
     } 

    } 
} 

的YouTube RecyclerView文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id ="@+id/Youtube_recycler"/> 

</LinearLayout> 

的YouTube cardView XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/youtube_cardView" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


    <com.google.android.youtube.player.YouTubePlayerView 
     android:id="@+id/youtuber" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 


</RelativeLayout> 

</android.support.v7.widget.CardView> 

回答

1

YoutubePlayer觀點是太大,被添加到recyclerview.So使用YouTubeThumbnailView來顯示縮略圖LS。當用戶點擊其中的一個時,您可以啓動YouTubePlayerFragment或帶有YouTubeplayerView視圖的活動。

下面是參考代碼,

在XML

<com.google.android.youtube.player.YouTubeThumbnailView 
        android:id="@+id/youtube_view" 
        android:layout_width="match_parent" 
        android:layout_height="180dp" 
        android:layout_marginBottom="@dimen/ten_dp" 
        android:visibility="gone"/> 

在Java:

// Initializing video player with developer key 
    youtube_view.initialize(Config.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() { 
     @Override 
     public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) { 
     youTubeThumbnailLoader.setVideo(videoId); 
     youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() { 
      @Override 
      public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) { 
       youTubeThumbnailLoader.release(); 
       Toast.makeText(getActivity(), "It's a valid youtube url.", Toast.LENGTH_SHORT).show(); 
      } 
      @Override 
      public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) { 
       try { 
         Toast.makeText(getActivity(), "Not a valid youtube url.", Toast.LENGTH_SHORT).show(); 
        } catch (Exception ex) { 
          ex.printStackTrace(); 
        } 
       } 
       }); 
      } 
      @Override 
      public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) { 
      } 
     }); 
+0

'Config'被示出的誤差不能解析符號,如何刪除它 –

+0

我已經解決了配置,但它仍然沒有顯示,還有我放在卡片視圖中的任何東西都沒有顯示 –

+0

Config是一個類,其中DEVELOPER_KEY是靜態缺點您可以在添加縮略圖視圖後使用任何其他類來代替Config –