I would suggest that skipping and stopping the operation is caused by using too much, and more importantly, by having MULTIPLE instances of your activity. This is exactly what will happen if you copy it to each ViewHolder of your Recycler.
This is actually a very bad template and should be avoided by accident.
It is difficult to give you a solution because you did not specify how your application looks exactly (for example, these search characters are present in every ViewHolder, etc.). But, considering this to be the βworstβ case, I suggest you pass another adapter listener that will listen to your seekBar or ViewHolder (depending on you, this interface may be more for advanced control). And the Fragment will do an updated thing within itself.
class Adapter(... startPlayingListener: (SeekBar) -> Unit) { fun onBindViewHolder() { whateverButton.setOnClickListener {startPlayingListener.invoke(seekBar)} } } class Fragment(...) { fun bindAdapter() { adapter = YourAdapter(..., this::onPlayerStart) } fun onPlayerStart(seekBar: SeekBar) { this.parentActivity.runOnUiThread( { if (mMediaPlayer != null){ int mCurrentPosition = mMediaPlayer.getCurrentPosition(); seekBar.setProgress(mCurrentPosition); } mHandler.postDelayed(this, 1000); } } }
EDIT:
As you said, your solution has only a list of songs to play, and each element has its own SeekBar. In this case, I suggest you create a delegate class that will handle the music playback logic, and use ViewHolder only as a view (in other worlds, create a class for the Presenter level of your MVP). Below I posted a code that shows this idea more or less.
public interface YourPlayerView() { void updateSeekBar(int currentPosition); } public interface YourPlayerPresenter { void startPlaying(YourPlayerView view, trackID or something) void stopPlaying(YourPlayerView view, trackID or something) } class YourPlayer(...) implements YourPlayerPresenter { private MediaPlayer; @Override public void startPlaying(YourPlayerView view, trackId or something) {
source share