我正在尝试构建自己的 GridView 函数 - 在 GridView
上进行扩展。
我唯一无法解决的是如何获取 GridView
的当前滚动位置。
getScrollY()
总是返回 0,onScrollListener
的参数只是可见 subview 的范围,而不是实际的滚动位置。
这似乎不是很困难,但我只是在网上找不到解决方案。
这里有人有想法吗?
最佳答案
我没有找到任何好的解决方案, 但是这个至少能够保持像素完美的滚动位置:
int offset = (int)(<your vertical spacing in dp> * getResources().getDisplayMetrics().density);
int index = mGrid.getFirstVisiblePosition();
final View first = container.getChildAt(0);
if (null != first) {
offset -= first.getTop();
}
// Destroy the position through rotation or whatever here!
mGrid.setSelection(index);
mGrid.scrollBy(0, offset);
这样你不能得到一个绝对的滚动位置,而是一个可见的项目+位移对。
注意事项:
- 这适用于 API 8+。
- 您可以在 API 16+ 中使用 mGrid.getVerticalSpacing()。
- 您可以在 API 11+ 中使用 mGrid.smoothScrollToPositionFromTop(index, offset) 而不是最后两行。
希望对您有所帮助并给您一个想法。
关于android - 如何从 GridView 获取滚动位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6125013/