如何检测一个小部件是开启/关闭屏幕?部件、屏幕

2023-09-06 22:19:57 作者:泪湿ベ回忆

我在写类似于网格自定义视图将被用来存放大量的图像。我使用的ImageButton显示图像,而我只想要加载图像到按钮,一旦他们在屏幕上(和公正的显示静态背景图片的时候都没有)。其理由是,我是从一些后端服务获取的图像和我只是想接他们时,按钮在屏幕上。

I'm writing a customized view similar to Grid which will be used to hold a large amount of images. I'm using ImageButton to display the images, and I only want to load images onto the buttons once they are on the screen (and just display a static background image when they are not). The reason for it is that I'm fetching images from some back-end service and I only want to fetch them when the buttons are on the screen.

要做到这一点,我想知道,如果小部件是在屏幕上。是否有可能以检测是否像按钮widget是开/关屏幕上?如果没有,就如何实现我想要做什么建议吗?

To do that, I'd like to know if the widget is on the screen. Is it possible to detect if a widget like button is on/off screen? If not, any suggestions on how to achieve what I'm trying to do?

推荐答案

我不认为你可以。创建窗口小部件时框架将只向您发送的onUpdate 请求,然后再根据您的更新策略。

I don't think you can. The framework will just send you an onUpdate request when the widget is created, and then again according to your update policy.

我不明白为什么会有一个理由来改变图像没有被显示的窗口小部件时。你不是拯救OS这样做的任何资源;它已经知道不要画你的部件时,它的屏幕外。

I don't see why there would be a reason to change the image when the widget isn't being shown. You're not saving the OS any resources by doing so; it already knows not to draw your widget when it's off-screen.