我有一个包含cardViews的RecyclerView作为成员,cardview包含图像视图,图像来自网址,并带有异步方法。 它可以正常工作,但是当我快速滚动几次时会导致未处理的错误。 我该怎么办?
public override async void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
{
var item = items[position];
var holder = viewHolder as MyViewHolder;
holder.image.SetImageBitmap(null);
holder.fname.Text = items[position].fname;
holder.sname.Text = items[position].sname;
holder.age.Text = items[position].age.ToString();
holder.progressLayout.Visibility = ViewStates.Visible;
using (var imageBitmap = await GetImageBitmapFromUrl(items[position].imageURL))
{
var width = imageBitmap.Width;
var height = imageBitmap.Height;
var max = Math.Max(width, height);
var scale = (double)holder.image.Width / max;
var scaled = Bitmap.CreateScaledBitmap(imageBitmap, (int)(width * scale), (int)(height * scale), false);
holder.image.SetImageBitmap(scaled);
}
holder.progressLayout.Visibility = ViewStates.Gone;
}
private async Task<Bitmap> GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
using (var webClient = new WebClient())
{
var imageBytes = await webClient.DownloadDataTaskAsync(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = await BitmapFactory.DecodeByteArrayAsync(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}
答案 0 :(得分:0)
解决, 在将其设置为imageview位图
后,我处理了“缩放”变量 var scaled = Bitmap.CreateScaledBitmap(imageBitmap, (int)(width * scale), (int)(height * scale), false);
holder.image.SetImageBitmap(scaled);
scaled.Dispose(); // here