2014-10-22 114 views
2

我有一个问题,当我滚动我的列表视图,图像似乎继续重新加载自己,它使得列表视图滞后很多。 我能做些什么来防止这种情况发生,我之前在我的listview中做过这件事,而且它没有这样做。毕加索图像重新加载滚动在列表视图

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

加上'Picasso.with(上下文).load(postList.get(位置).postPicture).into(架.image);'in you'if(convertView == null){...}'从其他地方移除。 – Rustam 2014-10-22 06:02:35

+0

public void add(PostList对象,int位置){ postList.add(position,object); }这就是为什么你使用 – 2014-10-22 06:04:26

+0

public PostsAdapter(Context context,ArrayList List){ this.context = context; postList = List; }在适配器中使用此缺点 – 2014-10-22 06:05:20

回答

0
public class PostsAdapter extends BaseAdapter 
    { 
     public List<PostList> postList; 
     protected Context context; 

    public PostsAdapter(Context context,ArrayList<PostList> List) 
    { 
     this.context = context; 
     postList = List; 
    } 

    @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; 
} 
+0

这仍然不起作用。 – Rhynoboy2009 2014-10-22 06:50:59

+0

检查yor活动代码becouse repting问题不是来自adapetr我想你可能会使用更多的代码在android也发布活动代码 – 2014-10-22 06:52:49

+0

我的意思是重复每次我滚动浏览列表视图,毕加索再次加载图像。我想要加载图像1次,并在不加载图像的情况下出现。 – Rhynoboy2009 2014-10-22 06:54:10

2

当用户触发一扔MotionEvent,应用程序下载图像,它需要停止滚动的浓缩,然后回到尽快停止运动再次下载图像。这样做的效果几乎是神奇的。

此外,谷歌给出了代码,以显示它是如何完成的。你可以在这里找到它 - https://github.com/google/iosched

下面是如何添加一个滚动监听器来停止和启动队列的快速片段。

listView.setOnScrollListener(new OnScrollListener() { 
    @Override 
    public void onScrollStateChanged(AbsListView listView, int scrollState) { 
     // Pause disk cache access to ensure smoother scrolling 
     if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) { 
      imageLoader.stopProcessingQueue(); 
     } else { 
      imageLoader.startProcessingQueue(); 
     } 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
      // TODO Auto-generated method stub 
    } 
}); 
0

使用调整与毕加索

    Picasso.with(context) 
       .load(imageURL) 
       .tag(context) 
       .placeholder(R.drawable.kanye8080s) 
       .error(R.drawable.stadiumarcadium) 
       .into(imageView) 
       .resize(x,y); 

//这绝对有帮助