Android懒惰的图像加载ListView无法正常工作

我捆绑到我的ListView惰性加载图像,图像加载正常,但我有一个问题。 加载图像时会互换。

假设ListView有10行。 它加载第1行的图像,将其显示在第1行,然后加载第2行的图像。 它在第二行显示片刻,然后显示第一行第二行的图像。 然后,row1中的ImageView在第1行和第2行的图像之间切换。 同样,加载下一行的图像。 前一行的图像会在两者之间切换。 然后加载所有的图像后,一切都得到正确显示。

这是我的代码

Adapater课堂:

public class FamilyMemberListAdapter extends ArrayAdapter<Map<String, String>> {
    List<Map<String, String>> familyMemberList = new ArrayList<Map<String, String>>();
    private Activity activity;

    public FamilyMemberListAdapter(Activity activity,
            List<Map<String, String>> familyMemberList) {
        super(activity, R.layout.activity_gch_family_members, familyMemberList);
        this.activity = activity;
        this.familyMemberList = familyMemberList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(this.getContext()).inflate(
                    R.layout.activity_gch_family_member_item, parent, false);
            holder = new ViewHolder();
            holder.lblFamilyMemberName = (TextView) convertView
                    .findViewById(R.id.lblFamilyMemberItem);
            holder.lblFamilyMemberRelation = (TextView) convertView
                    .findViewById(R.id.lblFamilyMemberRelationItem);
            holder.imgProfilePic = (ImageView) convertView
                    .findViewById(R.id.imgvProfilePic);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        int accountId = Integer.valueOf(familyMemberList.get(position).get(
                "accountId"));
        holder.lblFamilyMemberName.setText("Name: "
                + familyMemberList.get(position).get("name"));
        holder.lblFamilyMemberRelation.setText("Relation: "
                + familyMemberList.get(position).get("relation"));
        if (holder.imgProfilePic != null) {
            new ImageDownloaderTask(holder.imgProfilePic).execute(String
                    .valueOf(accountId));
        }
        return convertView;
    }

    @Override
    public int getCount() {
        return familyMemberList.size();
    }

    static class ViewHolder {
        TextView lblFamilyMemberName;
        TextView lblFamilyMemberRelation;
        ImageView imgProfilePic;
    }
}

Imageloader AsyncTask:

public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public ImageDownloaderTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        String responseText = null;
        HttpClient httpClient = ServiceHelper.getHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(RestApiPaths.GET_PROFILE_PIC + accountId);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    // getting contents from the stream
                    inputStream = entity.getContent();

                    // decoding stream data back into image Bitmap that android understands
                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                 } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
            Log.d(TAG, responseText);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                } else {
                    Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.holder_pic_side);
                    imageView.setImageDrawable(placeholder);
                }
            }
        }
    }
}

我为ListView项目创建了一个Static类。 仍然为什么图像在加载时进行交换。 请告诉我我在这里做错了什么。


添加holder.imgProfilePic.setTag(accountId); 在调用AsyncTask之前。 为您的任务添加一个额外的参数accountId。 然后在onPostExecute检查它是否与图像视图中的accountId相同


也许尝试使用像毕加索或通用图像加载器或类似的库。 他们解决了大部分图像加载问题

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

上一篇: android Lazy image load of ListView not working properly

下一篇: Lazy loading Bitmap Images in ListView