flexbox-layout icon indicating copy to clipboard operation
flexbox-layout copied to clipboard

Limit number of items per row/column?

Open sadr0b0t opened this issue 5 years ago • 5 comments

Hello, I am trying to use flexbox-layout with RecyclerView to scroll list with dozens of elements (icons with text). It works mostly fine, but elements would fill the whole available space on screen, but I want to limit number of elements per row (when scroll down) or number of elements per column (when scroll to the right).

I can achieve same thing with GridLayoutManager:

final GridLayoutManager gridLayoutManager = new GridLayoutManager(
    getApplicationContext(), 2, GridLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(gridLayoutManager);

but grid layout manager does not allow me to align items by center.

Can I do this with FlexBoxLaout?

sadr0b0t avatar Nov 17 '19 12:11 sadr0b0t

Any progress on it?

P1NG2WIN avatar May 11 '21 20:05 P1NG2WIN

Any progress on it?

pavel0001 avatar Mar 17 '22 15:03 pavel0001

Any progress on it?

ramseth001 avatar May 25 '22 03:05 ramseth001

You can use layout_wrapBefore attribute for this I guess. This can be done by sub-classing the FlexboxLayoutManager class.

@Override
public View getFlexItemAt(int index) {
    View item = super.getFlexItemAt(index);

    FlexboxLayoutManager.LayoutParams params =
            (FlexboxLayoutManager.LayoutParams) item.getLayoutParams();

    params.setWrapBefore(index % maxPerRow == 0);
    return view;
}

This is a hack, but I don't see any other solution at this point.

image

image

sriharshachilakapati avatar Jun 30 '22 19:06 sriharshachilakapati

You can use layout_wrapBefore attribute for this I guess. This can be done by sub-classing the FlexboxLayoutManager class.

@Override
public View getFlexItemAt(int index) {
    View item = super.getFlexItemAt(index);

    FlexboxLayoutManager.LayoutParams params =
            (FlexboxLayoutManager.LayoutParams) item.getLayoutParams();

    params.setWrapBefore(index % maxPerRow == 0);
    return view;
}

This is a hack, but I don't see any other solution at this point.

image

image

thank you, good answer

doanvu2000 avatar Nov 28 '23 07:11 doanvu2000