title: How to implement exoplayer with recyclerview and viewpager2?
body: <p>I am creating a video feed and was using the VideoView to preview video in the viewpager2. But because it was slow, I tried to shift to exoplayer. Everything works kind of okay but the problem is that every video in the adapter loads at once and when I swipe from the viewpager, the video does not pause. Here is the code for the adapter:</p>
  <pre><code>public class VideosAdapter extends RecyclerView.Adapter&lt;VideosAdapter.VideoViewHolder&gt; {
  
      private List&lt;VideoItem&gt; videoItems;
  
      public VideosAdapter(List&lt;VideoItem&gt; videoItems) {
          this.videoItems = videoItems;
      }
  
      @NonNull
      @Override
      public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          return new VideoViewHolder(
                 LayoutInflater.from(parent.getContext()).inflate(
                         R.layout.item_container_video,
                         parent,
                         false
                 )
          );
      }
  
  
  
      @Override
      public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        holder.setVideoData(videoItems.get(position));
      }
  
      
  
      @Override
      public int getItemCount() {
          return videoItems.size();
      }
  
  
      static class VideoViewHolder extends RecyclerView.ViewHolder {
  
          PlayerView playerView;
          TextView video__description;
          ProgressBar videoProgressBar;
          SimpleExoPlayer simpleExoPlayer;
          LoopingMediaSource loopingMediaSource;
          DefaultHttpDataSourceFactory factory;
          ExtractorsFactory extractorsFactory;
          BandwidthMeter bandwidthMeter;
          TrackSelector trackSelector;
  
          public VideoViewHolder(@NonNull View itemView) {
              super(itemView);
              playerView = itemView.findViewById(R.id.player__view);
              video__description = itemView.findViewById(R.id.video__description);
              videoProgressBar = itemView.findViewById(R.id.videoLoader);
              factory = new DefaultHttpDataSourceFactory(&quot;exoplayer_video&quot;);
              extractorsFactory = new DefaultExtractorsFactory();
              bandwidthMeter = new DefaultBandwidthMeter();
              trackSelector = new DefaultTrackSelector(
                      new AdaptiveTrackSelection.Factory(bandwidthMeter)
              );
          }
  
          void setVideoData(VideoItem videoItem) {
              Uri videoUrl =Uri.parse(videoItem.videoURL);
              simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(videoItem.context, trackSelector);
              MediaSource mediaSource = new ExtractorMediaSource(videoUrl,factory,extractorsFactory,null,null);
              loopingMediaSource = new LoopingMediaSource(mediaSource);
              playerView.setPlayer(simpleExoPlayer);
              playerView.setKeepScreenOn(true);
              simpleExoPlayer.prepare(loopingMediaSource);
              simpleExoPlayer.setPlayWhenReady(true);
              video__description.setText(videoItem.videoDescription);
              simpleExoPlayer.addListener(new Player.DefaultEventListener() {
                  @Override
                  public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                      super.onPlayerStateChanged(playWhenReady, playbackState);
                      if(playbackState==Player.STATE_BUFFERING) {
                          videoProgressBar.setVisibility(View.VISIBLE);
                      } else if(playbackState==Player.STATE_READY) {
                          videoProgressBar.setVisibility(View.GONE);
                      }
                  }
  
                  @Override
                  public void onPlayerError(ExoPlaybackException error) {
                      super.onPlayerError(error);
                  }
              });
          }
      }
  }
  
  
  </code></pre>
  <p>Here is the code for the videoItems Class:</p>
  <pre><code>public class VideoItem {
      public String videoURL, videoDescription;
      public Context context;
  
      public VideoItem(String videoURL, String videoDescription, Context context) {
          this.videoURL = videoURL;
          this.videoDescription = videoDescription;
          this.context = context;
      }
  }
  </code></pre>
  <p>And here is the code for setting the adapter:</p>
  <pre><code>public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          // Inflate the layout for this fragment
          View root = inflater.inflate(R.layout.fragment_first, container, false);
  
  
          final ViewPager2 videosViewPager = root.findViewById(R.id.videosViewPager);
  
              List&lt;VideoItem&gt; videoItems = new ArrayList&lt;&gt;();
  
  VideoItem videoItemFirst = new VideoItem(&quot;https://www.infinityandroid.com/videos/video1.mp4&quot;,&quot;Hello World&quot;, requireActivity().getApplicationContext());
              videoItems.add(videoItemFirst);
  
              VideoItem videoItemSecond = new VideoItem(&quot;https://www.infinityandroid.com/videos/video2.mp4&quot;,&quot;Hello World&quot;, requireActivity().getApplicationContext());
              videoItems.add(videoItemSecond);
  
              VideoItem videoItemThird = new VideoItem(&quot;https://www.infinityandroid.com/videos/video3.mp4&quot;,&quot;Hello World&quot;, requireActivity().getApplicationContext());
              videoItems.add(videoItemThird);
  
  videosViewPager.setAdapter(new VideosAdapter(videoItems));
  
  
  return root;
  }
  
  </code></pre>
  <p>Here is the layout file:</p>
  <pre><code>
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      tools:context=&quot;.fragments.firstFragment&quot;
      &gt;
  
      &lt;androidx.viewpager2.widget.ViewPager2
          android:id=&quot;@+id/videosViewPager&quot;
          android:orientation=&quot;horizontal&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:layout_constraintBottom_toTopOf=&quot;parent&quot;
          app:layout_constraintEnd_toStartOf=&quot;parent&quot;
           /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
  </code></pre>
  <p>It seems to be loading all the videos at once when the fragment is loaded and when I swipe to get to the next video, the previous video does not get paused. How to fix this? Is there a better method? Hope you will answer.</p>