Android适配器(回收视图)与assyncTask图像下载

我使用一个适配器蒙回收视图(持有人)。 这是代码:

@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);


    }
}

事实证明,当我在下载完成时在列表视图中滚动时,图像被加载到可见位置(并且在滚动速度很快时在列表视图的相同位置中,每次下载完成时图像总是被更改)。

我希望,如果要下载的图像的位置不再可见,则不会执行setImageBitmap

谢谢你,对不起我的英文。

链接地址: http://www.djcxy.com/p/50597.html

上一篇: Android adapter (recycler views) with assyncTask image downloader

下一篇: Duplicate items in GridView widget