0

Currently I am trying to use GridView to create a list of images with a textview beneath them, the layout should fit as many of them width wise as possible.

When using GridView with:

<GridView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/gridView"
    android:numColumns="auto_fit"
    android:layout_gravity="center_horizontal|top"
    android:clickable="false"
    />

The expected behaviour would be that the columns would automatically wrap the content and fit as many items width wise as possible?

The textview inside the grid item layout are set to wrap_content except the ImageView, it has a static width of 75dip.

I am seeing the following layout with this code:

XX

XX

XX

If the column width is specifically set, I can easily get:

XXXX

XXXX

XXXX

JamieB
  • 923
  • 9
  • 30
  • gridView coloumn width should fit with max size content inside gridview's coloumn if found and followed it with rest of content . – Tanim reja May 10 '14 at 12:07
  • The only way to determine the max width of your cell content is to instantiate and populate all cells in your grid and measure them, which is a very heavy operation and basically makes the views virtualization (only drawing what's visible) useless. – BladeCoder May 10 '14 at 12:16

2 Answers2

1

i think that what you should do is to measure the column width for gridview with

public int measureCellWidth( Context context, View cell )
{

    // We need a fake parent
    FrameLayout buffer = new FrameLayout( context );
    android.widget.AbsListView.LayoutParams layoutParams = new  android.widget.AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    buffer.addView( cell, layoutParams);

    cell.forceLayout();
    cell.measure(1000, 1000);

    int width = cell.getMeasuredWidth();

    buffer.removeAllViews();

    return width;
}

and then set it with

gridView.setColumnWidth(width);

also you should read this Android: How does GridView auto_fit find the number of columns?

Community
  • 1
  • 1
Matej Špilár
  • 2,617
  • 3
  • 15
  • 28
  • Negligible, the user does not have a chance to notice anything, even on weaker phones. You should really try it both with and without and see the result yourself :) – Matej Špilár May 10 '14 at 12:06
  • This only measures the width of one view. I think that JamieB wants to measure all views to determine the max width; in that case the performance is far from negligible with many items. – BladeCoder May 10 '14 at 12:18
1

If you set android:numColumns to auto_fit, you also need to set android:columnWidth so that the GridView is able to compute how many items can fit. The GridView will not create any item view before it knows how many of them it can fit in a row, and it will not measure the item views.

You should always be able to determine the width of your items in advance if you're using auto_fit. If you're not, it means that your items may have a different width and in that case your items will not render properly because columns in a GridView always have the same size.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51