Images in ListView are repeated

最后都变了- 提交于 2019-12-13 04:39:52

问题


I'm parsing vk's group wall. Some posts have photos(from 1 to 10), some don't have. And I need to display them. However, all the images are repeated. And I can't make them appear correctly. I'm using Picasso at the moment. It says it can process ListView images, however it doesn't. Other ways I foun on google don't work either. The question is how to process images correctly. Here is a piece of code that is responsible for photo.

private void place_photos(View view, ArrayList<VKPhoto> photos) {
    int pcount = photos.size();
    final View v = view;
    TableRow first = (TableRow)v.findViewById(R.id.first_row);
    TableRow second = (TableRow)v.findViewById(R.id.second_row);
    TableRow third = (TableRow)v.findViewById(R.id.third_row);

    for (int i = 0; i < pcount; i++) {
        if (i == 1) {
            ImageView img = new ImageView(mContext);
            String url = photos.get(i).photo_807;
            img.setTag(url);
            Picasso.with(mContext).load(url).into(img);
            if (first != null)
                first.addView(img);
        }
        if (i == 2 || i == 3) {
            ImageView img = new ImageView(mContext);
            String url = photos.get(i).photo_807;
            img.setTag(url);
            Picasso.with(mContext).load(url).into(img);
            if (second != null)
                second.addView(img);
        }
        if (i > 3) {
            ImageView img = new ImageView(mContext);
            String url = photos.get(i).photo_807;
            img.setTag(url);
            Picasso.with(mContext).load(url).into(img);
            if (third != null)
                third.addView(img);
        }
    }
}

And that's how it's called in getView method:

if (post.attachments.hasPhoto) {
        place_photos(v, post.attachments.photos);
} else {
        viewHolder.photo_wrapper.setVisibility(View.GONE);
}

UPD Here is the full adapter code: https://gist.github.com/alexbat98/7f22598e7e73d301a4ef


回答1:


Try

ImageView img; 

inside ViewHolder class and then use it as

 viewHolder.img = viewHolder.photo;
     viewHolder.img.setTag(photo_100);
     imageLoader.DisplayImage(photo_100,  viewHolder.img);



回答2:


Issue should be due to the ListView recycle.

Try to override getViewTypeCount and getItemViewType

If your view types are different then return the type count, e.g. if 3 different types,then

@Override
public int getViewTypeCount() {
    return 3;
}

And, then tell the ListView which position of item is different from other, e.g., if position 0 and 1 are different from other items then change it like this

@Override
public int getItemViewType(int position){
    // return a unique number
    if(position == 0){
        return 0;
    }
    if(position == 1){
        return 1;
    }
    else {
        return 2;
    }
}


来源:https://stackoverflow.com/questions/23564596/images-in-listview-are-repeated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!