【发布时间】:2016-03-30 22:33:39
【问题描述】:
我有一个项目列表视图 (ItemSong) ,单击时开始下载(使用 Asynctask)。我有一个显示下载进度的文本视图。
ItemSong 有一个用于跟踪下载进度的 int 属性 (percentCompleted)。 DownloadTask 更新这个 int。这样,当我滚动回该项目时,就会显示进度。但即使 DownloadTask 正在运行并且percentCompleted 正在更新,它也不会持续更新。
一切正常,但是当我向下和向后滚动时,即项目被回收时,进度停止更新。只有当我滚动并返回项目时,它才会显示最后更新的值。
我已将代码修剪为必要的部分,但如果有不清楚的地方,我会添加更多或解释。
任何关于我能做的建议,即使我需要改变整个方法,我们也很感激。谢谢。
public class ItemSong implements Item{
public final String track_name;
public final String album_name;
public final String track_num;
public final String album_id;
public final String album_link;
private int percentCompleted = 0;
private boolean activeDownload; //false by default
public ItemSong(String track_name, String album_name, String track_num, String album_id, String album_link) {
this.track_name = track_name;
this.album_name = album_name;
this.track_num = track_num;
this.album_id = album_id;
this.album_link = album_link;
}
@Override
public boolean isSection() {
return false;
}
public String itemType() {
return "song";
}
public void setCompleted(int percent){ percentCompleted = percent; }
public int getCompleted() {return percentCompleted;}
public void setDownloadStatus(boolean status) {activeDownload = status;}
public boolean downloadStatus() {return activeDownload;}
}
我的 getView() 在 Adapter 类中: 我设置了一个更新 textview 的标志是下载处于活动状态。但是,这会导致应用冻结。
TextView percentCompleted = (TextView)v.findViewById(R.id.completed);
while (itemSong.downloadStatus() == true)
{
percentCompleted.setText(Integer.toString(itemSong.getCompleted() ) );}
我的 MainActivity 中的 onItemClick
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
ItemSong itemSong = (ItemSong) items.get(position);
TextView percentCompleted = (TextView) arg1.findViewById(R.id.completed);
new DownloadTask(percentCompleted, itemSong).execute();
}
DownloadTask 类:
class DownloadTask extends AsyncTask<String, String, String> {
private TextView percentComplete;
private int prog=0;
private ItemSong itemSong;
DownloadTask(TextView percentComplete, ItemSong itemSong){
this.percentComplete = percentComplete;
this.itemSong = itemSong;
}
protected String doInBackground(String... f_url) {
itemSong.setDownloadStatus(true);
while (prog <= 100) {
itemSong.setCompleted(prog);
publishProgress("" + (prog));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
prog = prog+10;
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
// setting progress percentage
percentComplete.setText(progress[0].toString());
}
@Override
protected void onPostExecute(String file_url) {
itemSong.setDownloadStatus(false);
}
}
【问题讨论】:
-
你应该实现一个 hashMap
来存储进度项位置和进度值,并在列表适配器的 getView 方法中设置回收后的真实值 -
进度更新后你有没有调用 notifydatasetchanged()?
-
您可以使用 notifydataSetChanged() 但这会非常频繁地回收您的视图。您可以通过不在列表项中显示百分比值来避免这种情况。而是显示“正在下载....”。与显示百分比值相比,这将使您回收视图的次数非常少。
-
我遇到了一些像你一样的问题,但我添加了一个哈希图,在更新名为 notifydatasetChanged() 的值后,它解决了我的问题。我认为它不会导致问题,除了重新渲染列表视图的一些工作
-
设置textview,s text后必须在“onprogressupdate”主线程中调用
标签: android listview android-asynctask android-recyclerview