Android ListView 当前滚动位置 Y 像素 [英] Android ListView current scroll location Y pixels
问题描述
我试图检测列表视图何时滚动超过以像素为单位的特定固定阈值(第一项的一半).不幸的是,listview 的 getScrollY() 似乎总是返回滚动位置的 0 instad.有没有办法按像素获取实际滚动位置?
I'm trying to detect when a list view is scrolled beyond certain fixed threshold in pixels (half way through the first item). Unfortunately listview's getScrollY() seems to always return 0 instad of the scroll position. Is there any way to get the actual scroll location by pixel?
这是我尝试使用的代码,但如上所述它只返回 0.
Here's the code I tried to use but as said it only returns 0.
getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.d("scroll", "scroll: " + getListView().getScrollY());
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == 0)
Log.d("scroll", "scrolling stopped");
}
});
推荐答案
Android 中的 ListView 没有 Y 滚动的概念,因为内容的总高度是未知的.只有显示内容的高度是已知的.
There is no notion of Y scroll for a ListView in Android simply because the total height of the content is unknown. Only the height of the displayed content is known.
但是,可以使用以下技巧获取可见项目的当前位置/Y 滚动:
However it is possible to get the current position/Y scroll of a visible item using the following hack:
getListView().getChildAt(0).getTop();
这篇关于Android ListView 当前滚动位置 Y 像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!