Android ListView如何添加项目不使用notifyDataSetChanged?

时间:2014-09-11 01:31:44

标签: android listview notifydatasetchanged

我有ListView,底部有一个加载更多按钮。 单击“加载更多”按钮时,从远程添加数据以添加列表适配器数据集。 每个列表项都有一张大照片使用ImageLoader加载。 当使用适配器notifyDataSetChanged时,所有项目都将getView并且所有项目都将重绘,大照片将重绘,感觉UI刷新。 有一个方法通知新添加的项目已更改,只重新加载新项目吗?

我的适配器是这样的:

public class PhotoSquareAdapter extends BaseAdapter 
{
private List<PhotoRecom> data;
public PhotoSquareAdapter(List<PhotoRecom> data)
{
    this.data = data;
}
public void addList(List<PhotoRecom> d) {
    for (PhotoRecom n : d) {
        data.add(n);
        //addView(data.size() - 1);
    }
    super.notifyDataSetChanged();
}
@Override
public int getCount() {
    return data.size();
}
@Override
public PhotoRecom getItem(int position) {
    if (position >= getCount())
        return data.get(getCount() - 1);
    return data.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final PhotoRecom item = getItem(position);
    final ViewHolder holder;

    if (convertView == null) {
        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.photo_square_list_item, parent, false);
        holder = new ViewHolder();
        holder.iv_photo_square_listitem_head_thumbnail = (ImageView) convertView.findViewById(R.id.iv_photo_square_listitem_head_thumbnail);
        holder.iv_photo_square_city = (ImageView) convertView.findViewById(R.id.iv_photo_square_city);
        holder.tv_photo_square_nick = (TextView) convertView.findViewById(R.id.tv_photo_square_nick);
        holder.tv_photo_square_title = (TextView) convertView.findViewById(R.id.tv_photo_square_title);
        holder.tv_photo_square_like = (TextView) convertView.findViewById(R.id.tv_photo_square_like);
        holder.iv_photo_square_photo = (ImageView) convertView.findViewById(R.id.iv_photo_square_photo);
        holder.tv_photo_square_saw = (TextView) convertView.findViewById(R.id.tv_photo_square_saw);
        holder.tv_photo_square_chat = (TextView) convertView.findViewById(R.id.tv_photo_square_chat);
        holder.pb_loading_img = (ProgressBar) convertView.findViewById(R.id.pb_loading_img);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    imageLoader.displayImage((url + item.getPhoto()), holder.iv_photo_square_photo, options);
    //...
    return convertView;
}
private class ViewHolder {
    ImageView iv_photo_square_listitem_head_thumbnail;
    ImageView iv_photo_square_city;
    TextView tv_photo_square_nick;
    ProgressBar pb_loading_img;
    TextView tv_photo_square_title;
    TextView tv_photo_square_like;
    ImageView iv_photo_square_photo;
    TextView tv_photo_square_saw;
    TextView tv_photo_square_chat;
}

}

1 个答案:

答案 0 :(得分:2)

您无法拨打notifyDatasetChanged。这将导致至少为屏幕上的行调用getView。您应该为图像使用某种缓存,这样您就不必总是从零开始加载它们。有很多库可供您使用,或者您可以使用支持库中的LruCache