title: Exoplayer with Recyclerview
body: <p>I have a video feed (similar to TikTok) but when I swipe up the new video plays but the old one keeps playing. I've managed to get a page listener but I cannot control the ExoPlayer from the &quot;Home Fragment&quot;.</p>
  <p>Any idea on how to trigger something inside the RecyclerView Adapter from the Home Fragment?</p>
  <p>If there's a better solution for doing this, please tell me.</p>
  <p><strong>Home Fragment</strong></p>
  <pre><code>public class HomeFragment extends Fragment {
  
      private View view;
      RecyclerView recyclerView;
      List&lt;Model&gt; models = new ArrayList&lt;&gt;();
      MainRecylerAdapter adapter;
  
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          view = inflater.inflate(R.layout.fragment_home, container, false);
  
  
          recyclerView = view.findViewById(R.id.recyclerview);
  
          models.add(new Model(1, &quot;https://www.newslistener.com/Mukesh/1.mp4&quot;, &quot;one&quot;));
          models.add(new Model(2, &quot;https://www.newslistener.com/Mukesh/2.mp4&quot;, &quot;one&quot;));
          models.add(new Model(3, &quot;https://www.newslistener.com/Mukesh/3.mp4&quot;, &quot;one&quot;));
          models.add(new Model(4, &quot;https://www.newslistener.com/Mukesh/4.mp4&quot;, &quot;one&quot;));
          models.add(new Model(5, &quot;https://www.newslistener.com/Mukesh/5.mp4&quot;, &quot;one&quot;));
          models.add(new Model(6, &quot;https://www.newslistener.com/Mukesh/6.mp4&quot;, &quot;one&quot;));
          models.add(new Model(7, &quot;https://www.newslistener.com/Mukesh/7.mp4&quot;, &quot;one&quot;));
          models.add(new Model(8, &quot;https://www.newslistener.com/Mukesh/8.mp4&quot;, &quot;one&quot;));
          models.add(new Model(9, &quot;https://www.newslistener.com/Mukesh/9.mp4&quot;, &quot;one&quot;));
          models.add(new Model(10, &quot;https://www.newslistener.com/Mukesh/10.mp4&quot;, &quot;one&quot;));
          models.add(new Model(11, &quot;https://www.newslistener.com/Mukesh/11.mp4&quot;, &quot;one&quot;));
          models.add(new Model(12, &quot;https://www.newslistener.com/Mukesh/12.mp4&quot;, &quot;one&quot;));
          models.add(new Model(13, &quot;https://www.newslistener.com/Mukesh/13.mp4&quot;, &quot;one&quot;));
          models.add(new Model(14, &quot;https://www.newslistener.com/Mukesh/14.mp4&quot;, &quot;one&quot;));
          models.add(new Model(15, &quot;https://www.newslistener.com/Mukesh/15.mp4&quot;, &quot;one&quot;));
          models.add(new Model(16, &quot;https://www.newslistener.com/Mukesh/16.mp4&quot;, &quot;one&quot;));
          models.add(new Model(17, &quot;https://www.newslistener.com/Mukesh/17.mp4&quot;, &quot;one&quot;));
          models.add(new Model(18, &quot;https://www.newslistener.com/Mukesh/18.mp4&quot;, &quot;one&quot;));
          models.add(new Model(19, &quot;https://www.newslistener.com/Mukesh/19.mp4&quot;, &quot;one&quot;));
          models.add(new Model(20, &quot;https://www.newslistener.com/Mukesh/20.mp4&quot;, &quot;one&quot;));
          models.add(new Model(21, &quot;https://www.newslistener.com/Mukesh/21.mp4&quot;, &quot;one&quot;));
          models.add(new Model(22, &quot;https://www.newslistener.com/Mukesh/22.mp4&quot;, &quot;one&quot;));
          models.add(new Model(23, &quot;https://www.newslistener.com/Mukesh/23.mp4&quot;, &quot;one&quot;));
          models.add(new Model(24, &quot;https://www.newslistener.com/Mukesh/24.mp4&quot;, &quot;one&quot;));
          models.add(new Model(25, &quot;https://www.newslistener.com/Mukesh/25.mp4&quot;, &quot;one&quot;));
          models.add(new Model(26, &quot;https://www.newslistener.com/Mukesh/26.mp4&quot;, &quot;one&quot;));
          models.add(new Model(27, &quot;https://www.newslistener.com/Mukesh/27.mp4&quot;, &quot;one&quot;));
          models.add(new Model(28, &quot;https://www.newslistener.com/Mukesh/28.mp4&quot;, &quot;one&quot;));
          models.add(new Model(29, &quot;https://www.newslistener.com/Mukesh/29.mp4&quot;, &quot;one&quot;));
          models.add(new Model(30, &quot;https://www.newslistener.com/Mukesh/30.mp4&quot;, &quot;one&quot;));
          models.add(new Model(31, &quot;https://www.newslistener.com/Mukesh/31.mp4&quot;, &quot;one&quot;));
          models.add(new Model(32, &quot;https://www.newslistener.com/Mukesh/32.mp4&quot;, &quot;one&quot;));
          models.add(new Model(33, &quot;https://www.newslistener.com/Mukesh/33.mp4&quot;, &quot;one&quot;));
          models.add(new Model(34, &quot;https://www.newslistener.com/Mukesh/34.mp4&quot;, &quot;one&quot;));
          models.add(new Model(35, &quot;https://www.newslistener.com/Mukesh/35.mp4&quot;, &quot;one&quot;));
  
          SnapHelper snapHelper = new PagerSnapHelper();
          snapHelper.attachToRecyclerView(recyclerView);
          LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
          recyclerView.setLayoutManager(layoutManager);
          adapter = new MainRecylerAdapter(models, getActivity());
          recyclerView.setAdapter(adapter);
  
  
          return view;
      }
  }
  </code></pre>
  <p><strong>Adapter</strong></p>
  <pre><code>public class MainRecylerAdapter extends RecyclerView.Adapter&lt;MainRecylerAdapter.ViewHolder&gt; {
      List&lt;Model&gt; models = new ArrayList&lt;&gt;();
      Context context;
      private int index = 0;
  
      public MainRecylerAdapter(List&lt;Model&gt; models, Context context) {
          this.models = models;
          this.context = context;
      }
  
      @NonNull
      @Override
      public MainRecylerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_recyler_layout, parent, false);
          return new ViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull final MainRecylerAdapter.ViewHolder holder, final int position) {
  
          if (holder.isPlaying()) {
              Log.e(&quot;TAG1&quot;, &quot;play&quot;);
              holder.releasePlayer();
              holder.intiPlayer(models.get(position).getUrl());
          } else {
              Log.e(&quot;TAG1&quot;, &quot;empty&quot;);
              holder.intiPlayer(models.get(position).getUrl());
          }
  
      }
  
      @Override
      public int getItemCount() {
          return models.size();
      }
      
  
      public class ViewHolder extends RecyclerView.ViewHolder {
  
          SimpleExoPlayerView exoPlayerView;
          SimpleExoPlayer exoPlayer;
          private long playbackPosition;
          private int currentWindow;
          private boolean playWhenReady;
          ProgressBar progressBar;
  
          public ViewHolder(@NonNull View itemView) {
              super(itemView);
              exoPlayerView = itemView.findViewById(R.id.exoplayerview);
              progressBar  = itemView.findViewById(R.id.progress);
  
          }
  
          private void intiPlayer(String url) {
                  try {
                      BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
                      TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
                      exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
                      Uri videoURI = Uri.parse(url);
                      DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory(&quot;exoplayer_video&quot;);
                      ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
                      MediaSource mediaSource = new ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null);
                      exoPlayerView.setPlayer(exoPlayer);
                      exoPlayer.prepare(mediaSource);
                      exoPlayer.setPlayWhenReady(true);
                  } catch (Exception e) {
                      Log.e(&quot;MainAcvtivity&quot;, &quot; exoplayer error &quot; + e.toString());
                  }
  
  
          }
  
          private boolean isPlaying() {
              return exoPlayer != null
                      &amp;&amp; exoPlayer.getPlaybackState() != Player.STATE_ENDED
                      &amp;&amp; exoPlayer.getPlaybackState() != Player.STATE_IDLE
                      &amp;&amp; exoPlayer.getPlayWhenReady();
          }
  
          private void releasePlayer() {
              if (exoPlayer != null) {
                  playbackPosition = exoPlayer.getCurrentPosition();
                  currentWindow = exoPlayer.getCurrentWindowIndex();
                  playWhenReady = exoPlayer.getPlayWhenReady();
                  exoPlayer.release();
                  exoPlayer = null;
              }
          }
  
  
      }
  </code></pre>