Android ListView如何添加项目don';不使用notifyDataSetChanged?
我有一个Android ListView如何添加项目don';不使用notifyDataSetChanged?,android,listview,notifydatasetchanged,Android,Listview,Notifydatasetchanged,我有一个列表视图,底部有一个加载更多按钮。 单击“加载更多”按钮时,从远程添加数据以添加列表适配器数据集。 每个列表项都有一张大照片,请使用ImageLoader加载。 当使用adapternotifyDataSetChanged时,所有项目都将获取视图,所有项目都将重画,大照片将重画,感觉UI刷新。 是否已更改通知新添加项的方法,并且仅重新加载新项 我的适配器如下所示: public class PhotoSquareAdapter extends BaseAdapter { private
列表视图
,底部有一个加载更多按钮。
单击“加载更多”按钮时,从远程添加数据以添加列表适配器数据集。
每个列表项都有一张大照片,请使用ImageLoader加载。
当使用adapternotifyDataSetChanged
时,所有项目都将获取视图,所有项目都将重画,大照片将重画,感觉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;
}
}
公共类PhotoSquareAdapter扩展BaseAdapter
{
私人名单数据;
公共PhotoSquareAdapter(列表数据)
{
这个数据=数据;
}
公共无效地址列表(列表d){
用于(PhotoCom n:d){
数据。添加(n);
//addView(data.size()-1);
}
super.notifyDataSetChanged();
}
@凌驾
public int getCount(){
返回data.size();
}
@凌驾
公共PhotoCom getItem(内部位置){
如果(位置>=getCount())
返回data.get(getCount()-1);
返回数据。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
最终项目=获取项目(位置);
最终持票人;
if(convertView==null){
LayoutInflater li=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
convertView=li.充气(R.layout.photo\u square\u list\u项,父项,false);
holder=新的ViewHolder();
holder.iv_photo_square_listitem_head_thumbj=(ImageView)convertView.findViewById(R.id.iv_photo_square_listitem_head_thumbj);
holder.iv_photo_square_city=(ImageView)convertView.findViewById(R.id.iv_photo_square_city);
holder.tv\u photo\u square\u nick=(TextView)convertView.findviewbyd(R.id.tv\u photo\u square\u nick);
holder.tv\u photo\u square\u title=(TextView)convertView.findViewById(R.id.tv\u photo\u square\u title);
holder.tv_photo_square_like=(TextView)convertView.findviewbyd(R.id.tv_photo_square_like);
holder.iv_photo_square_photo=(ImageView)convertView.findViewById(R.id.iv_photo_square_photo);
holder.tv\u photo\u square\u saw=(TextView)convertView.findviewbyd(R.id.tv\u photo\u square\u saw);
holder.tv\u photo\u square\u chat=(TextView)convertView.findviewbyd(R.id.tv\u photo\u square\u chat);
holder.pb_loading_img=(ProgressBar)convertView.findviewbyd(R.id.pb_loading_img);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
displayImage((url+item.getPhoto()),holder.iv\u photo\u square\u photo,选项);
//...
返回视图;
}
私有类视窗持有者{
ImageView iv\照片\正方形\列表项\头部\缩略图;
ImageView iv_photo_square_city;
TextView tv\u photo\u square\u nick;
ProgressBar pb\u加载\u img;
TextView电视、照片、方形标题;
TextView电视、照片、正方形;
ImageView iv_photo_square_photo;
TextView电视、照片、方形锯;
TextView电视、照片、广场、聊天;
}
}
您无法避免调用notifyDatasetChanged
。这将导致至少为屏幕上的行调用getView。您应该为图像使用某种缓存,这样您就不必总是从无到有地加载它们。有很多库可用于此目的,或者您可以从支持库使用。关键是,您不必每次加载列表项视图时都调用图像加载。对于一台设备来说,这真的是一项乏味的工作。有许多方法可以临时缓存数据