从滞后的 AsyncTask 更新 ProgressBar UI

Updating ProgressBar from AsyncTask lagging UI

我有一个 Fragment 和一个 RecyclerViewRecyclerViewViewHolder 拿着一个 ProgressBar 来显示下载过程的进度.

public static class ViewHolder extends RecyclerView.ViewHolder {
    ....
    private ProgressBar progressBar = null;
    ....
    public ViewHolder(View itemView) {
        super(itemView);

        ....
        this.progressBar = (ProgressBar) itemView.findViewById(R.id.progessbar);
        ....
    }
}

我为 Fragment 创建了一个回调:

public interface CallbackItemChanged {
    void onItemChanged(final int position);
}

如果它叫我:

@Override
public void onItemChanged(final int position) {
    this.adapter.notifyItemChanged(position);
}

在我的 AsyncTask 中:

protected void onProgressUpdate(Integer... progress) {
    super.onProgressUpdate(progress);

    this.entry.setProgress(progress[0]);
    this.entry.setProgressString(progress[0] + "%");

    this.callbackItemChanged.onItemChanged(this.entry.getPosition());
}

进度已成功发布,但 ui 却非常滞后,但我不知道为什么? onProgressUpdate 是 ui 线程上的 运行 不是吗?我觉得应该是这样还是我错了?

如何在更新进度条时让 ui 顺利运行?

编辑

@Override
protected String doInBackground(Void... params) {
    this.inputStream = null;
    this.outputStream = null;
    this.connection = null;

    File file = new File(this.entry.getPath(this.context));
    File parent = file.getParentFile();

    try {
        parent.mkdirs();
        file.createNewFile();

        this.connection = (HttpsURLConnection) new URL(this.entry.getUrl()).openConnection();
        this.connection.connect();

        int fileLength = this.connection.getContentLength();

        this.inputStream = this.connection.getInputStream();
        this.outputStream = new FileOutputStream(file);

        byte data[] = new byte[4096];
        long progress = 0;
        int count;

        while ((count = this.inputStream.read(data)) != -1) {
            if (this.isCancelled() || this.entry.isCancelled()) {
                this.handleClose();
                this.handleDelete();

                return null;
            }

            if (fileLength > 0) {
                this.publishProgress((int) ((progress +=count) * 100 / fileLength));
            }

            this.outputStream.write(data, 0, count);
        }
    } catch (Exception e) {
        this.handleDelete();

        return e.toString();
    } finally {
        this.handleClose();
    }

    return null;
}

您在循环内发布进度,因此您的主线程将被调用很多次。

您可以通过简单的 Thread.sleep():

延迟发布进度
    while ((count = this.inputStream.read(data)) != -1) {
        if (this.isCancelled() || this.entry.isCancelled()) {
            this.handleClose();
            this.handleDelete();

            return null;
        }

        // Write the data before publishing the progress
        this.outputStream.write(data, 0, count);

        try{
            // Adjust this value. It shouldn't be too small.
            Thread.sleep(100);
        }catch (InterruptedException e){
            // Nothing you can do here
        }finally {
            if (fileLength > 0) {
                this.publishProgress((int) ((progress +=count) * 100 / fileLength));
            }
        }
    }

或者您只能以 x% 的增量发布:

while ((count = this.inputStream.read(data)) != -1) {
    if (this.isCancelled() || this.entry.isCancelled()) {
        this.handleClose();
        this.handleDelete();

        return null;
    }

    // Write the data before publishing the progress
    this.outputStream.write(data, 0, count);

    if (fileLength > 0) {
        currentProgress = ((progress += count) * 100 / fileLength);
        // Publish only on increments of 1%
        if (currentProgress >= previousProgress + 1) {
            this.publishProgress(currentProgress);
            previousProgress = currentProgress;
        }

    }
}