在RecyclerView中滚动几次会导致错误xamarin.android

时间:2017-04-03 07:29:45

标签: asynchronous android-recyclerview xamarin.android

我有一个包含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;
    }

1 个答案:

答案 0 :(得分:0)

解决, 在将其设置为imageview位图

后,我处理了“缩放”变量
            var scaled = Bitmap.CreateScaledBitmap(imageBitmap, (int)(width * scale), (int)(height * scale), false);
            holder.image.SetImageBitmap(scaled);
            scaled.Dispose(); // here