How can you tell if a View is visible on screen in Android?

后端 未结 5 806
你的背包
你的背包 2020-12-23 19:32

I want to check if a View within a ScrollView is currently visible in Android. I am not checking if it is focused on yet but if it is currently bei

5条回答
  •  一生所求
    2020-12-23 19:54

    This code works for me:

    public static boolean isVisible(final View view) {
        if (view == null) {
            return false;
        }
        if (!view.isShown()) {
            return false;
        }
        final Rect actualPosition = new Rect();
        view.getGlobalVisibleRect(actualPosition);
        final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight());
        return actualPosition.intersect(screen);
    }
    

提交回复
热议问题