android studio java mapping function for multiple IDs

206 Views Asked by At

I'm a beginner in android development with java and I have some code that inserts and plays a youtube video in a fragment .

private YouTubePlayerView youTubePlayerView;

    public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {


    binding = FragmentSlideshowBinding.inflate(inflater, container, false);
    View root = binding.getRoot();

     youTubePlayerView = root.findViewById(R.id.video1);
     getLifecycle().addObserver(youTubePlayerView);

     youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
         @Override
         public void onReady(@NotNull YouTubePlayer youTubePlayer) {
             super.onReady(youTubePlayer);

             String videoID = "yyduqrCpKGg";
             youTubePlayer.cueVideo(videoID,0);
         }
     });

    return root;
}

With my code above I can create and play a video for a single url. How can I perform the code above for multiple IDS with urls and create multiple videos instead of manually doing it?

My YoutubeVideo.xml for one single video

 <com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
            android:id="@+id/video1"
            android:layout_width="match_parent"
            android:layout_height="216dp"
            app:autoPlay="false"
            app:showFullScreenButton="true" />
2

There are 2 best solutions below

0
On

The pierfrancescosoffritti youtube view acts just like a normal view .So you can create a recycler view and then pass a list of URL to the recycler view.

0
On

If you want to display multiple video with different IDs then simply use recyclerview . For more details check below link for recycler view implementation. https://developer.android.com/guide/topics/ui/layout/recyclerview