The Android UI is built up from Views, and in a regular application, there are usually several of them. To find out which View the user is currently looking at, you need to install Visibility Listeners.

Android UI是从Views构建的,在常规应用程序中,通常有几种。 要了解用户当前正在查看哪个View,您需要安装Visibility Listeners

Read below to find out about the different options you have to identify the visibility status of a View.

请阅读以下内容,了解确定视图的可见性状态所必须使用的不同选项。

如何变得可见 (How To Become Visible)

In order for our listeners to work, we must first make sure our View is found in the layout hierarchy. There are two ways this happens:

为了使我们的监听器正常工作,我们必须首先确保在布局层次结构中找到我们的View。 发生这种情况的方式有两种:

  1. Your View is already part of your layout as it is defined in an XML file您的视图已经是布局的一部分,因为它是在XML文件中定义的
  2. You created a View dynamically, and you need to add it using the addView method您动态创建了一个View,需要使用addView方法添加它

A View’s visibility status is of Integer type and can have one of three options:

视图的可见性状态为整数类型,可以具有以下三个选项之一:

  1. VISIBLE (0) - The View is visible to the user

    可见(0) -视图对用户可见

  2. INVISIBLE (4) - The View is invisible to the user, but still takes up space in the layout

    看不见的(4) -视图对用户不可见,但仍占据布局中的空间

  3. GONE (8) - The View is invisible, and it does not take up space in the layout

    消失(8) -视图是不可见的,并且不占用布局中的空间

Once inside our layout hierarchy, there are a few native options to help us know when our View’s visibility has changed.

进入布局层次结构后,有一些本机选项可帮助我们了解View的可见性已更改的时间。

onVisibilityChanged (onVisibilityChanged)

protected void onVisibilityChanged (View changedView, int visibility)

This method is triggered when the visibility of the view or of an ancestor of the view has changed. The status of the visibility is found inside the visibility parameter.

当视图或视图祖先的可见性已更改时,将触发此方法。 可见性的状态位于可见性参数内。

onWindowVisibilityChanged (onWindowVisibilityChanged)

protected void onWindowVisibilityChanged (int visibility)

This method is triggered when the containing window of our View has changed its visibility. This does not guarantee that the window your View is in is visible to the user, as it may be obscured by another window.

当视图的包含窗口更改其可见性时,将触发此方法。 这不能保证用户可以看到您的视图所在的窗口,因为它可能被另一个窗口遮盖。

行动中的可见性侦听器 (Visibility Listeners In Action)

To see these two listeners in action, let us create a simple project. We will have a LinearLayout with a TextView and a button. We’ll make the button’s on click action add our custom view to the layout.

要查看这两个侦听器的运行情况,让我们创建一个简单的项目。 我们将使用带有TextView和按钮的LinearLayout。 我们将使按钮的单击动作添加我们的自定义视图到布局。

Our custom view:

我们的自定义视图:

package com.tomerpacific.viewvisibility;import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;import static android.view.Gravity.CENTER;public class MyCustomView extends LinearLayout {private final String TAG = MyCustomView.class.getSimpleName();public MyCustomView(Context context) {super(context);this.setBackgroundColor(Color.GREEN);this.setGravity(CENTER);TextView myTextView = new TextView(context);myTextView.setText("My Custom View");addView(myTextView);}@Overridepublic void onVisibilityChanged(View changedView, int visibility) {super.onVisibilityChanged(changedView, visibility);Log.d(TAG, "View " + changedView + " changed visibility to " + visibility);}@Overridepublic void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility);Log.d(TAG, "Window visibility changed to " + visibility);}}

And finally, the code in our MainActivity:

最后,我们MainActivity中的代码:

When we run the application and press the button we get:

当我们运行该应用程序并按下按钮时,我们得到:

https://giphy.com/gifs/8JZA6Djt7DmYpEXj2h/html5

https://giphy.com/gifs/8JZA6Djt7DmYpEXj2h/html5

You can get the sample project here.

您可以在此处获得示例项目。

ViewTreeObserver (ViewTreeObserver)

This is a native object that has a wide range of listeners that are notified of various visibility changes to the view tree. Some prominent ones to take notice of are:

这是一个本机对象,具有广泛的侦听器,这些侦听器会收到有关视图树的各种可见性更改的通知。 需要注意的一些著名的是:

  • OnGlobalLayoutListener

    OnGlobalLayoutListener

  • OnWindowAttachListener

    OnWindowAttachListener

  • OnWindowFocusChangeListener

    OnWindowFocusChangeListener

To attach a ViewTreeObserver, you need to do the following:

要附加ViewTreeObserver,您需要执行以下操作:

The line linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this) makes sure that the listener will only get called once. If you want to continue listening in on changes, remove it.

linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this) 确保侦听器仅被调用一次。 如果要继续收听更改,请将其删除。

If you have any comments or suggestions, feel free to let me know.

如果您有任何意见或建议,请随时告诉我。

翻译自: https://www.freecodecamp.org/news/how-and-why-to-use-android-visibility-listeners-971e3b6511ec/

如何以及为什么使用Android Visibility Listeners相关推荐

  1. (转载)android:visibility和android:scaleType 属性

    1.android:visibility="gone" 其有三个属性:visible显示:invisible显示黑背景条:gone不显示 2.android:scaleType属性 ...

  2. [Android] android:visibility属性应用

    在Android开发中,有这样一种场景: 当edittext中有内容时候,旁边的提交或确定按钮显示: 当edittext中内容为空的时候,提交或确定按钮隐藏: 要实现起来其实并不困难.很多控件都有vi ...

  3. View 的 android:visibility属性的讨论

    Android VIEW 中的 visibility 属性,在API中的描述为:Controls the initial visibility of the view. [控制VIEW的初始可见性]. ...

  4. android:visibility的VISIABLE,INVISIABLE,GONE的区别

    View.VISIALBE 可见,显示到页面 View.INVISIABLE 不可见,但是还是占用位置 View.GONE 隐藏,不可见并且不占用位置

  5. android visibility动画,android – 动画和setVisibility

    我有一个LinearLayout,我想通过点击"更多详细信息"链接可以显示/隐藏.我通过电话来做到这一点 moreDetailsSection.setVisibility(View ...

  6. Android中visibility属性VISIBLE、INVISIBLE、GONE的区别

    在Android开发中,大部分控件都有visibility这个属性,其属性有3个分别为"visible "."invisible"."gone&quo ...

  7. Android中的visibility属性

    Android开发中,大部分控件都有visibility这个属性,其属性有3个分别为"visible "."invisible"."gone" ...

  8. JAVA中的visible什么意思_详解Android中visibility属性VISIBLE、INVISIBLE、GONE的区别

    在Android开发中,大部分控件都有visibility这个属性,其属性有3个分别为"visible "."invisible"."gone&quo ...

  9. Android 系统(145)---ODM 开发用户常见需求文档(七)

    一:相机连拍声音要比成像速度快 (vendor/mediatek/proprietary/packages/apps/Camera/src/com/mediatek/camera/util/Captu ...

最新文章

  1. 【1024创造营】精彩课程回顾
  2. java网络通信:异步非阻塞I/O (NIO)
  3. LoRa技术实现水表抄表远距离无线传输方案的应用
  4. [模板]fhqTreap
  5. 编译wxWidgets
  6. 2019.7.20十道js题目
  7. html5连接mysql数据库操作_html5-本地数据库的操作
  8. python gridsearch_Python超参数自动搜索模块GridSearchCV上手
  9. 项目期复习:JS操作符,弹窗与调试,凝视,数据类型转换
  10. 桌面时间的最佳管理者-软媒时间
  11. java平方根函数_java程序中怎么调用平方根函数
  12. 华为服务器怎么查看系统日志,云服务器怎么查看系统日志
  13. Flink编程中遇到”scala.tools.reflect.ToolBoxError: reflective compilation has failed“的解决方法
  14. Redis源码分析(sds)
  15. 【Python】爬取理想论坛单帖爬虫
  16. 我的世界光影mod怎么用_我的世界光影怎么安装 光影安装教程
  17. Linux常用命令和操作
  18. mysql查找操作返回值出现 - [User{id=1, userName='null', date=Sun Sep 16 00:00:00 CST 2018}]
  19. UTM: 如何注册 SonicWALL 防火墙
  20. 信息论复习—线性分组码的基本性质

热门文章

  1. docker 安装和使用
  2. JavaScript脚本文件学习总结
  3. 小程序订阅消息 订阅消息开发
  4. flask的客户端服务端
  5. 郭为:大数据时代的企业管理挑战
  6. Linux (x86) Exploit 开发系列教程之十一 Off-By-One 漏洞(基于堆)
  7. Jfinal Generator 不需要生成带某个前缀的表名数组的方法
  8. Linux字符设备驱动程序的框架(新写法)
  9. Fatal Error: Out of memory php内存溢出处理三种方法
  10. linux中的一些命令的想法