Scroll the VerticalGridView by keeping focus on center element. I am implementing a verticalgridview with 1 column count and I want to keep the focus on a single item which is in center and move the verticalgridview.I want to set focus on center object in vertical grid view and scroll the view by fixing the focus there
VerticalGridView contains its built-in layout manager which does exactly that. Do NOT use verticalGridView.setLayoutManager(new LinearLayoutManager())
, which one would usually do with RecyclerView. By default the focused item of a VerticalGridView is always positioned at the center unless the item is at the beginning/end, or there are too few items.
If there are too few items, you can use verticalGridView.setWindowAlignment(WINDOW_ALIGN_NO_EDGE)
. Now the items at the beginning/end will also be positioned at the center. You can also choose WINDOW_ALIGN_LOW_EDGE
or WINDOW_ALIGN_HIGH_EDGE
depending on your needs. The default is WINDOW_ALIGN_BOTH_EDGE
.
You can also use verticalGridView. setWindowAlignmentOffsetPercent (offsetPercent)
to manually determine the focus position. If offsetPercent
is 50, the focus is at the center; if it's 90, it's towards the bottom; if it's 10, it' near the top, etc.