滚动 gridview,使位置位于准确的中心

Scroll gridview so a position is at the exact center

我正在制作一个 android 应用程序,它使用 GridView。我制作了一个搜索按钮,以便人们可以转到 GridView 中的特定位置。 我目前正在使用:

gridview.smoothScrollToPosition(index);

但是在GridView.
的中间位置并没有居中 这就是我想要的插图:

实际上,问题在于 GridView 从正确项目可见的那一刻起就停止滚动。这意味着它在向上或向下滚动时会有不同的位置。

这可能是这个问题的重复: android: smoothScrollToPosition() not working correctly

我以前在使用列表视图时遇到过这样的问题,我使用 setSelection() 来解决它。

我有类似的问题,所以我手动计算了它。 v 是要居中的视图:

   Rect rect = new Rect();
   mMediaGridView.getGlobalVisibleRect(rect);

   mMediaGridView.smoothScrollBy(v.getBottom()  - mMediaGridView.getScrollY()-rect.centerY()+v.getHeight()/2, 200);