2014-09-11 64 views
0

我有一个ListView,底部有一个加载更多按钮。 当点击加载更多按钮时,从远程添加数据以添加列表适配器数据集。 每个列表项都有一个大的照片使用ImageLoader加载。 使用适配器notifyDataSetChanged时,所有项目都会得到View,所有项目都会重绘,大照片会重绘,感觉UI刷新。 是否有方法通知新添加的项目更改,只重新加载新项目?Android ListView如何添加项目不使用notifyDataSetChanged?

我的适配器是这样的:

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; 
} 

} 

回答

2

不能避免调用notifyDatasetChanged。这将导致至少为屏幕上的行调用getView。你应该为你的图像使用某种缓存,这样你就不需要总是从空间加载它们。有很多这样的库,或者您可以使用支持库中的LruCache

+0

这里指出的一点是,您绝对不应该在每次列表项视图加载时调用图像加载。这对于一个设备来说确实很乏味。有很多方法可以暂时缓存数据 – zgc7009 2014-09-11 02:13:48

相关问题