我使用带有回收视图(支架)的适配器。这是代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.playlists_item, null);
holder = new ViewHolder();
holder.picture = (ImageView) convertView.findViewById(R.id.playlist_picture);
holder.name = (TextView) convertView.findViewById(R.id.playlist_name);
holder.desc = (TextView) convertView.findViewById(R.id.playlist_desc);
holder.play_bt = (ImageView) convertView.findViewById(R.id.playlist_play);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.picture.setImageResource(R.drawable.placeholder);
...
if(messages.get(position).containsKey("IMAGE"))
{
if(global.isInCache(messages.get(position).get("IMAGE")))
holder.picture.setImageBitmap(global.getBitmap(messages.get(position).get("IMAGE")));
else{
holder.picture.setTag("http://**********" + messages.get(position).get("IMAGE"));
new loadImageTask().execute(holder.picture,cache,messages.get(position).get("IMAGE"),"********/upload/********/");
}
}
这是我的下载器:
public class loadImageTask extends AsyncTask< Object , Void, Void>
{
...
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Object... params) {
imagem = (ImageView) params[0];
image_tag = (String)imagem.getTag();
caminho = (String) params[3];
name = (String) params[2];
imgLoad = LoadImageFromWeb(caminho+name);
return null;
}
@Override
protected void onPostExecute(Void result) {
if (imgLoad instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable)imgLoad).getBitmap();
}
bitmap = getResizedBitmap(bitmap, 100, 100);
global.putCache(name, bitmap);
imagem.setImageBitmap(bitmap);
}
}
事实证明,当我在listview
下载完成后滚动时,它image
会加载到我可见的位置(并且在快速滚动时的相同位置,listview
每次下载完成时总是会更改图像)。
我希望,如果您正在下载要完成的图像的位置不再可见,那就没有完成setImageBitmap