使用Xutils 框架 下载功能进度条无法更新

11 篇文章 0 订阅

  在下载列表页面DownloadListActivity可以看到下载进度的变化,但是我使用对话方式的显示下载进度的时候,进度没有发生变化。经过一步一步的调试最后找到了原因。

DownloadItemViewHolder类里面的

//刷新

 public void refresh() {
     
            label.setText(downloadInfo.getFileName());
            state.setText(downloadInfo.getState().toString());
            if (downloadInfo.getFileLength() > 0) {
                progressBar.setProgress((int) (downloadInfo.getProgress() * 100 / downloadInfo.getFileLength()));
            } else {
                progressBar.setProgress(0);
            }
            stopBtn.setVisibility(View.VISIBLE);
            stopBtn.setText("暂停");
            HttpHandler.State state = downloadInfo.getState();
            
            switch (state) {
                case WAITING:
                    stopBtn.setText("暂停");
                    break;
                case STARTED:
                    stopBtn.setText("暂停");
                    break;
                case LOADING:
                    stopBtn.setText("暂停");
                    break;
                case CANCELLED:
                    stopBtn.setText("继续");
                    break;
                case SUCCESS:
                    stopBtn.setVisibility(View.INVISIBLE);
                    break;
                case FAILURE:
                    stopBtn.setText("重试");
                    break;
                default:
                    break;
            }
            downloadListAdapter.notifyDataSetChanged(); // 添加这行对适配器进行刷新一次。
        }
    }

首先,我们需要定义一个模型来存储应聘的信息,比如应聘者姓名、职位、面试时间等等。假设我们定义了一个名为 `Application` 的模型,代码如下: ```python from django.db import models class Application(models.Model): name = models.CharField(max_length=100) position = models.CharField(max_length=100) interview_time = models.DateTimeField() # 其他字段省略 ``` 接下来,我们需要创建一个视图来处理页面的请求。这个视图需要从数据库中查询出所有的应聘信息,并将它们转换成一个进度条展示在页面上。我们可以使用 Django 的模板语言来渲染 HTML 页面。代码如下: ```python from django.shortcuts import render from django.utils import timezone from .models import Application def application_list(request): applications = Application.objects.all() now = timezone.now() context = { 'applications': applications, 'now': now, } return render(request, 'application_list.html', context) ``` 在上面的代码中,我们首先从数据库中查询出所有的应聘信息,并获取当前时间。然后将这些信息存储在 `context` 变量中,并将其传递给模板。最后,我们使用 `render` 函数将模板渲染成 HTML 页面并返回给浏览器。 接下来,我们需要在 HTML 模板中展示进度条。我们可以使用 Bootstrap 框架提供的进度条组件。代码如下: ```html {% extends 'base.html' %} {% block content %} <div class="container"> <h1>应聘进度</h1> {% for application in applications %} <div class="progress"> <div class="progress-bar" role="progressbar" style="width: {{ application.progress(now) }}%;" aria-valuenow="{{ application.progress(now) }}" aria-valuemin="0" aria-valuemax="100">{{ application.name }} - {{ application.position }}</div> </div> {% endfor %} </div> {% endblock %} ``` 在上面的代码中,我们首先继承了一个名为 `base.html` 的基础模板。然后,我们使用 `for` 循环遍历所有的应聘信息,并为每个应聘信息创建一个进度条进度条的宽度和当前进度通过调用 `Application` 模型中的 `progress` 方法计算得出。最后,我们将应聘者的姓名和职位显示在进度条上方。 最后,我们还需要在 `Application` 模型中定义一个 `progress` 方法来计算进度条的宽度。代码如下: ```python class Application(models.Model): # 省略其他字段 def progress(self, now): total_days = (self.interview_time - self.created_at).days passed_days = (now - self.created_at).days return passed_days / total_days * 100 ``` 在上面的代码中,我们首先计算出应聘者从创建应聘信息到面试的总天数,然后计算出从创建应聘信息到当前时间的天数。最后,我们将已经过去的天数除以总天数乘以 100,得到当前进度百分比。 完整的代码如下所示: ```python # models.py from django.db import models from django.utils import timezone class Application(models.Model): name = models.CharField(max_length=100) position = models.CharField(max_length=100) interview_time = models.DateTimeField() created_at = models.DateTimeField(default=timezone.now) def progress(self, now): total_days = (self.interview_time - self.created_at).days passed_days = (now - self.created_at).days return passed_days / total_days * 100 # views.py from django.shortcuts import render from django.utils import timezone from .models import Application def application_list(request): applications = Application.objects.all() now = timezone.now() context = { 'applications': applications, 'now': now, } return render(request, 'application_list.html', context) # application_list.html {% extends 'base.html' %} {% block content %} <div class="container"> <h1>应聘进度</h1> {% for application in applications %} <div class="progress"> <div class="progress-bar" role="progressbar" style="width: {{ application.progress(now) }}%;" aria-valuenow="{{ application.progress(now) }}" aria-valuemin="0" aria-valuemax="100">{{ application.name }} - {{ application.position }}</div> </div> {% endfor %} </div> {% endblock %} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

i_a_jun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值