Android Progressbar无法更新 [英] Android Progressbar not updating

查看:303
本文介绍了Android Progressbar无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新进度栏时遇到问题.我正在更新单独的线程中的进度条,并且进度条进度所依赖的变量(这是一个类变量)正在另一个线程中更新.因此,进度对话框会显示,但始终0%不会更新进度.请帮帮我.

i got a problem in updating progress bar. I am updating progress bar in separate Thread and the variable on which the progressbar progress is depending(which is a class variable) updating in another thread. So, the progress dialog shows but always 0% not updating it progress. Help me please.

public void setProgressbar()
{
    progressBar = new ProgressDialog(context);
    progressBar.setCancelable(true);
    progressBar.setMessage("File downloading ...");
    progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressBar.setProgress(0);
    progressBar.setMax(100);
    progressBar.show();

    Thread thread = new Thread()
    {
        public void run()
        {


             while(progressBar.getProgress() < 100)
             {
                Log.v("progressbar", getProgress()+"");
                 progressBar.setProgress(getProgress());
                 try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
             }
        }
    };
    thread.start();

更新值代码

  Thread thread = new Thread()
   {
        public void run()
      {
      .
      .
      .
 while((bytesRead = is.read(bytearray, 0, bytearray.length)) != -1)
                    {
                     bos.write(bytearray, 0, bytesRead);
                     totaldownload = totaldownload + bytesRead;
                     Log.v("downloadign ", totaldownload+"");
                    // progressBar.setProgress((int) ((totaldownload/sizeoffile) * 100));
                    }
                    bos.close();
.
.
.
};
thread.start()

和getPrgoress方法

And getPrgoress method

 public int getProgress()
    {
        return (int) ((totaldownload/sizeoffile) * 100);
    }

推荐答案

这是ProgressBar中的错误!

如果再次传递相同的值,则setProgress(...)似乎不会触发drawable上的更新.但是它也不会在setMax期间触发.因此缺少更新.

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.

要解决此问题,我只是在每次更新前先执行bar.setProgress(0) ...这只是一种解决方法,但它对我来说确实如预期:

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.setProgress(0); // call these two methods before setting progress.
bar.setMax(20);
bar.setProgress(20);

第二个选项.

mSeekBar.post(new Runnable() {
        @Override
        public void run() {
            mSeekBar.setProgress(percentOfFullVolume);
        }
    });

它也可能对某人有用.

这篇关于Android Progressbar无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆