android progressBar does not update progress view/drawable

后端 未结 15 2271
悲哀的现实
悲哀的现实 2020-11-28 09:11

two Bars which shows the progress of a game. If the user get points or time is up etc the progressBars should be updated:

private TextView tv;
private Progre         


        
相关标签:
15条回答
  • 2020-11-28 10:06

    if you love full progressbar

    time_prg_bar.setMax(100);
    time_prg_bar.setProgress(0); <--
    

    if your progressbar empty

    time_prg_bar.setMax(100);
    time_prg_bar.setProgress(100); <--
    
    0 讨论(0)
  • 2020-11-28 10:10

    You can use a handler to update progressbar

    Handler progressBarHandler = new Handler();
    
    ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);;
    
    progressBarHandler .post(new Runnable() {
    
          public void run() {
              bar.setProgress(progress);
          }
    });
    
    0 讨论(0)
  • 2020-11-28 10:11

    SOLUTION: It's a Bug in ProgressBar!

    finally... I think I found the solution...

    this does not work as one would expect:

    bar.setMax(50);
    bar.setProgress(20);
    bar.setMax(20);
    bar.setProgress(20);
    

    The setProgress(...) seems to not trigger the update on the drawable if the same value is passed again. But it's not triggered during the setMax, too. So the update is missing. Seems like a Bug in the android ProgressBar! This took me about 8 hours now.. lol :D

    To solve this, I'm just doing a bar.setProgress(0) before each update... this is only a workaround, but it works for me as expected:

    bar.setMax(50);
    bar.setProgress(20);
    bar.setProgress(0); // <--
    bar.setMax(20);
    bar.setProgress(20);
    
    0 讨论(0)
  • 2020-11-28 10:11

    I was able to get this to work with View.post():

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        // ...
    
        mSeekBar.post(new Runnable() {
            @Override
            public void run() {
                mSeekBar.setProgress(percentOfFullVolume);
            }
        });
    }
    

    As with SKTs answer it is unclear to me why this should work when this does not

    mSeekBar.setProgress(percentOfFullVolume);
    

    However, it seems to be true up to and including Android Lollipop.

    0 讨论(0)
  • 2020-11-28 10:11

    I tried all solutions listed, they did not work. Turns out... the real problem was my code was busy running, essentially blocking the UI thread. So the proper solution which instantly fixed the problem was... put my busy code inside an async task.

    In my case I was loading several thousand record from XML into a SQLite database. Naturally I wanted to update the progress bar. Code that should have done this ( prg_bar.setProgress(iProgressPercentage) ) did nothing, and at the end of the load, the UI finally got a chance to run. Poof, progress bar goes from zero to 100.

    Same code put into the onProgressUpdate() of the async task, wrapped around the same functional code, worked perfectly. This makes me very skeptical of the idea "that there's a bug in the progress bar code" and that setting the max and to zero or any of those things were ever more than a coincidental work around.

    0 讨论(0)
  • 2020-11-28 10:12

    Make sure style for the progressBar is set to be style="@android:style/Widget.ProgressBar.Horizontal" otherwise it will show Indeterminate Progress

    (Just in case it will help someone)

    0 讨论(0)
提交回复
热议问题