我有一个 ScrollView
,其中包含一系列 Views
。我希望能够确定一个 View 当前是否可见(如果它的任何部分当前由 ScrollView
显示)。我希望下面的代码可以做到这一点,令人惊讶的是它没有:
Rect bounds = new Rect();
view.getDrawingRect(bounds);
Rect scrollBounds = new Rect(scroll.getScrollX(), scroll.getScrollY(),
scroll.getScrollX() + scroll.getWidth(), scroll.getScrollY() + scroll.getHeight());
if(Rect.intersects(scrollBounds, bounds))
{
//is visible
}
最佳答案
这行得通:
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
// Any portion of the imageView, even a single pixel, is within the visible window
} else {
// NONE of the imageView is within the visible window
}
关于Android:如何检查 ScrollView 内的 View 是否可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4628800/