我正在使用Async Task在我的recyclerView中添加数据并为每个列表项设置进度条,当我点击项目将下载的项目和正在加载的进度条显示时。在我去另一个活动然后返回下载过程工作但进度条不显示我想看到进度条。
我认为这是问题:在onClick监听器中我显示进度条
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getLayoutPosition();
downloading = true;
holder.progressBar.setVisibility(view.VISIBLE);
FeedItem feedItem = feedItemList.get(position);
String url = feedItem.getUrl();
url = url.replace(" ", "%20");
startDownload(position, url);
}
};
这是我的完整代码
public class SingleMovieAdapter extends RecyclerView.Adapter<CustomViewHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
private Handler handler = new Handler();
private boolean downloading = false;
public SingleMovieAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_movie_songlist_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
FeedItem feedItem = feedItemList.get(i);
String title = feedItem.getTitle();
title = title.replaceAll(" - TamilTunes.com.mp3", "");
customViewHolder.textView.setText(Html.fromHtml(title));
customViewHolder.txtSinger.setText(Html.fromHtml(feedItem.getSinger()));
customViewHolder.progressBar.setProgress(feedItem.getProgress());
customViewHolder.textView.setOnClickListener(clickListener);
customViewHolder.imageView.setOnClickListener(clickListener);
customViewHolder.textView.setTag(customViewHolder);
customViewHolder.imageView.setTag(customViewHolder);
if (downloading == false) {
ColorGenerator generator = ColorGenerator.MATERIAL;
int color1 = generator.getRandomColor();
TextDrawable drawable = TextDrawable.builder().buildRound(String.valueOf(i + 1), color1);
customViewHolder.imageView.setImageDrawable(drawable);
}
}
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getLayoutPosition();
downloading = true;
holder.progressBar.setVisibility(view.VISIBLE);
FeedItem feedItem = feedItemList.get(position);
String url = feedItem.getUrl();
url = url.replace(" ", "%20");
startDownload(position, url);
}
};
public void startDownload(final int position, final String url) {
Runnable runnable = new Runnable() {
int Status = 0;
public void run() {
System.out.println("URL-" + url);
String urlDownload = url;
int count = 0;
try {
URL url = new URL(urlDownload);
HttpURLConnection conexion = (HttpURLConnection) url.openConnection();
conexion.setRequestProperty("Accept-Encoding", "identity");
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
// Get File Name from URL
String fileName = urlDownload.substring(urlDownload.lastIndexOf('/') + 1, urlDownload.length());
fileName = fileName.replaceAll("%20-%20TamilTunes.com", "");
fileName = fileName.replaceAll("%20", " ");
OutputStream output = new FileOutputStream("/sdcard/" + fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
System.out.println("Output - total" + total);
total += count;
Status = ((int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
// Update ProgressBar
handler.post(new Runnable() {
public void run() {
System.out.println("Output - Status" + Status);
updateStatus(position, Status);
}
});
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
}
};
new Thread(runnable).start();
}
public void updateStatus(int position, int Status) {
FeedItem feedItem = feedItemList.get(position);
feedItem.setProgress(Status);
notifyItemChanged(position);
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
}
答案 0 :(得分:0)
尝试在onBindViewHolder方法中添加其他内容,也许您的视图会重新启用,如果下载正在运行,您也不会将视图设置为可见。
if (downloading == false) {
ColorGenerator generator = ColorGenerator.MATERIAL;
int color1 = generator.getRandomColor();
TextDrawable drawable = TextDrawable.builder().buildRound(String.valueOf(i + 1), color1);
customViewHolder.imageView.setImageDrawable(drawable);
} else {
holder.progressBar.setVisibility(view.VISIBLE);
}