2

I am having a problem where any time I scroll through my listview,images seem to keep on reloading themselves and it makes the listview lag a lot. What can I do to prevent this from happening,I've done this in my listview before and it doesn't do this.

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;
    }
Rhynoboy2009
  • 140
  • 3
  • 11
  • add `Picasso.with(context).load(postList.get(position).postPicture).into(holder.image);` in you `if(convertView==null) {...}` remove from other place. – Rustam Oct 22 '14 at 06:02
  • public void add(PostList object,int position) { postList.add(position,object); } what is this why u use – Naveen Tamrakar Oct 22 '14 at 06:04
  • public PostsAdapter(Context context,ArrayList List) { this.context = context; postList = List; } use this cons in adapter – Naveen Tamrakar Oct 22 '14 at 06:05

4 Answers4

2

When a user fires a fling MotionEvent , application is downloading images, it needs to stop, concentrate of scrolling, and then get back to downloading images again as soon as motion stops. The effects of doing this is almost magical.

Also, Google gave out code to show how it's done. You can find it here - https://github.com/google/iosched

Here's a quick snippet of how do you add a scroll listener to stop and start the queue.

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
    }
});
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
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;
}
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

I had a similar issue, and combined the tips from Detecting the scrolling direction in the adapter (up/down) and Fetch images with Callback in Picasso?. That is, I detected whether the user was scrolling up or down by comparing the last position accessed by the adapter to the current position, and loaded the next images they would encounter into an empty target to warm the cache.

Community
  • 1
  • 1
zafirk
  • 79
  • 1
  • 1
0

Use resize with picasso

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

//This would definitely help

Akshay Sahai
  • 2,121
  • 1
  • 17
  • 20