通过保持对中心元素的关注来滚动 VerticalGridView 或 RecyclerView

Scroll the VerticalGridView or RecyclerView by keeping focus on center element

通过将焦点保持在中心元素上来滚动 VerticalGridView。我正在实现一个包含 1 列的 verticalgridview,我想将焦点放在位于中心的单个项目上并移动 verticalgridview。I want to set focus on center object in vertical grid view and scroll the view by fixing the focus there

VerticalGridView 包含其内置的布局管理器,它正是这样做的。 不要 使用verticalGridView.setLayoutManager(new LinearLayoutManager()),这通常会用 RecyclerView 来做。默认情况下,VerticalGridView 的焦点项目始终位于中心 ,除非 项目位于 beginning/end,或者项目太少。

如果项目太少,可以使用verticalGridView.setWindowAlignment(WINDOW_ALIGN_NO_EDGE)。现在 beginning/end 处的项目也将位于中心。您也可以根据需要选择 WINDOW_ALIGN_LOW_EDGEWINDOW_ALIGN_HIGH_EDGE。默认为 WINDOW_ALIGN_BOTH_EDGE.

您也可以使用verticalGridView. setWindowAlignmentOffsetPercent (offsetPercent)手动确定焦点位置。如果offsetPercent为50,则焦点在中心;如果它是 90,它就在底部;如果是 10,则接近顶部,等等