Android 正确实现自定义适配器以使用毕加索显示图像

Android 正确实现自定义适配器以使用毕加索显示图像,android,adapter,picasso,Android,Adapter,Picasso,我已经多次以不同的方式编写和重写这个适配器,但我得到的唯一结果是启动应用程序时出现了一个空白屏幕 我想使用毕加索和一串URL作为基础数据,将图像加载到gridview中,但我并不打算继续 问题在于imageView宽度为0(使用:Poster.getWidth();检查),这就是为什么您看不到图像的原因,自定义适配器没有问题 您可以通过使用setLayoutParams设置imageView的宽度/高度来解决此问题,如下例所示: if (convertView == null) { Lay

我已经多次以不同的方式编写和重写这个适配器,但我得到的唯一结果是启动应用程序时出现了一个空白屏幕

我想使用毕加索和一串URL作为基础数据,将图像加载到gridview中,但我并不打算继续


问题在于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);
或者使用毕加索的不同方法。调整大小

//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);
您可以使用
.centerInside()

  • 使用
    .centerCrop()

  • 使用
    '.centerInside()'
    (这将考虑图像纵横比)

你可以看看这个。是的,就是它!我认为通过将imageview的大小设置为与父对象匹配,可以让gridview决定大小,但我似乎错了。非常感谢,先生,我被困在这一点上了,没有教程真正解释这一点,所以我真的迷路了。当在gridView/listView中时,imageview的大小发生了一些奇怪的变化(我仍然不知道为什么它的行为与任何其他视图不同),但我很高兴它为您工作,只是出于好奇,你在寻找两个选项中的哪一个,centerInside还是centerCrop?真奇怪!所以奇怪的消息来源应该提到这一点:/我目前正在学习Udacity Nanodegree课程,所以最终我将不得不展示海报电影列表,所以我会选择看起来更好的阶梯。在我使用这些类之前,我真的需要阅读更多关于这些类的文档,这会让我和其他人省去一些麻烦。