用Xutils下载,但是下载列表进度却不动,只是刷新progress有进度,但是刷新其他textView,就都不能刷新了。
如图:
全部刷新不了:
附上代码:
package com.work.driver.adapter;
import java.io.File;
import java.lang.ref.WeakReference;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.library.app.bitmap.BitmapTools;
import com.library.app.download.DownloadInfo;
import com.library.app.download.DownloadManager;
import com.library.app.instrument.Common;
import com.library.app.storage.SDCardTools;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.work.driver.R;
import com.work.driver.activity.BaseActivity;
/**
@date 2015-6-23 下午6:28:37
*/
public class DownloadManageAdapter extends BaseAdapter {
private final Context mContext;
private final LayoutInflater mInflater;
private DownloadManager downloadManager;
private BitmapTools bitmapTools;
public DownloadManageAdapter(Context context, DownloadManager downloadManager) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
this.downloadManager = downloadManager;
bitmapTools = ((BaseActivity) context).mBitmapTools;
}
@Override
public int getCount() {
if (downloadManager == null)
return 0;
return downloadManager.getDownloadInfoListCount();
}
@Override
public Object getItem(int i) {
return downloadManager.getDownloadInfo(i);
}
@Override
public long getItemId(int i) {
return i;
}
@SuppressWarnings("unchecked")
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
DownloadItemViewHolder holder = null;
DownloadInfo downloadInfo = downloadManager.getDownloadInfo(i);
if (view == null) {
view = mInflater.inflate(R.layout.new_frg_manage_item, null);
holder = new DownloadItemViewHolder(downloadInfo);
ViewUtils.inject(holder, view);
view.setTag(holder);
holder.refresh();
} else {
holder = (DownloadItemViewHolder) view.getTag();
holder.update(downloadInfo);
}
HttpHandler handler = downloadInfo.getHandler();
if (handler != null) {
RequestCallBack callBack = handler.getRequestCallBack();
if (callBack instanceof DownloadManager.ManagerCallBack) {
DownloadManager.ManagerCallBack managerCallBack = (DownloadManager.ManagerCallBack) callBack;
managerCallBack.setBaseCallBack(new DownloadRequestCallBack());
}
callBack.setUserTag(new WeakReference(holder));
}
return view;
}
public class DownloadItemViewHolder {
@ViewInject(R.id.tv_name)
TextView name;
@ViewInject(R.id.tv_state)
TextView state;
@ViewInject(R.id.tv_progress)
TextView tv_progress;
@ViewInject(R.id.progressBar)
ProgressBar progressBar;
@ViewInject(R.id.btn_down)
Button startBtn;
@ViewInject(R.id.img_icon)
ImageView icon;
private DownloadInfo downloadInfo;
public DownloadItemViewHolder(DownloadInfo downloadInfo) {
this.downloadInfo = downloadInfo;
}
public void update(DownloadInfo downloadInfo) {
this.downloadInfo = downloadInfo;
refresh();
}
@OnClick(R.id.btn_down)
public void start(View v) {
HttpHandler.State state = downloadInfo.getState();
switch (state) {
case WAITING:
Toast.makeText(mContext, "排队中,请稍后~", Toast.LENGTH_SHORT).show();
return;
case STARTED:
case LOADING:
try {
downloadManager.stopDownload(downloadInfo);
} catch (DbException e) {
LogUtils.e(e.getMessage(), e);
}
break;
case FAILURE:
case CANCELLED:
try {
downloadManager.resumeDownload(downloadInfo, new DownloadRequestCallBack());
} catch (DbException e) {
LogUtils.e(e.getMessage(), e);
}
notifyDataSetChanged();
break;
case SUCCESS:
if (Common.isAppInstall(mContext, downloadInfo.getPkg())) {
startBtn.setText(R.string.tv_open);
Common.openApp(mContext, downloadInfo.getPkg());
} else {
startBtn.setText(R.string.tv_install);
if (SDCardTools.exists(downloadInfo.getFileSavePath())) {
Common.installAPK(downloadInfo.getFileSavePath(), mContext);
}
}
break;
default:
break;
}
notifyDataSetChanged();
}
@OnClick(R.id.btn_del)
public void del(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(mContext).setTitle(R.string.app_name).setMessage("您确定要删除么?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
remove(downloadInfo);
}
}).setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
alertDialog.show();
}
public void remove(DownloadInfo downloadInfo) {
try {
downloadManager.removeDownload(downloadInfo);
SDCardTools.delFiel(downloadInfo.getFileSavePath());
} catch (DbException e) {
LogUtils.e(e.getMessage(), e);
}
notifyDataSetChanged();
}
public void refresh() {
bitmapTools.disPlay(icon, downloadInfo.getIcon());
name.setText(downloadInfo.getFileName());
if (Common.isAppInstall(mContext, downloadInfo.getPkg())) {
startBtn.setText("打开");
return;
}
if (downloadInfo.getFileLength() > 0) {
int progress = (int) (downloadInfo.getProgress() * 100 / downloadInfo.getFileLength());
progressBar.setProgress(progress);
tv_progress.setText(mContext.getString(R.string.PROGRESS,
progress) + "%");
} else {
progressBar.setProgress(0);
tv_progress.setText("0%");
}
HttpHandler.State state = downloadInfo.getState();
switch (state) {
case WAITING:
this.state.setText("等待中...");
startBtn.setText("等待中...");
break;
case STARTED:
this.state.setText("开始下载");
startBtn.setText("暂停");
break;
case LOADING:
this.state.setText("正在下载");
startBtn.setText("暂停");
break;
case CANCELLED:
this.state.setText("已暂停");
startBtn.setText("继续");
break;
case SUCCESS:
this.state.setText("成功");
startBtn.setText("安装");
break;
case FAILURE:
this.state.setText("失败");
startBtn.setText("重试");
break;
default:
break;
}
}
}
private class DownloadRequestCallBack extends RequestCallBack {
private void refreshListItem() {
if (userTag == null)
return;
WeakReference tag = (WeakReference) userTag;
DownloadItemViewHolder holder = (DownloadItemViewHolder) tag.get();
if (holder != null) {
holder.refresh();
}
}
@Override
public void onStart() {
refreshListItem();
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
refreshListItem();
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
refreshListItem();
}
@Override
public void onFailure(HttpException error, String msg) {
refreshListItem();
}
@Override
public void onCancelled() {
refreshListItem();
}
}
}