0

I have an activity which has 2 or 3 fragments in one of those fragments i want to show Youtube video inside a recycler view what i have tried is

1.

when i try to access YouTubePlayerFragment which is in recyclerview item layout i got this error

"Type parameter T has incompatible upper bounds: View YouTubePlayerFragment"

public static class MyVideoViewHolder extends RecyclerView.ViewHolder{

    YouTubePlayerFragment youTubePlayerFragment;
    public MyVideoViewHolder(View itemView) {
        super(itemView);

        youTubePlayerFragment=itemView.findViewById(R.id.youtube_player_fragment);

    }
}

i can't access fragment with findViewById and not able to get context here i don't how to call findFragmentById here if it will work.

2.

another solution i tried is using a frameLayout inside recyclerview item layout and then replace the fragment inside that framelayout but that is also not working i am getting

"No view found for id 0x7f070096 for fragment YouTubePlayerFragment"


 public void onBindViewHolder(@NonNull MyVideoViewHolder holder, final int position) {
    YouTubePlayerFragment youTubePlayerFragment=YouTubePlayerFragment.newInstance();
    ((Activity)mContext).getFragmentManager().beginTransaction().replace(holder.youtubePlayerFrameLayout.getId(),youTubePlayerFragment).commit();
   youTubePlayerFragment.initialize(YoutubePlayerConfig.API_KEY, new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.cueVideo(videoListDataArrayList.get(position));
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
            Log.d(TAG,"initilization fail");

        }
    });
Arthveda
  • 11
  • 1
  • 3

1 Answers1

1

You can do this easily using Android-YouTube-Player.

Just create a ViewHolder containing the YouTubePlayerView and you're done.

static class ViewHolder extends RecyclerView.ViewHolder {
        private YouTubePlayerView youTubePlayerView;
        private YouTubePlayer youTubePlayer;
        private String currentVideoId;

        ViewHolder(YouTubePlayerView playerView) {
            super(playerView);
            youTubePlayerView = playerView;

            youTubePlayerView.initialize(initializedYouTubePlayer ->
                    initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
                        @Override
                        public void onReady() {
                            youTubePlayer = initializedYouTubePlayer;
                            youTubePlayer.cueVideo(currentVideoId, 0);
                        }
                    }), true
            );
        }

        void cueVideo(String videoId) {
            currentVideoId = videoId;

            if(youTubePlayer == null)
                return;

            youTubePlayer.cueVideo(videoId, 0);
        }
    }
}

The rest of the code is just your usual recycler view code.

You can see a complete example here. And you can try it in the sample app.

Pierfrancesco Soffritti
  • 1,678
  • 1
  • 18
  • 22