简体   繁体   中英

Android Single Column Gridview

Am making a simple wallpaper app and i have some very high resolution images. i was wondering if it was possible to make a single column gridview where each image fit the width of the device 100%. an example of what i want is shown in the image below.

我希望GridView看起来像什么

Here is my xml code:

     <GridView
     android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:columnWidth="90dp"
     android:numColumns="auto_fit"
     android:verticalSpacing="10dp"
     android:horizontalSpacing="10dp"
     android:gravity="center"
     >
   </GridView>

Here is my java code for the images:

        // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        final int p = position;
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(2, 2, 2, 2);
        } else {
            imageView = (ImageView) convertView;
        }

You can set numCols to 1.

android:numColumns="1"

in

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:gravity="center"
    >
</GridView>

You can also refer to Gallery-like Single column Gridview . Hope this helps.

Try some thing like this:

  <GridView
        android:id="@+id/grdProducts"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
           />

Java file:

private GridView gridView;

    private int columnWidth;
    public static final int NUM_OF_COLUMNS = 1;
    public static final int GRID_PADDING = 5;


        gridView = (GridView)findViewById(R.id.grdProducts); 
        InitilizeGridLayout();

and the InitializeGridLayout()

 private void InitilizeGridLayout() {
            Resources r = getResources();
            float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    GRID_PADDING, r.getDisplayMetrics());

            columnWidth = (int) (((getScreenWidth()) - ((NUM_OF_COLUMNS + 1) * padding)) / NUM_OF_COLUMNS);

            gridView.setNumColumns(NUM_OF_COLUMNS);
            gridView.setColumnWidth(columnWidth);
            gridView.setStretchMode(GridView.NO_STRETCH);
            gridView.setPadding((int) padding, (int) padding, (int) padding,(int) padding);
            gridView.setHorizontalSpacing((int) padding);
            gridView.setVerticalSpacing((int) padding);
        }
        @SuppressLint("NewApi")
        public int getScreenWidth() {
            int columnWidth ;
            WindowManager wm = (WindowManager) this
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();

            final Point point = new Point();
            try {
                display.getSize(point);
            } catch (java.lang.NoSuchMethodError ignore) { // Older device
                point.x = display.getWidth();
                point.y = display.getHeight();
            }                              
            columnWidth = point.x;
            return columnWidth;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM