Android how to update value of ProgressBar?

后端 未结 4 1656
旧巷少年郎
旧巷少年郎 2021-01-12 01:46

My activity have a ProgressBar. When start activity, I\'ll check value get from another place and update to ProgressBar. Here\'s my code:

final ProgressBar p         


        
4条回答
  •  星月不相逢
    2021-01-12 02:17

    You can auto-advance progress bar using RXJava.

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (autoProgress) {
            autoProgressSubscriber = Observable.interval(0, 1000/60, TimeUnit.MILLISECONDS) //~60fps
                        .map(index -> calculateAutoProgress())
                        .subscribe(progress -> {
                            if (progressBar != null) {
                                progressBar.setProgress(progress);
                            }
                        });
        }
    }
    
    @Override
    protected void onDetachedFromWindow() {
        if (autoProgress) {
            autoProgressSubscriber.unsubscribe();
        }
        super.onDetachedFromWindow();
    }
    

提交回复
热议问题