检查视图是否在窗口上可见的最佳方法是什么? [英] What's the best way to check if the view is visible on the window?

查看:27
本文介绍了检查视图是否在窗口上可见的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查视图是否在窗口上可见的最佳方法是什么?

What's the best way to check if the view is visible on the window?

我有一个 CustomView,它是我 SDK 的一部分,任何人都可以将 CustomView 添加到他们的布局中.当我的 CustomView 定期对用户可见时,它正在执行一些操作.因此,如果视图对用户不可见,那么它需要停止计时器,当它再次可见时,它应该重新开始它的进程.

I have a CustomView which is part of my SDK and anybody can add CustomView to their layouts. My CustomView is taking some actions when it is visible to the user periodically. So if view becomes invisible to the user then it needs to stop the timer and when it becomes visible again it should restart its course.

但不幸的是,没有特定的方法可以检查我的 CustomView 对用户是可见还是不可见.我可以检查和聆听的内容很少:

But unfortunately there is no certain way of checking if my CustomView becomes visible or invisible to the user. There are few things that I can check and listen to:

onVisibilityChange //it is for view's visibility change, and is introduced in new API 8 version so has backward compatibility issue
onWindowVisibilityChange //but my CustomView can be part of a ViewFlipper's Views so it can pose issues
onDetachedFromWindows //this not as useful
onWindowFocusChanged //Again my CustomView can be part of ViewFlipper's views.

所以如果有人遇到过这种问题,请多多指教.

So if anybody has faced this kind of issues please throw some light.

推荐答案

就我而言,以下代码最适合监听 View 是否可见:

In my case the following code works the best to listen if the View is visible or not:

@Override
protected void onWindowVisibilityChanged(int visibility) {
    super.onWindowVisibilityChanged(visibility);
    Log.e(TAG, "is view visible?: " + (visibility == View.VISIBLE));
}

这篇关于检查视图是否在窗口上可见的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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