2014-09-11 47 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

相關問題