android progressBar does not update progress view/drawable

后端 未结 15 2326
悲哀的现实
悲哀的现实 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:04

    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.

提交回复
热议问题