GridView库

时间:2015-06-23 16:52:00

标签: android gridview

我在GridView中的图片有些问题。我正在使用BaseAdapter在picasso库的帮助下填充它。 任何人都可以告诉我如何以这种方式填充GridView,如下所述:

https://lh5.googleusercontent.com/L-C8nVIw4RWb-5CN9L07qz7S91qE9lqyYjHSpqGm7CrrSzSnO1px8pJ32X7fsx4pS9bguFVTFWM00q_YXZxMmRYE20QJaADmgaSAqajJs9U8g9hT3EckA6KqZEGL0P-t3F0k9zk

(抱歉链接,但我无法上传图片)

图片已定义尺寸,我无法拉伸它。

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

1 个答案:

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