public class FragAdapter extends BaseAdapter implements View.OnClickListener {
WeakReference<Context> downloadsWeakReference;
List<DownloadRequest> downloadsPojos;
Button pause, resume, cancel, open, remove;
public FragAdapter(WeakReference<Context> downloadsWeakReference, List<DownloadRequest> downloadsPojos) {
this.downloadsWeakReference = downloadsWeakReference;
this.downloadsPojos = downloadsPojos;
}
@Override
public int getCount() {
if (downloadsPojos != null) {
return downloadsPojos.size();
} else {
return 0;
}
}
@Override
public Object getItem(int i) {
if (downloadsPojos != null) {
return downloadsPojos.get(i);
} else {
return null;
}
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(downloadsWeakReference.get()).inflate(R.layout.download_list_item, viewGroup, false);
}
ProgressBar progressBar = view.findViewById(R.id.downloadProgressBar);
TextView name = view.findViewById(R.id.name);
TextView down_progress = view.findViewById(R.id.down_progress);
TextView download_size = view.findViewById(R.id.download_size);
TextView status = view.findViewById(R.id.status);
cancel = view.findViewById(R.id.cancel);
pause = view.findViewById(R.id.pause);
resume = view.findViewById(R.id.resume);
open = view.findViewById(R.id.open);
remove = view.findViewById(R.id.remove);
pause.setTag(i);
resume.setTag(i);
remove.setTag(i);
cancel.setTag(i);
DownloadRequest downloadRequest = downloadsPojos.get(i);
progressBar.setMax((int) downloadRequest.getTotalBytes());
progressBar.setProgress((int) downloadRequest.getDownloadedBytes());
down_progress.setText(String.valueOf((int) downloadRequest.getDownloadedBytes() / 100));
name.setText(downloadRequest.getFileName());
download_size.setText(String.valueOf((int) downloadRequest.getTotalBytes() / 1000000));
//status.setText(String.valueOf(PRDownloader.getStatus(downloadsPojo.getDownloadId())));
cancel.setText(R.string.cancel);
pause.setText(R.string.pause);
resume.setText(R.string.resume);
pause.setOnClickListener(this);
resume.setOnClickListener(this);
cancel.setOnClickListener(this);
open.setOnClickListener(this);
remove.setOnClickListener(this);
return view;
}
public void updateResults(List<DownloadRequest> results) {
downloadsPojos = results;
notifyDataSetChanged();
}
public void hideView() {
cancel.setVisibility(View.GONE);
remove.setVisibility(View.VISIBLE);
pause.setVisibility(View.GONE);
resume.setVisibility(View.GONE);
open.setVisibility(View.VISIBLE);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.cancel:
int cancelTag = (int) view.getTag();
PRDownloader.cancel(Constant.downloadsPojoList.get(cancelTag).getDownloadId());
for (int i = 0; i < Constant.downloadsPojoList.size(); i++) {
if (Constant.downloadsPojoList.get(i).getDownloadId() == Constant.downloadsPojoList.get(cancelTag).getDownloadId()) {
Log.e("constant", "constant");
Constant.downloadsPojoList.remove(i);
}
}
updateResults(Constant.downloadsPojoList);
break;
case R.id.pause:
int pos = (int) view.getTag();
PRDownloader.pause(Constant.downloadsPojoList.get(pos).getDownloadId());
pause.setVisibility(View.GONE);
resume.setVisibility(View.VISIBLE);
break;
case R.id.resume:
int posi = (int) view.getTag();
PRDownloader.resume(Constant.downloadsPojoList.get(posi).getDownloadId());
Log.e("resume", String.valueOf(posi));
pause.setVisibility(View.VISIBLE);
resume.setVisibility(View.GONE);
break;
case R.id.open:
//For showing downloads folder.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + File.separator + "sachin")), "*/*");
downloadsWeakReference.get().startActivity(Intent.createChooser(intent, "Open Downloads using"));
break;
case R.id.remove:
int removeTag = (int) view.getTag();
for (int i = 0; i < Constant.downloadsPojoList.size(); i++) {
if (PRDownloader.getStatus(Constant.downloadsPojoList.get(i).getDownloadId()) == PRDownloader.getStatus(Constant.downloadsPojoList.get(removeTag).getDownloadId())) {
Constant.downloadsPojoList.remove(i);
Log.e("removed", "removed");
}
}
updateResults(Constant.downloadsPojoList);
break;
default:
}
}
}
在此列表中,每个列表项都有五个按钮PAUSE,RESUME,CANCEL,OPEN,REMOVE。其中两个按钮将一次显示,其他按钮可见性将消失。 这是一个下载列表,显示哪个文件正在下载两个按钮,CANCEL和PAUSE,点击PAUSE按钮后,PAUSE可见性将变为GONE,RESUME可见性将变为VISIBLE,点击RESUME按钮后,RESUME的可见性变为GONE, PAUSE可见性变得可见。 所以,问题是当我下载两个文件并单击第一个项目的暂停按钮时,恢复按钮将出现在第二个项目上而不是第一个项目上。它禁止我暂停第二次下载。
答案 0 :(得分:1)
在DownloadRequest模型中引入布尔变量,如 isResume , isPause 等。 在模型中应用变量字段后,尝试在单击 PAUSE , RESUME 等按钮时在条件下更改布尔值true / false,并确保执行 notifyDataSetChanged ()强>
这将解决您的问题。
如果您仍然遇到任何问题,请提供型号和活动代码。