listview中的可重复图像 - Android

时间:2015-07-10 06:42:48

标签: android listview

我有ListView,其中包含从服务器下载的图片和文字。

文字显得很好而且不重复,但图片却没有。我有模板布局来显示我根据收到的图像数量膨胀的图像。然而,在我的第三个列表条目(行)之后,图像开始重复。

以下是getView()

中的ArrayAdapter方法
@Override
    public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(resourceId, parent, false);
    }

    TextView feedHeading = (TextView) convertView.findViewById(R.id.tv_feedheading);
    TextView feedSubheading = (TextView) convertView.findViewById(R.id.tv_feedsubheading);

    LinearLayout imagesLayout = (LinearLayout) convertView.findViewById(R.id.ll_images);

    View view;
    Feeds feed = mfeeds.get(position);

    ImageLoader.getInstance().clearDiskCache();
    ImageLoader.getInstance().clearMemoryCache();

    feedHeading.setText(feed.title);
    feedSubheading.setText(feed.subtitle);

    ImageView image1;
    ImageView image2 = null;
    ImageView image3 = null;
    ImageView image4 = null;
    ImageView image5 = null;
    List<ImageView> imageViewList = new ArrayList<>();

    if (feed.imageIds.length == 0){
        view = inflater.inflate(R.layout.template_oneimage2, imagesLayout, false);
        imageViewList.clear();
        image1 = (ImageView) view.findViewById(R.id.iv_t_1);
        imageViewList.add(image1);

    }
    else if (feed.imageIds.length == 1){
        view = inflater.inflate(R.layout.template_oneimage2, imagesLayout, false);
        imageViewList.clear();
        image1 = (ImageView) view.findViewById(R.id.iv_t_1);
        imageViewList.add(image1);
    }
    else if (feed.imageIds.length == 2){
        view = inflater.inflate(R.layout.template_twoimage, imagesLayout, false);
        imageViewList.clear();
        image1 = (ImageView) view.findViewById(R.id.iv_t_1);
        image2 = (ImageView) view.findViewById(R.id.iv_t_2);
        imageViewList.add(image1);
        imageViewList.add(image2);
    }
    else if (feed.imageIds.length == 3){
        view = inflater.inflate(R.layout.template_threeimage1, imagesLayout, false);
        imageViewList.clear();
        image1 = (ImageView) view.findViewById(R.id.iv_t_1);
        image2 = (ImageView) view.findViewById(R.id.iv_t_2);
        image3 = (ImageView) view.findViewById(R.id.iv_t_3);

        imageViewList.add(image1);
        imageViewList.add(image2);
        imageViewList.add(image3);
    }
    else if (feed.imageIds.length == 4){
        view = inflater.inflate(R.layout.template_fourimage1, imagesLayout, false);
        imageViewList.clear();
        image1 = (ImageView) view.findViewById(R.id.iv_t_1);
        image2 = (ImageView) view.findViewById(R.id.iv_t_2);
        image3 = (ImageView) view.findViewById(R.id.iv_t_3);
        image4 = (ImageView) view.findViewById(R.id.iv_t_4);

        imageViewList.add(image1);
        imageViewList.add(image2);
        imageViewList.add(image3);
        imageViewList.add(image4);
    }
    else {
        view = inflater.inflate(R.layout.template_fiveimage, imagesLayout, false);
        imageViewList.clear();
        image1 = (ImageView) view.findViewById(R.id.iv_t_1);
        image2 = (ImageView) view.findViewById(R.id.iv_t_2);
        image3 = (ImageView) view.findViewById(R.id.iv_t_3);
        image4 = (ImageView) view.findViewById(R.id.iv_t_4);
        image5 = (ImageView) view.findViewById(R.id.iv_t_5);

        imageViewList.add(image1);
        imageViewList.add(image2);
        imageViewList.add(image3);
        imageViewList.add(image4);
        imageViewList.add(image5);
    }

    imagesLayout.addView(view);

    for (int i=0; i<feed.imageIds.length; i++){
        //load image using imageloader

        //use specific imageid in imagepath
        String format = feed.imageFormat.replace("{image_id}",String.valueOf(feed.imageIds[i]));

        ImageLoader.getInstance().displayImage(format,imageViewList.get(i), Utility.displayImageOptions);
    }

    return convertView;
}

1 个答案:

答案 0 :(得分:1)

对此不确定,但我认为问题来自这一行:

imagesLayout.addView(view);

当视图被回收时,每次“创建”视图时,都会将y images of imagesview列表添加到布局中。

每次创建视图时,都不应清除缓存。

 ImageLoader.getInstance().clearDiskCache();
 ImageLoader.getInstance().clearMemoryCache();