Android: how to check if a View inside of ScrollView is visible?

后端 未结 14 2035
自闭症患者
自闭症患者 2020-11-22 16:42

I have a ScrollView which holds a series of Views. I would like to be able to determine if a view is currently visible (if any part of it is curre

14条回答
  •  孤城傲影
    2020-11-22 17:42

    To expand a bit on Bill Mote's answer using getLocalVisibleRect, you may want to check if the view is only partially visible:

    Rect scrollBounds = new Rect();
    scrollView.getHitRect(scrollBounds);
    if (!imageView.getLocalVisibleRect(scrollBounds)
        || scrollBounds.height() < imageView.getHeight()) {
        // imageView is not within or only partially within the visible window
    } else {
        // imageView is completely visible
    }
    

提交回复
热议问题