如何从GridView中获取滚动位置? [英] How to get scroll position from GridView?
问题描述
我试图建立自己的网格视图功能 - 对的GridView
延伸。
我解决不了的唯一的事情是如何得到的当前滚动位置的GridView
。
I am trying to build my own grid view functions - extending on the GridView
.
The only thing I cannot solve is how to get the current scroll position of the GridView
.
getScrollY()
并始终返回0,而 onScrollListener
的参数只是一系列可见子意见,而不是实际的滚动位置。
getScrollY()
does always return 0, and the onScrollListener
's parameters are just a range of visible child views, not the actual scroll position.
这似乎并不很困难,但我无法找到在网络的解决方案。
This does not seem very difficult, but I just can't find a solution in the web.
要是谁有一个想法?
推荐答案
我没有找到什么好的解决办法, 但是这一次至少能够保持滚动位置的那种像素完美的:
I did not find any good solution, but this one is at least able to maintain the scroll position kind of pixel-perfectly:
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);
到那个你不能得到绝对的滚动位置,但一个明显的项目+位移对。
By that you can not get an absolute scroll position, but a visible item + displacement pair.
注:
- 这是为API 8 +。
- 您可以在API 16 +与mGrid.getVerticalSpacing()得到的。
- 您可以使用mGrid.smoothScrollToPositionFromTop(索引,偏移量)的API 11+,而不是最后两行。
希望有所帮助,给你出个主意。
Hope that helps and gives you an idea.
这篇关于如何从GridView中获取滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!