正确实现自定义适配器以使用Picasso显示图像

时间:2016-01-25 17:35:38

标签: android adapter picasso

我一直在用不同的方式多次编写和重写这个适配器,但是当我启动应用程序时,我得到的只是一个空白屏幕。

我想用picasso和一串网址作为基础数据将图像加载到gridview中,但我并没有真正推进它。

https://github.com/Valakias/PortFolioMovieApp

1 个答案:

答案 0 :(得分:1)

问题是imageView宽度为0(使用:Poster.getWidth();检查),这就是为什么你没有看到图像,自定义适配器就好了。

您可以使用setLayoutParams将宽度/高度设置为imageView,如下例所示:

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

使用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);

您可以使用.centerCrop()

代替.centerInside()
  • 使用 .centerCrop()

enter image description here

  • 使用 '.centerInside()'(这将尊重图像宽高比)。

enter image description here

相关问题