4

I am learning GridView. As you can see from the screenshot below, I have added 13 buttons with various length strings as their text. However long text buttons overlap each other, I tried but I couldn't fix it. All I want is that each row has the same height of the most high element, for instance all the buttons in row 1 should have the "lorem ipsum dolor sit amet" s height, and all the other rows should have their longest elements height. How can I achieve this?

If it is not possible, alternatively I can have the following: All the elements are in their size, however the grid is not actually a rectangle, it can have the following scheme: (where there is actually no grid, it just appends the Views together in columns, without considering where is the acutal grid.

------------------------------------------------|
|              |       2       |                |
|      1       |----------------      3         |
|              |               |----------------|
---------------|       5       |                |
|        4     |               |      6         |
|------------------------------|-----------------

My Gridview:

<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="fill_vertical"
    android:numColumns="3"
    >
</GridView>

getView method of my adapter class that extens BaseAdapter:

    public View getView(int position, View convertView, ViewGroup arg2) {
        Button btn = null;
        if (convertView == null) {
            btn = new Button(mContext);
        } else {
            btn = (Button) convertView;
        }
        btn.setText(txts[position]);
        return btn;
    }

Note: I will have my custom view, I am just testing it with simple buttons.

Veger
  • 37,240
  • 11
  • 105
  • 116
Mustafa
  • 10,013
  • 10
  • 70
  • 116
  • Seems like the best approach is to use multiple listviews and put them into gridview and intelligently fill them – Mustafa Jan 15 '13 at 21:18
  • I have the same issue, but, it's me or "use multiple listviews and put them into gridview" seems to be a bit overkill? Is it possible that we don't have another solution? – dentex Apr 15 '14 at 10:08
  • @dentex you are right, it might be better to use something like that: http://www.androidviews.net/2014/01/androidstaggeredgrid-view/ – Mustafa Apr 15 '14 at 11:19
  • you should look at http://stackoverflow.com/a/2379461/1865860 and at http://stackoverflow.com/a/13994344/1865860 for two possible solutions to similar questions – dentex Apr 15 '14 at 13:22

1 Answers1

1

What you are asking is not what GridView is capable of. You might want to modify onMeasure in a custom Gridview.

On the other hand, it seems what you want to achieve can be done by using scrollview and linearlayout (because in your mock, the columns are aligned).

Check out this post: Android heterogeneous gridview like pinterest

Community
  • 1
  • 1
Ian Wong
  • 1,617
  • 14
  • 11