【发布时间】:2014-10-22 05:55:52
【问题描述】:
我遇到一个问题,每当我滚动浏览我的列表视图时,图像似乎都会不断地重新加载,这使得列表视图滞后很多。 我能做些什么来防止这种情况发生,我之前在我的列表视图中做过这个,但它没有这样做。
public class PostsAdapter extends BaseAdapter{
public List<PostList> postList;
protected Context context;
public void add(PostList object,int position) {
postList.add(position,object);
}
public PostsAdapter(Context context) {
this.context = context;
postList = new ArrayList<PostList>();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return postList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return postList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.posts_list, null);
holder = new ViewHolder();
holder.image = (ImageView)convertView.findViewById(R.id.postImage);
holder.username = (TextView)convertView.findViewById(R.id.postUsername);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.username.setText(postList.get(position).user);
Picasso.with(context).load(postList.get(position).postPicture).into(holder.image);
return convertView;
}
static class ViewHolder{
ImageView image;
TextView username;
}
【问题讨论】:
-
在你的
if(convertView==null) {...}中添加Picasso.with(context).load(postList.get(position).postPicture).into(holder.image);从其他地方删除。 -
public void add(PostList object,int position) { postList.add(position,object); } 这就是为什么你使用
-
public PostsAdapter(Context context,ArrayList
List) { this.context = context;后列表 = 列表; } 在适配器中使用这个缺点
标签: android listview android-listview baseadapter picasso