我遇到问题,无论何时我滚动浏览列表视图,图像似乎都在继续重新加载,这使得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 :(得分: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
}
});
答案 1 :(得分: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;
}
答案 2 :(得分:0)
我遇到了类似的问题,并结合了Detecting the scrolling direction in the adapter (up/down)和Fetch images with Callback in Picasso?的提示。也就是说,我通过将适配器访问的最后位置与当前位置进行比较来检测用户是向上还是向下滚动,并将他们将遇到的下一个图像加载到空目标中以加热缓存。
答案 3 :(得分:0)
将调整大小与picasso
一起使用 Picasso.with(context)
.load(imageURL)
.tag(context)
.placeholder(R.drawable.kanye8080s)
.error(R.drawable.stadiumarcadium)
.into(imageView)
.resize(x,y);
//这肯定有帮助