【问题标题】:Picasso Images Reloading on Scroll in listview毕加索图像在列表视图中滚动时重新加载
【发布时间】: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


【解决方案1】:

当用户触发fling MotionEvent 时,应用程序正在下载图像,它需要停止,专注于滚动,然后在运动停止后立即重新下载图像。这样做的效果几乎是神奇的。

此外,Google 还提供了代码来展示它是如何完成的。你可以在这里找到它 - https://github.com/google/iosched

这里有一个关于如何添加滚动侦听器来停止和启动队列的快速 sn-p。

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
    }
});

【讨论】:

    【解决方案2】:
       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;
    }
    

    【讨论】:

    • 检查你的活动代码,因为 repting 问题不是来自 adapetr 我想你可能会在 android 中使用更多代码也发布活动代码
    • 我重复的意思是每次滚动列表视图时,毕加索都会再次加载图像。我想加载图像 1 次并让它出现而不再次加载图像。
    • 使用 lurchaech 在 sdcard 中加载图像而不是 goos approch
    【解决方案3】:

    我遇到了类似的问题,并结合了 Detecting the scrolling direction in the adapter (up/down)Fetch images with Callback in Picasso? 的提示。也就是说,我通过将适配器访问的最后一个位置与当前位置进行比较来检测用户是向上还是向下滚动,并将他们遇到的下一个图像加载到一个空目标中以预热缓存。

    【讨论】:

      【解决方案4】:

      对毕加索使用调整大小

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

      //这肯定有帮助

      【讨论】:

        猜你喜欢
        • 2015-03-02
        • 2017-08-31
        • 1970-01-01
        • 1970-01-01
        • 2014-09-06
        • 1970-01-01
        • 1970-01-01
        • 2015-12-17
        • 1970-01-01
        相关资源
        最近更新 更多