图像循环,直到添加ViewHolder后异步成功

时间:2014-07-23 07:26:45

标签: java android performance

我的异步任务和ViewHolder有问题......

在使用ViewHolder之前,一切都运行良好,但我想优化和重用childViews以获得更好的性能。但是,由于我添加了ViewHolder,图像开始循环,直到图像加载完成,然后显示正确的图像。

我的问题是我在加载之前不想要这个随机循环,为什么会发生? 我一直试图在进入异步任务之前将visibity设置为GONE,甚至将ImageView.setBitmap设置为null ...仍然会执行此循环的图像,直到加载正确的一个..

它看起来很酷,但我现在不想要它:)

我的代码如下所示:

适配器

public class ContactAdapter extends ArrayAdapter<Contacts> {

private final Context _context;
private final ArrayList<Contact> _contactList;
ViewHolder holder;
ImageHelper task;

public ContactAdapter(Context context, ArrayList contactList) {
    super(context, R.layout.row_contact_list, contactList);

    this._context = context;
    this._contactList = contactList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Create inflater
    LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null) {
        // Get rowView from inflater
        convertView = inflater.inflate(R.layout.row_contact_list, parent, false);

        this.holder = new ViewHolder();

        // Get childViews within rowView
        holder.urlView = (ImageView) convertView.findViewById(R.id.contact_url);
        holder.contactLoading = (ProgressBar) convertView.findViewById(R.id.contact_loading);
        holder.nameView = (TextView) convertView.findViewById(R.id.contact_name);

        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
        holder.urlView.setImageBitmap(null);
    }

    // Assign the values to the childViews
    // Either using library Picasso or own AsyncTask, in this case named ImageHelper
    //Picasso.with(this._context).load(_contactList.get(position).getUrl()).into(urlView);
    task = new ImageHelper(holder.urlView, holder.contactLoading);
    holder.urlView.setImageBitmap(null);
    task.execute(_contactList.get(position).getUrl());
    holder.nameView.setText(_contactList.get(position).getName());

    return convertView;
}

static class ViewHolder {
    ImageView urlView;
    TextView nameView;
    ProgressBar contactLoading ;
}
}

异步任务

public class ImageHelper extends AsyncTask<String, Void, Bitmap> {

private final WeakReference<ImageView> imageViewReference;
private ProgressBar progressBar;
private Boolean running = true;

public ImageHelper(ImageView imageView, ProgressBar progressBar) {
    this.imageViewReference = new WeakReference<ImageView>(imageView);
    this.progressBar = progressBar;
}

@Override
protected void onPreExecute() {
    imageViewReference.get().setVisibility(View.GONE);
    progressBar.setVisibility(View.VISIBLE);
}

protected Bitmap doInBackground(String... params) {
    while (running) {
        try {
            URL url = new URL(params[0]);
            HttpURLConnection httpCon =
                    (HttpURLConnection) url.openConnection();
            if (httpCon.getResponseCode() != 200)
                throw new Exception("Failed to connect");
            InputStream is = httpCon.getInputStream();
            return BitmapFactory.decodeStream(is);
        } catch (Exception e) {
            Log.e("Image", "Failed to load image", e);
        }
    }
    return null;
}

@Override
protected void onCancelled() {
    running = false;
}

// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
    if (imageViewReference != null && bitmap != null) {
        final ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            progressBar.setVisibility(View.GONE);
            imageViewReference.get().setVisibility(View.VISIBLE);
            imageView.setImageBitmap(bitmap);
        }
    }
}
}

我感谢我能得到的任何帮助:)

0 个答案:

没有答案