Android 在listview中滚动重新加载毕加索图像
我遇到了一个问题,每当我滚动浏览我的listview时,图像似乎总是不断地重新加载,这使得listview滞后了很多。 我可以做些什么来防止这种情况发生,我以前在listview中做过,但它没有这样做Android 在listview中滚动重新加载毕加索图像,android,listview,android-listview,baseadapter,picasso,Android,Listview,Android Listview,Baseadapter,Picasso,我遇到了一个问题,每当我滚动浏览我的listview时,图像似乎总是不断地重新加载,这使得listview滞后了很多。 我可以做些什么来防止这种情况发生,我以前在listview中做过,但它没有这样做 public class PostsAdapter extends BaseAdapter{ public List<PostList> postList; protected Context context; public void add(PostL
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;
}
公共类PostsAdapter扩展BaseAdapter{
公开名单张贴名单;
受保护的语境;
公共void add(PostList对象,int位置){
添加(位置、对象);
}
公共PostsAdapter(上下文){
this.context=上下文;
postList=newarraylist();
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回postList.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回postList.get(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视窗座;
if(convertView==null){
convertView=LayoutInflater.from(context).充气(R.layout.posts\u list,null);
holder=新的ViewHolder();
holder.image=(ImageView)convertView.findViewById(R.id.postImage);
holder.username=(TextView)convertView.findViewById(R.id.posterName);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.username.setText(postList.get(position.user));
毕加索.with(context).load(postList.get(position.postPicture).into(holder.image);
返回视图;
}
静态类视窗夹{
图像视图图像;
文本视图用户名;
}
公共类PostsAdapter扩展BaseAdapter
{
公开名单张贴名单;
受保护的语境;
公共PostsAdapter(上下文,数组列表)
{
this.context=上下文;
postList=列表;
}
@凌驾
public int getCount()
{
//TODO自动生成的方法存根
返回postList.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回postList.get(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视窗座;
if(convertView==null){
convertView=LayoutInflater.from(context).充气(R.layout.posts\u list,null);
holder=新的ViewHolder();
holder.image=(ImageView)convertView.findViewById(R.id.postImage);
holder.username=(TextView)convertView.findViewById(R.id.posterName);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.username.setText(postList.get(position.user));
毕加索.with(context).load(postList.get(position.postPicture).into(holder.image);
返回视图;
}
静态类视窗夹
{
图像视图图像;
文本视图用户名;
}
当用户触发fling MotionEvent时,应用程序正在下载图像,它需要停止、集中滚动,然后在运动停止后重新下载图像。这样做的效果几乎是神奇的
此外,谷歌还发布了代码来展示它是如何完成的。你可以在这里找到它-
下面是如何添加滚动侦听器以停止和启动队列的快速片段
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
}
});
我有一个类似的问题,并结合了和的提示。也就是说,我通过比较适配器访问的最后一个位置和当前位置来检测用户是否在向上或向下滚动,并将他们将遇到的下一个图像加载到空目标中以加热缓存。使用毕加索的调整大小
Picasso.with(context)
.load(imageURL)
.tag(context)
.placeholder(R.drawable.kanye8080s)
.error(R.drawable.stadiumarcadium)
.into(imageView)
.resize(x,y);
//这绝对有助于add
Picasso.with(context).load(postList.get(position.postPicture).into(holder.image);
if(convertView==null){…}remove from other place.public void add(postList object,int position){postList.add(position,object);}这就是为什么要使用public PostsAdapter(上下文,数组列表){this.Context=Context;postList=List;}在Adapter中使用此缺点检查你的活动代码,因为报告问题不是来自adapetr,我想你可能会在android中使用更多代码,也可以使用后期活动代码。我所说的重复是指每次我滚动列表视图时,毕加索都会再次加载图像。我想加载图像1次,并让它在不再次加载图像的情况下显示。使用lurchaech将图像加载到SD卡中,而不是goos方法
Picasso.with(context)
.load(imageURL)
.tag(context)
.placeholder(R.drawable.kanye8080s)
.error(R.drawable.stadiumarcadium)
.into(imageView)
.resize(x,y);