android progressBar does not update progress view/

2019-01-06 15:56发布

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 ProgressBar levelHoldBar;
private ProgressBar levelUpBar;

//...
private void updateViews() {

    // ...
    levelHoldBar.setMax(currentLevel.getThreshold());
    levelHoldBar.setProgress(currentPoints > currentLevel.getThreshold() ? currentLevel.getThreshold() : currentPoints);

    levelUpBar.setMax(nextLevel.getThreshold());
    levelUpBar.setProgress(currentPoints > nextLevel.getThreshold() ? nextLevel.getThreshold() : currentPoints);

    tv.setText(currentPoints+"/"+currentLevel.getThreshold());

    Log.d(TAG, "hold prog/max "+levelHoldBar.getProgress()+"/"+levelHoldBar.getMax());
    Log.d(TAG, "up   prog/max "+levelUpBar.getProgress()+"/"+levelUpBar.getMax());
}

ie. Outputs:

12-03 17:48:33.918: DEBUG/MainActivity(6829): hold prog/max 20/20
12-03 17:48:33.918: DEBUG/MainActivity(6829): up   prog/max 20/50

The Log.d(...) in the end shows ALWAYS the correct values, but SOMETIMES the visual bars of the progressBars do not show the correct progesses. They show progresses that had been set previously even if the getters for "max" and "progress" return correct values (in the example the bar shows about 20% (instead of 100%) for the levelHoldBar and about 2% (instead of 40%) for the levelUp-bar). I cannot figure out, why the log-output is correct but the drawables are wrong!? The TextView (tv) is updated correctly! Whats going on here? How can I fix that?

14条回答
女痞
2楼-- · 2019-01-06 16:01

Nothing here worked for me, so I've came with a bit different solution. I noticed that everything is working well until I try to change maximal value (calling setMax with different value).

My code:

class FixedSeekBar : SeekBar {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    companion object {
        private const val PROGRESS_MAX = 1000000.toDouble()
    }

    private var mLastMaxValue: Int = 100

    override fun setMax(max: Int) {
        mLastMaxValue = max
        super.setMax(PROGRESS_MAX.toInt())
    }

    fun setProgressFixed(progress: Int) {
        setProgress((PROGRESS_MAX / mLastMaxValue * progress).toInt())
    }

    fun getProgressFixed(): Int {
        return (getProgress() / PROGRESS_MAX * mLastMaxValue).toInt()
    }

}

This works perfectly for me. I can call setMax as usually and I just need to replace getProgress and setProgress with getProgressFixed and setProgressFixed.

Do not override setProgress and getProgress. They are called internally.

My solution doesn't count with setMin. If you need it, change the code accordingly.

查看更多
Evening l夕情丶
3楼-- · 2019-01-06 16:02

For me worked if I set setVisibility() to visible before each setProgress()

setVisibility(View.VISIBLE);
setProgress(progress);
查看更多
该账号已被封号
4楼-- · 2019-01-06 16:03

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);
      }
});
查看更多
对你真心纯属浪费
5楼-- · 2019-01-06 16:03

Create updateThumb(); method in VerticalSeekbar

public void updateThumb(){
     onSizeChanged(getWidth(), getHeight(), 0, 0);
}

And then call update thumb method after setting progress.

seekBar.setProgress((int) progress);
seekBar.updateThumb();

its work for me in verticalseekbar calss

查看更多
孤傲高冷的网名
6楼-- · 2019-01-06 16:03

Thanks Stuck for the post stating that there is an Android bug. I also have a vertical seek bar.

The issue was when I was changing the progress drawable. The drawable would be updated but the bar would be set to 0 and be a very small size. I was able to get it to resize with updateThumb(), but the progress was still at 0.

To set the progress back at the original value I added a mylayer.setLevel(...);. I found this by reading the source code of ProgressBar.doRefreshProgress(...). You might be able to do seekBar.getProgressDrawable.setLevel(...).

LayerDrawable mylayer = //new LayerDrawable()
seekBar.setProgressDrawable(mylayer);
seekBar.updateThumb();
seekBar.setProgress((int) chLevel);
mylayer.setLevel((int) (chLevel / MAX_LEVEL * 10000));
查看更多
放我归山
7楼-- · 2019-01-06 16:04

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); <--
查看更多
登录 后发表回答