我开始开发一个新的Android单声道应用程序,需要我从远程服务器加载大量图像。为了做到这一点,我编写了一个视图,我可以插入布局,并在后台加载图像。这非常好用,直到我在ListView的适配器中使用它,这是我主要计划使用它的地方。应用程序加载一些图像而不加载其他图像,并将它们加载到错误的位置。滚动列表后,它会立即开始正确加载图像,但显示的第一个图像不完整且错误。我尝试过不同的策略来运行后台进程(asynctask,threadpools等),并且都有同样的问题。这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using System.Net;
using Android.Graphics;
using ListViewExample;
using System.Threading;
namespace klvpn.components
{
public class WebImageView : RelativeLayout
{
ImageView theImage;
TextView waitingTxt;
Activity context;
Uri imageUri;
WebClient wc;
public WebImageView(Context context, IAttributeSet attrs) :
base(context, attrs)
{
this.context = context as Activity;
this.SetBackgroundColor(Android.Graphics.Color.Black);
this.SetGravity(GravityFlags.Center);
waitingTxt = new TextView(context);
waitingTxt.SetText("...", TextView.BufferType.Normal);
AddView(waitingTxt, new LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent));
}
public void SetImageSize(int width, int height)
{
LayoutParameters.Width = width;
LayoutParameters.Height = height;
}
public void SetImageUriStr(string uriStr)
{
imageUri = new Uri(uriStr);
if (theImage == null)
{
theImage = new ImageView(context);
AddView(theImage, 0);
}
wc = new WebClient();
wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
wc.DownloadDataAsync(imageUri);
}
void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (e.Error != null)
{
context.RunOnUiThread(() =>
{
theImage.SetImageResource(Resource.Drawable.noImage_x);
string error = "URI:" + imageUri.OriginalString + "\n" + e.Error.Message;
Log.Error("WebImageView", error);
});
}
else
{
Bitmap bmp = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);
context.RunOnUiThread(() =>
{
theImage.SetImageBitmap(bmp);
});
}
wc = null;
}
protected override void OnDetachedFromWindow()
{
base.OnDetachedFromWindow();
if (wc != null)
{
wc.CancelAsync();
}
}
}
}
并且适配器的GetView方法如下所示:
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = this.Episodes[position];
var view = convertView;
if (convertView == null || !(convertView is LinearLayout))
{
view = context.LayoutInflater.Inflate(Resource.Layout.EpisodeItem, parent, false);
}
var imageItem = view.FindViewById<WebImageView>(Resource.Id.imageItem);
var textTop = view.FindViewById<TextView>(Resource.Id.textTop);
var textBottom = view.FindViewById<TextView>(Resource.Id.textBottom);
//imageItem.SetImageResource(item.Image);
imageItem.SetImageSize(100, 36);
imageItem.SetImageUriStr(item.ImageUri);
textTop.SetText(item.Name, TextView.BufferType.Normal);
textBottom.SetText(item.Description, TextView.BufferType.Normal);
return view;
}
任何想法如何解决这个问题?我在MonoTouch中使用了这个策略并且非常好用,因为图像下载是封装的,用户不必等到所有内容都加载才能看到它们。
由于