'Wrap Content grid view items with span count
I have a recyclerview with gridlayoutmanager having column span 4,
I am able to achieve below output
with following code
recyclerGridLayout.addItemDecoration(new GridSpacingItemDecoration(spanCount, rowSpacing, columnSpacing));
recyclerGridLayout.setNestedScrollingEnabled(false);
recyclerGridLayout.setLayoutManager(new GridLayoutManager(mContext, 3));
But is there any way i can get the below desired output

Where the space between items is fixed
Thank you
Solution 1:[1]
You can set 3 column span to 4. e.g.:
recyclerGridLayout.setLayoutManager(new GridLayoutManager(mContext, 4));
Solution 2:[2]
Solved using FlexBoxLayoutManager, To get fixed space between items and to get fix number of items per row or so to say to set spanCount like in gridlayoutManager, OnBindViewHolder, we do the following
FlexboxLayoutManager.LayoutParams layoutParams = (FlexboxLayoutManager.LayoutParams) itemView.getLayoutParams();
layoutParams.setWrapBefore(position != 0 && (position % 4 == 0)); //4 is spanCount
itemView.setLayoutParams(layoutParams);
And while configuring recyclerview, we use
FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(mContext, FlexDirection.ROW, FlexWrap.WRAP);
flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START);
recyclerGridLayout.setLayoutManager(flexboxLayoutManager);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Donald Duck |
| Solution 2 | barasingha |
