I have a ListView which keeps refreshing images each time I scroll up and down
I am picking image urls from JSON which I supply to Picasso.
I am using Picasso to load images.
The total size of the images all together is 516kb, knowing that, I'm not thinking its a cache problem because image sizes are actually small.
Honestly, I have researched over this issue on S.O, and not seen any good solution yet.
Maybe the issue isn't related to Picasso, maybe my implementation is actually wrong. Please I need more knowledge on this.
This is my code below.
public class ImageAdapter extends ArrayAdapter<Genres> {
ArrayList<Genres> movieList;
TextView movietitle, moviecategory;
LayoutInflater vi;
int Resource;
ViewHolder holder;
private Context mContext;
public ImageAdapter(Context context, int resource, ArrayList<Genres> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
movieList = objects;
mContext = context;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ImageView imageView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.movieimage);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(96, 128);
holder.imageview.setLayoutParams(layoutParams);
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageview.setPadding(4, 4, 4, 4);
holder.imageview.setAdjustViewBounds(true);
holder.movietitle = (TextView) v.findViewById(R.id.movietitle);
holder.moviecategory = (TextView) v.findViewById(R.id.moviecategory);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
Picasso.with(mContext)
.load(movieList.get(position).getImage())
.noFade()
.into(holder.imageview);
holder.movietitle.setText(movieList.get(position).getMoviename());
holder.moviecategory.setText(movieList.get(position).getCategory());
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView movietitle;
public TextView moviecategory;
}
}
I think the problem that you have here is that you made your holder object an instance variable of of your adapter class, so in effect you have only a single view holder.
Try removing your instance variable declaration
and then make both the new view holder and the one returned by the
getTag()
local variables.
So these two changes:
first:
and then in your else clause :
Now the holder stored by the layout will be different views rather than the one and only one that you currently have.
As a reference here is an adapter that I use with Picasso. There are several differenced but I think the main thing is your use of a single instance variable for the viewHolder variable.