我在GridView中的图片有些问题。我正在使用BaseAdapter在picasso库的帮助下填充它。 任何人都可以告诉我如何以这种方式填充GridView,如下所述:
(抱歉链接,但我无法上传图片)
图片已定义尺寸,我无法拉伸它。
private class GridAdapter extends BaseAdapter{
Context mContext;
private ArrayList<String> data;
private GridAdapter(ArrayList<String> list, Context mContext) {
data=list;
this.mContext = mContext;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView == null){
imageView = new ImageView(mContext);
}
else {
imageView = (ImageView) convertView;
}
Picasso.with(mContext).load("http://image.tmdb.org/t/p/w185/" + data.get(position)).into(imageView);
return imageView;
}
}
答案 0 :(得分:0)
强制拥有2列
<GridView
android:id="@+id/gridviewGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
...
/>
并在适配器中,计算屏幕的宽度并除以2,并修改图像的imageView.setLayoutParams
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth()/2 ;
int height = width *1,5 ; // proportional to your image
imageView.setLayoutParams(new GridView.LayoutParams(width, height));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);