LimitDownloadAdapter.java
4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.cnlive.downloadfile.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.cnlive.downloadfile.R;
import com.cnlive.downloadfile.download.limit.DownLoadService;
import com.cnlive.downloadfile.download.limit.DownloadInfo;
import com.cnlive.downloadfile.download.limit.DownloadStatus;
import java.util.List;
public class LimitDownloadAdapter extends RecyclerView.Adapter<LimitDownloadAdapter.UploadHolder> {
private Context mContext;
private List<DownloadInfo> mdata;
private DelectClick delectClick;
public DelectClick getDelectClick() {
return delectClick;
}
public void setDelectClick(DelectClick delectClick) {
this.delectClick = delectClick;
}
public LimitDownloadAdapter(Context context , List<DownloadInfo> mdata) {
this.mContext = context;
this.mdata = mdata;
}
/**
* 更新下载进度
* @param info
*/
public void updateProgress(DownloadInfo info){
for (int i = 0; i < mdata.size(); i++){
if (mdata.get(i).getUrl().equals(info.getUrl())){
mdata.set(i,info);
notifyItemChanged(i);
break;
}
}
}
@Override
public UploadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.item_download_layout,null);
return new UploadHolder(view);
}
@Override
public void onBindViewHolder(final UploadHolder holder, final int position) {
final DownloadInfo info = mdata.get(position);
holder.name.setText(info.getUrl().substring(info.getUrl().length()-5,info.getUrl().length()));
if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_WAIT)){
holder.main_btn_down.setText("待下载");
}else if (info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_PAUSE) || info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD_NET_PAUSE)){
holder.main_btn_down.setText("已暂停");
}
if (info.getTotal() == 0){
holder.main_progress.setProgress(0);
}else {
float d = info.getProgress() * holder.main_progress.getMax() / info.getTotal();
holder.main_progress.setProgress((int) d);
if(info.getDownloadStatus().equals(DownloadStatus.DOWNLOAD)) holder.main_btn_down.setText("下载中");
}
holder.main_btn_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(holder.main_btn_down.getText().toString().equals("下载中")){//暂停
Intent intent = new Intent(mContext, DownLoadService.class);
intent.setAction(DownLoadService.ACTION_PAUSE);
intent.putExtra("fileUrl", info);
mContext.startService(intent);
}else if(holder.main_btn_down.getText().toString().equals("已暂停")){
Intent intent = new Intent(mContext, DownLoadService.class);
intent.setAction(DownLoadService.ACTION_WAITING);
intent.putExtra("fileUrl", info);
mContext.startService(intent);
}else {
Intent intent = new Intent(mContext, DownLoadService.class);
intent.setAction(DownLoadService.ACTION_START);
intent.putExtra("fileUrl", info);
mContext.startService(intent);
}
}
});
holder.main_btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(delectClick != null){
delectClick.onDelectClickListener(info,position);
}
}
});
}
@Override
public int getItemCount() {
return mdata.size();
}
public class UploadHolder extends RecyclerView.ViewHolder{
private ProgressBar main_progress;
private Button main_btn_down;
private TextView name;
private Button main_btn_cancel;
public UploadHolder(View itemView) {
super(itemView);
main_progress = itemView.findViewById(R.id.main_progress);
main_btn_down = itemView.findViewById(R.id.main_btn_down);
name = itemView.findViewById(R.id.name);
main_btn_cancel = itemView.findViewById(R.id.main_btn_cancel);
}
}
public interface DelectClick{
void onDelectClickListener(DownloadInfo downloadInfo,int position);
}
}