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

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

    public static int getVisiblePercent(View v) {
            if (v.isShown()) {
                Rect r = new Rect();
                v.getGlobalVisibleRect(r);
                double sVisible = r.width() * r.height();
                double sTotal = v.getWidth() * v.getHeight();
                return (int) (100 * sVisible / sTotal);
            } else {
                return -1;
            }
        }
    

提交回复
热议问题