问题描述
限时送ChatGPT账号..我正在尝试构建我自己的网格视图函数 - 在 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.
注意:
希望对您有所帮助并给您一个想法.
Hope that helps and gives you an idea.
这篇关于如何从 GridView 获取滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论