Divide width between two columns in GridLayoutManager
Asked Answered
A

5

8

I used GridLayoutManager as my layout manger for recycler view.

  mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

the result is like below: enter image description here

but I want to divide width between two columns equally. how can I do that?

Anomalism answered 21/12, 2017 at 7:46 Comment(0)
A
10

Try adding this on your recyclerView:

mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int spanCount = 2;
            int spacing = 10;//spacing between views in grid

            if (position >= 0) {
                int column = position % spanCount; // item column

                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = 0;
                outRect.right = 0;
                outRect.top = 0;
                outRect.bottom = 0;
            }
        }
    });
Aramanta answered 21/12, 2017 at 7:52 Comment(0)
H
3

Setting the column count like you do:

mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

... actually makes the grid splitting into columns of the same width.

This suggests that the problem lies somewhere else, probably in the item layout. I suppose that some whitespaces (paddings/margins) are responsible for the fact that the items are shifted to the right. Try to remove these spacings and check if it works.

If it helps, you can always add new spacing with ItemDecoration but it isn't the reason for the incorrect size of your columns.

I think that turning on Show Layout Bounds option may be helpful for you.

Honan answered 21/12, 2017 at 11:19 Comment(1)
there is no gap between columns in your solution almost uselessEndoergic
A
2

I wrote a library for equal spacing which supports Vertical/Horizontal, LTR/RTL, LinearLayout/GridLayout manager and Edge inclusion. Its basically a single file, so you can copy paste that file into your code.

enter image description here

I tried to support StaggeredGridLayout but span index returned by this layout is not reliable. I would be glad to hear any suggestion for that.

Alwitt answered 19/12, 2020 at 9:5 Comment(0)
S
1

You can use custom ItemDecoration and you can split the space as you want :

SpacesItemDecoration can help you with this:

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private int space;

    public SpacesItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {

        outRect.bottom = space;

        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view)%2 == 0) {
            outRect.left = space;
            outRect.right = space;
        } else {
            outRect.right = space;
        }
    }
}

In your acitivty or fragment :

int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
        spacesItemDecoration = new SpacesItemDecoration(spacingInPixels);

      recyclerview.addItemDecoration(spacesItemDecoration);

You can change the outRect.left & outRect.Right as per your need

Sloven answered 21/12, 2017 at 7:58 Comment(1)
Yes but it has some changes if you can seeSloven
M
0

You can use ItemDecoration for this purpose,have a look at here

And there are some very good answers here also

Monocle answered 21/12, 2017 at 7:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.