I was looking at the documentation of GridLayoutManager and its SpanSizeLookUp Class for RecyclerView. But I cant get to know about the getSpanGroupIndex and getSpanIndex methods of the SpanSizeLookUp Class. I also experimented by overriding and returning arbitary number(less than span count). But I cant get to know what is it used for.
I am not able to understand the Documentation instructions. I would like to clear this.
By default, each item in a grid layout takes 1 span. If Grid is horizontal, span means a row and if Grid is vertical, span means a column. If your items may need to layout themselves into multiple spans (e.g. a header item in a 3 column grid would have spanSize=3), you can provide a SpanSizeLookup which returns 3 if item is a header item and 1 if it is a regular item.
When GridLayoutManager (GLM) needs to decide the span for an item, it calls getSpanIndex in SpanSizeLookup. That method traverses every item before the given position to decide which span this item should be placed at.
For example, if you call
glm.scrollToPosition(n)
, SpanSizeLookup goes through every item from 0 to N, checks how many spans each of them consume and decide where the item atN
would be positioned. Lets say you have a grid w/ a header row at index 0 and 3, and rest of the items take 1 span. If GLM callsspanSizeLookup.getSpanIndex(4)
it will callgetSpanIndex
for0 1 2 3
, realize that after 3rd item, next item should be placed at span 0 and return it.Although it caches these results, it is inefficient to traverse all items if you can calculate it faster. For instance, maybe you know that all items take 1 span, then you can override
getSpanIndex
and returnn % spanCount
. This is why that method is public.getSpanGroupIndex
is the group of the item. If GLM is vertical, group is "row" and if GLM is horizontal, group is "column". It is used mainly for accessibility. For the example above where each item consumes 1 span, you can override this method and returnn / spanCount
. I agree that this method is not clear, will create a ticket to improve documentation.