带有assyncTask图像下载器的Android适配器(回收器视图)

时间:2012-06-20 09:18:35

标签: android listview adapter

我使用带有回收视图的适配器(holder)。这是代码:

@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

1 个答案:

答案 0 :(得分:0)

尝试Picasso或Glide库

     Picasso.get().load(url)
     .placeholder(R.drawable.iconname)
     .error(R.drawable.iconname)
      .into(/*your image view*/);

    Glide.with(context)
     .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
     .into(ivImg);