BomberBus
BomberBus

Reputation: 1235

Difference Horizontal Spacing in Android GridView

I would like to implement the UI like the below image.

enter image description here

I layout the UI using GridView and I got only the result like the below image with the same horizontal spacing.

enter image description here

I would like to know that does Android GridView support difference spacing size between grid item? If there is any solution or third party lib, please help. Thank you. Sorry for my poor in english.

Edit:

According to sgadde and Gary Bak's answers, I calculate and add right margin to items in second column in adapter's getView method with the following code.

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 20, 0);
        viewHolder.numberTextView.setLayoutParams(params);

and I got the following result.

enter image description here

My code to add margin of grid item shrinked the size of grid item. Is something wrong? Please help how to add spacing. Thank you.

Upvotes: 0

Views: 346

Answers (3)

Hein Htet Aung
Hein Htet Aung

Reputation: 854

My answer will be a little tricky.

First you need to reduce horizontalSpacing to adjust to be square.

Now, you need to add margin for all columns.

There is four columns, and so

column 1: left margin

column 2: right margin

column 3: left margin

column 4: right margin

This will be like what you want.

Upvotes: 0

Gary Bak
Gary Bak

Reputation: 4798

In your adapter's getView method you have the position, when you hit the position for 2, 6, 10, etc.. then you can add addition spacing on the right.

@Override
public View getView(int position, View convertView, ViewGroup parent) {

Upvotes: 1

sgadde
sgadde

Reputation: 51

You should calculate and add right margin to items in second column, left margin to items in third column dynamically. You can do it in getView() of your adapter.

Upvotes: 1

Related Questions