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;
}
}
這裏指出的一點是,您絕對不應該在每次列表項視圖加載時調用圖像加載。這對於一個設備來說確實很乏味。有很多方法可以暫時緩存數據 – zgc7009 2014-09-11 02:13:48