title: Is there a way to set adapter position of view holder of recycler view programmatically?
body: <p>Just like a view holder has the method getAdapterPosition(), I want to write my own setAdapterPosition() for the view holder(which doesn't exist).</p>
  
  <p><strong>What am I working on?</strong></p>
  
  <p>Currently, I am working on a project wherein I have to list 10 songs in the form of a recycler view and then play them, if any particular song is clicked. Each list item contains the song name, a seek bar and a play button</p>
  
  <p>I have implemented this part, by adding all the media player contros inside the adapter class.</p>
  
  <p><strong>What do I want?</strong> </p>
  
  <p>Currently, this model used mediaPlayer.setLooping(true) so that as soon as a song finished, it kept looping till some other song is clicked for playing. </p>
  
  <p>Now I want to extend this model to an "autoplay" version so that as soon as a song is finished, the next song is played</p>
  
  <p><strong>What is the problem?</strong></p>
  
  <p>The seekBar progress and playButton image transition to a pauseButton image were happening with the call</p>
  
  <pre><code>viewHolder.seekBar.setProgress(...)
  
  viewHolder.playButton.setImageDrawable(...)
  </code></pre>
  
  <p>Since this was happening on the</p>
  
  <pre><code>viewHolder.itemView.setOnClickListener(...)
  </code></pre>
  
  <p>as soon as a new song was clicked, the seekBar and playButton image were being updated.</p>
  
  <p>Now, when the song is completed, I don't want the user to click on the next song to play it. I want it to be autoplayed, but I am unable to understand, how do I change myViewHolder position accordingly.</p>
  
  <p>Suppose Song 0 has finished playing and Song 1 has started playing, viewHolder.getAdapterPosition() still returns 0 instead of 1. Hence the seekBar &amp; playButton of song 0 is being updated and not of song 1.</p>
  
  <p>I want to be able to do something like this</p>
  
  <pre><code>mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                      @Override
                      public void onCompletion(MediaPlayer mp) {
  
                          int nextSongPosition, currentSongPosition;
                          currentSongPosition = viewHolder.getAdapterPosition();
                          if(currentSongPosition==(songs.size()-1)){
                              nextSongPosition = 0;
                              //do some miracle so that viewHolder.getAdapterPosition() now shows 0 instead of currentSongPosition
                          }else{
                              nextSongPosition = currentSongPosition + 1;
                              //do some miracle so that viewHolder.getAdapterPosition() now shows nextSongPosition instead of the currentSongPosition;
                          }
                      }
                  });
  </code></pre>
  
  <p>As clear from the question statement, I want to implement something similar to a setAdapterPosition() which doesn't exist.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p><strong>onBindViewHolder Code:</strong></p>
  
  <pre><code>@Override
      public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
          Song song = songs.get(i);
          viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View v) {
                  audioManager = (AudioManager) Objects.requireNonNull(context).getSystemService(Context.AUDIO_SERVICE);
                  if(mediaPlayer.isPlaying()){
                      pauseMediaPlayer(viewHolder);
                  }else{
                      StorageReference storageRef = FirebaseStorage.getInstance().getReference("Songs");
                      StorageReference dateRef = storageRef.child(song.getStoredAs());
                      dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener&lt;Uri&gt;() {
                          @Override
                          public void onSuccess(Uri uri) {
                              try {
                                  mediaPlayer.setDataSource(uri.toString());
                                  mediaPlayer.prepare();
  
                                  viewHolder.seekBar.setMax(mediaPlayer.getDuration() / 1000);
                                  mSeekbarUpdateHandler = new Handler();
                                  mUpdateSeekbar = new Runnable() {
                                      @Override
                                      public void run() {
                                          if (viewHolder.seekBar != null &amp;&amp; mediaPlayer != null) {
                                              try{
                                                  viewHolder.seekBar.setProgress(mediaPlayer.getCurrentPosition() / 1000);
                                                  mSeekbarUpdateHandler.postDelayed(this, 50);
                                              } catch(IllegalStateException e){
                                                  Log.i("Exception",e.toString());
                                              }
  
                                          }
                                      }
                                  };
  
  
                                  viewHolder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                                      @Override
                                      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                                          if (fromUser &amp;&amp; mediaPlayer != null)
                                              mediaPlayer.seekTo( progress * 1000);
                                      }
  
                                      @Override
                                      public void onStartTrackingTouch(SeekBar seekBar) {
  
                                      }
  
                                      @Override
                                      public void onStopTrackingTouch(SeekBar seekBar) {
  
                                      }
                                  });
  
                                  startMediaPlayer(viewHolder);
  
                              } catch (IllegalArgumentException e) {
                                  e.printStackTrace();
                              } catch (SecurityException e) {
                                  e.printStackTrace();
                              } catch (IllegalStateException e) {
                                  e.printStackTrace();
                              } catch (IOException e) {
                                  e.printStackTrace();
                              }
                          }
                      }).addOnFailureListener(new OnFailureListener() {
                          @Override
                          public void onFailure(@NonNull Exception exception) {
                              // Handle any errors
                              Log.i("Song Loading Error",  exception.toString());
                          }
                      });
                  }
  
              }
  
  
          });
          mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
              @Override
              public void onCompletion(MediaPlayer mp) {
  
                  int nextSongPosition, currentSongPosition;
                  currentSongPosition = viewHolder.getAdapterPosition();
                  if(currentSongPosition==(songs.size()-1)){
                      nextSongPosition = 0;
                      //do some miracle so that viewHolder.getAdapterPosition() now shows 0 instead of currentSongPosition
                  }else{
                      nextSongPosition = currentSongPosition + 1;
                      //do some miracle so that viewHolder.getAdapterPosition() now shows nextSongPosition instead of the currentSongPosition;
                  }
              }
          });
      }
  </code></pre>
  
  <p><strong>pauseMediaPlayer(..) &amp; startMediaPlayer(..) Code:</strong></p>
  
  <pre><code>private void pauseMediaPlayer(ViewHolder viewHolder) {
          int result = audioManager.requestAudioFocus(this, AudioManager.USE_DEFAULT_STREAM_TYPE, AudioManager.AUDIOFOCUS_GAIN);
          if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
              isSongPlaying = false;
              viewHolder.playButton.setImageResource(R.drawable.play_button);
              if (mediaPlayer != null) {
                  mediaPlayer.pause();
              }
          }
      }
  
      private void startMediaPlayer(ViewHolder viewHolder) {
          int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
          if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
              isSongPlaying = true;
              viewHolder.playButton.setImageResource(R.drawable.pause_button);
              if (mediaPlayer != null) {
                  //mediaPlayer.setLooping(true);
                  mediaPlayer.start();
              }
              mSeekbarUpdateHandler.postDelayed(mUpdateSeekbar, 0);
          }
      }
  </code></pre>