Listview lags behind even when saving images with Picasso
I have a list view that contains images and when I scroll past a certain point the delays are scrolling.
I use the Picasso
following code as well to make sure the images are loaded into memory on a background thread.
picasso
.load(f)
.fit()
.error(R.drawable.broken_image)
.into(viewHolder.imageView, loadDoneCallback);
If all my entries ListView
are loading the same image, there is no lag, so the problem is with loading the image from memory into viewHolder.imageView
.
Another data point is that I see lagging on my device O + O with Android 4.4.4
. However, my Nexus 5 running under Android 5.1
doesn't seem to have this issue. Both seem to be using a heap size of around 128MB +. At ~ 40KB per thumb and 3 thumbs per line and a total of 50 lines (I see a lag as I go past the first 10 lines), my total in-memory image size should be ~ 6 MB and all images should be included into the cache (even though the download is done on a background thread anyway).
When I repeated the problem several times by scrolling up and down, I was able to get the message Skipped x frames
.
06-03 22:24:30.279 2382-2382/xxx I/Choreographer﹕ Skipped 58 frames! The application may be doing too much work on its main thread.
Again, I am assuming that this work, which is happening on the main thread, has to do with the individual images being loaded Picasso
from their cache into ImageView
. I'm guessing it can't be related to other layout related calculations etc., since using the same image for all posts ListView
works great.
Is this expected or am I making a rookie mistake?
UPDATE: this is my code getView
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder = new ViewHolder();
final String imageId = getItem(position);
if (convertView == null) {
view = inflator.inflate(R.layout.image_row_home, parent, false);
viewHolder.imageView = (ImageView) view.findViewById(R.id.img);
viewHolder.imageView.getLayoutParams().height = imageWidth;
viewHolder.imageView.getLayoutParams().width = imageWidth;
viewHolder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
viewHolder.imageId = imageId;
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = ((ViewHolder) view.getTag());
// If this view is already for the image we want, no need to recreate it.
if (viewHolder.imageId.equals(imageId)) {
return view;
}
viewHolder.imageId = imageId;
}
final File f = getImageFileForPosition(position);
picasso
.load(f)
.fit()
.error(R.drawable.broken_image)
.into(viewHolder.imageView);
}
source to share
No one has answered this question yet
Check out similar questions: