什么是检查视图窗口上看到的最好的方法是什么?最好的、视图、窗口、方法

2023-09-06 01:51:54 作者:深情娇喘

什么是检查视图窗口上看到的最好的方法是什么?

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.

推荐答案

的OnDraw()被称为每次视图需要绘制。当视图是关闭屏幕,然后的OnDraw()不会被调用。当视图的一点点是成为对用户可见的OnDraw然后()被调用。这是不理想,但我不能看到另一个电话使用,因为我想要做同样的事情。请记住调用super.onDraw或视图不会拿得出。小心改变的OnDraw任何导致认为是无效的,这将导致再次调用OnDraw中的。

onDraw() is called each time the view needs to be drawn. When the view is off screen then onDraw() is never called. When a tiny bit of the view is becomes visible to the user then onDraw() is called. This is not ideal but I cannot see another call to use as I want to do the same thing. Remember to call the super.onDraw or the view won't get drawn. Be careful of changing anything in onDraw that causes the view to be invalidate as that will cause another call to onDraw.

如果您使用的是列表视图,然后getView可以用,只要您的列表视图变为显示给用户。

If you are using a listview then getView can be used whenever your listview becomes shown to the user.

显然活性的onPause()被称为所有的意见都被掩盖起来,并且不会给用户。也许调用无效()父,如果的OnDraw()是不是叫那么它是不可见的。

obviously the activity onPause() is called all your views are all covered up and are not visible to the user. perhaps calling invalidate() on the parent and if ondraw() is not called then it is not visible.