YouTubePlayerFragment无法在RecycleView中使用

时间:2016-12-15 09:29:53

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

我正在开发一个使用Fragments完成大部分工作的应用。在一个片段中,我必须显示包含Youtube播放器的循环视图。但是Youtube Player只出现在第一行的循环视图中。我有这个代码用于recycleView Adapter。

 public static class VideoViewHolder  extends RecyclerView.ViewHolder  {
       public FrameLayout ViewVideo;
     ViewVideo = (FrameLayout) itemView.findViewById(R.id.MainVideo);
    }

关于Bind方法

public void onBindViewHolder(VideoViewHolder holder, final int position) {  
youTubePlayerFragment = new YouTubePlayerSupportFragment();   
        FragmentTransaction transaction = VideoPage.getChildFragment().beginTransaction();
        transaction.replace(holder.ViewVideo.getId(), youTubePlayerFragment).commit();
        youTubePlayerFragment.initialize("****************************", new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                if (!b) {
                    youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                    youTubePlayer.loadVideo(VideoAvaliable[position]);
//                    youTubePlayer.play();
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                String errorMessage = youTubeInitializationResult.toString();
                Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show();
            }
        });

在我的布局中

<FrameLayout
    android:id="@+id/MainVideo"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:layout_weight="1"/>

1 个答案:

答案 0 :(得分:0)

而不是'FrameLayout'使用

<fragment
    class="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:id="@+id/youtube_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

并尝试