当我在列表视图中滚动时,我的图像被洗牌或更改

时间:2013-12-01 09:44:38

标签: java android android-listview

当我在列表视图中滚动时,我的图像被洗牌或更改....... 当我向下滚动并继续改变自己的图像时,图像会被洗牌............... 公共类CustomListAdapter扩展了BaseAdapter {

private ArrayList<NewsItem> listData;

private LayoutInflater layoutInflater;

public CustomListAdapter(Context context, ArrayList<NewsItem> listData) {

    this.listData = listData;
    layoutInflater = LayoutInflater.from(context);

}

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

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.activity_list_search, null);
        holder = new ViewHolder();
        holder.headlineView = (TextView) convertView.findViewById(R.id.title);
        holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
        holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
        holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();

    }

    NewsItem newsItem = (NewsItem) listData.get(position);

    holder.headlineView.setText(newsItem.getHeadline());
    holder.reporterNameView.setText("Model: " + newsItem.getReporterName());
    holder.reportedDateView.setText(newsItem.getDate());

    if (holder.imageView != null) {
        new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
    }

    return convertView;

}

static class ViewHolder {
    TextView headlineView;
    TextView reporterNameView;
    TextView reportedDateView;
    ImageView imageView;
}

}

1 个答案:

答案 0 :(得分:0)

对于新的Android开发人员来说,这个问题非常普遍。

你的ImageDownloaderTask应该关注下载的图像是否仍然应该在屏幕上发布。

例如,在ImageDownloaderTask完成一个图像下载任务(例如索引15)之后,应确保第15个项目仍在屏幕上显示。由于ListView正在重用所有项目视图,因此如果您直接将位图设置为原始引用视图,则您的图像视图将被随机播放。