如何判断一个视图在 Android 屏幕上是否可见? [英] How can you tell if a View is visible on screen in Android?

查看:57
本文介绍了如何判断一个视图在 Android 屏幕上是否可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查 ScrollView 中的 View 当前是否在 Android 中可见.我不是在检查它是否被关注,而是检查它当前是否正在显示在屏幕上.View 中是否有一个方法可以告诉我该视图当前是否可见?

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 being displayed on screen. Is there a method in View that can tell me if the view is currently visible?

推荐答案

这段代码对我有用:

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);
}

这篇关于如何判断一个视图在 Android 屏幕上是否可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆