Valerio
Valerio

Reputation: 13

Properly implement Custom Adapter to show images with Picasso

I have been writing and rewriting this adapter many times in different ways, but only thing I get is a blank screen when I launch the app.

I want to load images into a gridview using picasso and a string of urls as base data, but I'm not really going forward with it.

https://github.com/Valakias/PortFolioMovieApp

Upvotes: 1

Views: 293

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40434

The problem is that the imageView width is 0 (check it using: Poster.getWidth();), that's why you're not seeing the images, the Custom Adapter is fine.

You can fix this, using setLayoutParams to set a width/height to the imageView as the example below:

if (convertView == null) {
   LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertView = inflater.inflate(R.layout.gridview_item, null);

   //Get screen size, and divide it by the number of columns of your grid view.
   int width = getContext().getResources().getDisplayMetrics().widthPixels / 2;
   ((ImageView) convertView.findViewById(R.id.gridview_item_image)).setLayoutParams(new GridView.LayoutParams(width, width));
}

ImageView Poster = (ImageView) convertView.findViewById(R.id.gridview_item_image);
Picasso.with(getContext())
       .load(getItem(position))
       .fit()
       .centerCrop()
       .into(Poster);

Or a different approach using Picasso.resize

//Get screen size, and divide it by the number of columns of your grid view.
int width = getContext().getResources().getDisplayMetrics().widthPixels / 2;
ImageView Poster = (ImageView) convertView.findViewById(R.id.gridview_item_image);

Picasso.with(getContext())
    .load(getItem(position))     
    .resize(width, width)
    .centerCrop()
    .into(Poster);

Instead of .centerCrop() you can use .centerInside()

  • Using .centerCrop()

enter image description here

  • Using '.centerInside()' (This will respect image aspect ratio).

enter image description here

Upvotes: 1

Related Questions