如何检查,如果我的ListView有项目滚动多少? [英] How to check if a my ListView has scrollable number of items?
问题描述
你怎么知道你的ListView的项目有足够的数量,以便它可以滚动?
How do you know if your ListView has enough number of items so that it can scroll?
例如,如果我有我的ListView 5项这一切将在一个屏幕上显示。但是,如果我有7个或更多,我的ListView开始滚动。我怎么知道我的列表可以以编程方式滚动?
For instance, If I have 5 items on my ListView all of it will be displayed on a single screen. But if I have 7 or more, my ListView begins to scroll. How do I know if my List can scroll programmatically?
推荐答案
在最后一个项目是在屏幕上部分可见Diegosan的回答不能区分。这里是对这一问题的解决方案。
Diegosan's answer cannot differentiate when the last item is partially visible on the screen. Here is a solution to that problem.
首先,在ListView必须在屏幕上呈现之前,我们可以检查,如果它的内容是滚动的。这可以用一个ViewTreeObserver完成:
First, the ListView must be rendered on the screen before we can check if its content is scrollable. This can be done with a ViewTreeObserver:
ViewTreeObserver observer = listView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (willMyListScroll()) {
// Do something
}
}
});
这是 willMyListScroll()
:
boolean willMyListScroll() {
int pos = listView.getLastVisiblePosition();
if (listView.getChildAt(pos).getBottom() > listView.getHeight()) {
return true;
} else {
return false;
}
}
这篇关于如何检查,如果我的ListView有项目滚动多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!