从AsyncTask滞后UI更新ProgressBar

时间:2016-05-28 11:18:52

标签: android android-asynctask android-progressbar

我有Fragment RecyclerViewRecyclerView' s ViewHolder持有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;
}

1 个答案:

答案 0 :(得分:1)

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

您可以使用简单的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;
        }

    }
}