The following method code is available:

private int secs = 0; private void onPlayClick(){ Runnable runnable = new Runnable() { @Override public void run() { double percent = (double) secs/SONG_LENGTH; int progress =(int) (percent*100); seekBar.setProgress(progress); secs++; } }; for (int i = seekBar.getProgress()*SONG_LENGTH/100; i<=SONG_LENGTH;i++) { handler.postDelayed(runnable,1000*i); } } 

His task is to gradually change the position of the SeekBar slider. However, for some reason, instead of gradually moving, it instantly moves to the end of the SeekBar . What is the problem?

  • it is better not to rewrite the question, but to add data so that you can trace the logic of the question itself. - Jarvis_J

1 answer 1

In fact, you have written: "post the result after 1 second X times."

That is, all your code is resolved in one moment (after 1 second). Enter a multiplier for 1000 in handler.postDelayed() , depending on the iteration of the loop, and everything should work.

upd: It’s better to do something like this:

 secs = 0; //глобальная переменная Runnable runnable = new Runnable() { @Override public void run() { seekBar.setProgress(secs); secs++; } }; for(int i=0;i<SONG_LENGTH/100;i++) new Handler().postDelayed(runnable,1000*i); 
  • I do not quite understand. PostDelayed () also pauses the stream for (in this case) 1000 ms at each iteration, is it not? - Sergei Mikhailovskii
  • no, it does not pause the stream. It only postpones the "post" for 1 second. According to your code - X times. - Jarvis_J
  • In general, it is better to take out the cycle from the stream and register it in postDelayed - Jarvis_J at postDelayed pm
  • I understood this moment, but what kind of multiplier are we talking about? I tried for example to multiply secs - did not help - Sergei Mikhailovskii
  • @SergeiMikhailovskii, look, updated the answer. - Jarvis_J 2:21 pm