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

后端 未结 14 2032
自闭症患者
自闭症患者 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:29

    If you want to detect that the view is FULLY visible:

    private boolean isViewVisible(View view) {
        Rect scrollBounds = new Rect();
        mScrollView.getDrawingRect(scrollBounds);
    
        float top = view.getY();
        float bottom = top + view.getHeight();
    
        if (scrollBounds.top < top && scrollBounds.bottom > bottom) {
            return true;
        } else {
            return false;
        }
    }
    

提交回复
热议问题