Staggered Grid Layout Manager that works in both orientations

1.2k views Asked by At

What I'm trying to do:

I'm trying to create a staggered grid layout that takes items with varying width and height. The layout dynamically moves items around so they don't overlap.

What I have tried:

I looked into using the default StaggeredGridLayoutManager, but it appears to only work in one orientation (i.e. Vertical or Horizontal). Furthermore, every library or tutorial I've come across also only deals with items containing either a dynamic height or dynamic width, but not both.

Is there a way to make the StaggeredGridLayoutManager dynamically span both vertically and horizontally? Or will I have to create my own custom layout manager? If the latter, could someone point me in the right direction to learn about how one would create such a layout manager?

1

There are 1 answers

2
Varsha Ravikumar On

Instead of StaggeredGridLayoutManager, GridLayoutManagercan be used with spanSizeLookup. Using spanSizeLookup, we can specify the column span.

mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
 item[position].columnSpan
}
}

While creating the GridLayoutManager , specify the max no of columns like this

layoutManager = GridLayoutManager(context, MAX_NUM_COL)

The row span can be applied by calculating the height using the spanning factor and height of the parent