如何以及为什么使用Android Visibility Listeners
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。 发生这种情况的方式有两种:
- Your View is already part of your layout as it is defined in an XML file您的视图已经是布局的一部分,因为它是在XML文件中定义的
- 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:
视图的可见性状态为整数类型,可以具有以下三个选项之一:
VISIBLE (0) - The View is visible to the user
可见(0) -视图对用户可见
INVISIBLE (4) - The View is invisible to the user, but still takes up space in the layout
看不见的(4) -视图对用户不可见,但仍占据布局中的空间
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/
相关文章:

在vue中使用Element-UI
Element-UI是一套基于Vue2.0的UI组件库,http://element.eleme.io/#/zh-CN/component/carousel 首先npm install element-ui --save 然后在main.js中引入: import Vue from vue import ElementUI from element-ui import element-ui/lib/theme-default/in…
Flex布局教程(来源:阮一峰)
网页布局(layout)是 CSS 的一个重点应用。Flex 布局将成为未来布局的首选方案。本文介绍它的语法,下一篇文章给出常见布局的 Flex 写法。网友 JailBreak 为本文的所有示例制作了 Demo,也可以参考。 以下内容主要参考了下面两篇文…

ibatis的there is no statement named xxx in this SqlMap
报错情况如下:com.ibatis.sqlmap.client.SqlMapException: There is no statement named Control.insert-control in this SqlMap. at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.getMappedStatement(SqlMapExecutorDelegate.java:231)at com.ibatis.sq…

javascript案例_如何在JavaScript中使用增强现实-一个案例研究
javascript案例by Apurav Chauhan通过Apurav Chauhan 如何在JavaScript中使用增强现实-一个案例研究 (How to use Augmented Reality with JavaScript — a case study) In this experiment, I talk about how Augmented Reality with JS can be used to make learning more f…

久未更 ~ 一之 —— 关于ToolBar
很久没更博客了,索性开一个久未更 系列 > > > > > 久未更 系列一:关于ToolBar的使用(后续补充) 1 //让 ToolBar 单独使用深色主题 使得 toolbar 中元素 变为淡色 2 android:theme"style/ThemeOverlay.AppCompat.Dark.ActionBar"…
SQLServer怎样把本地数据导入到远程服务器上(转载)
平常用到mssql时间比较少,总是过一段时间就忘记应该怎么操作了。当要做mssq把本地数据导入到远程服务器的时候,就去网上搜索很久都没有图解的,所以今天自己收集一下免得下次又到处去找。希望对自己,同时对其它需要的人都有一定的帮…

input 默认样式的修改
/* 修改input选中的默认边框样式 */ outline: none; /* 修改input的选中时的光标颜色 */ caret-color:red; /* 修改input的选中时的默认边框 */ border: none; /* 修改input的提示文字的默认样式 */ input::-webkit-input-placeholder{color:#d0d0d0;}

巨石加密_点餐:如何吃一个可怕的巨石
巨石加密by Alan Ridlehoover通过艾伦里德尔霍弗 点餐:如何吃一个可怕的巨石 (Ordering Take Out: How to Eat a Scary Monolith) Martin Fowler said:马丁福勒(Martin Fowler) 说 : Almost all the successful microservice stories have started wit…

Halcon学习之六:获取Image图像中Region区域的特征参数
area_center_gray ( Regions, Image : : : Area, Row, Column ) 计算Image图像中Region区域的面积Area和重心(Row,Column)。 cooc_feature_image ( Regions, Image : : LdGray, Direction : Energy,Correlation, Homogeneity, Contrast ) …

dos下命令行执行程序时候注意程序所使用文件的路径问题
dos下命令行执行程序时候,最好是用cd命令先切换到程序所在目录下,这样就不会出现文件找不到的问题,如果由于特殊原因,不使用cd命令,而只使用路径命令时候程序中访问的资源也只能是改成绝对路径了,这样对有源…

Vant 使用之Toast Vant安装和使用
Vant 是一个VUE 的移动端组件库,里面有很多好用的组件。 第一步,安装和配置 Vant npm i vant -S npm i babel-plugin-import -D 安装完成之后,在项目 .babelrc 文件修改配置 plugins "plugins": [["import", {"…

15-5重构_重构-糟糕,我一直在向后做。
15-5重构by Justin Fuller贾斯汀富勒(Justin Fuller) 重构-糟糕,我一直在向后做。 (Refactoring — oops, I’ve been doing it backwards.) Welcome to my intervention. I’m a refactoring addict and I’m not afraid to admit it, but there’s only one prob…

JPush 使用教程
JPush 使用教程 自己使用的一些经验,为了方便直接从这里复制过去就行。 就当做个笔记,防止长时间忘记之后,还需要去官网看文档。 主要思路: sdk文件 三方依赖系统库 头文件 添加代理 初始化代码 1.版本信息 JPush : 2.2.0 Xco…

浏览器常见兼容性问题汇总
1、随便写几个标签,不加样式控制的情况下,各自的margin 和padding差异较大,解决方案是:*{margin:0;padding:0;} 2、块属性标签float后,又有横行的margin情况下,在IE6显示margin比设置的大,常出现…

VUE 动态绑定class
第一种:通过一个布尔值判断样式类是否生效 //isActive 是在data里面布尔值, rotateRight 是 class 样式类 //isActive 为true时样式类 rotateRight 生效 <div :class"{rotateRight:isActive}">abs</div> 第二种:通…

低声教育_我内心低声说:“成为建设者”
低声教育by Rebecca Radding由丽贝卡拉丁(Rebecca Radding) 我内心低声说:“成为建设者” (Something within me whispered: “Be the builder”) 加沙代码学院前任主持人Yasmin Hillis(自称嬉皮士)是一个内心的嬉皮士,她谈到了弗吉尼亚伍尔夫如何激发她…

【Web API系列教程】1.2 — Web API 2中的Action Results
前言 本节的主题是ASP.NET Web API怎样将控制器动作的返回值转换成HTTP的响应消息。 Web API控制器动作能够返回下列的不论什么值: 1。 void 2。 HttpResponseMessage 3, IHttpActionResult 4, Some other type 取决于返回的以上哪一种。…

前端开发常用单词
methods 方法 mounted 创建完成 export 输出 default 默认的 install 安装 components 组件 template 模板 params 参数 route 路线;途径 package 包;盒子;袋 ; toutes 路由器 plugin 插件 local host 本地 require 需要;依赖; storage 储存 prototype 原型 …

原生ajax的post操作
xml.open(方法,路径,是否开启异步); console.log(e);来找出数据所在位置; 调用 ajax只能传二进制或字符串,需要先把json转一下,JSON.stringify(); 获取到数据时我们要通过JSON.parse()再转成JSO…

jquery后学什么_我在训练营两年后学到了什么
jquery后学什么by Kara Luton卡拉卢顿(Kara Luton) 我在训练营两年后学到了什么 (What I’ve Learned Two Years Post-Bootcamp) It’s been two entire years since I left behind my career of being a music publicist — one I worked towards all of college and miracul…

ExecutorService 的理解与使用
接口 Java.util.concurrent.ExecutorService 表述了异步执行的机制,并且可以让任务在后台执行。壹個 ExecutorService 实例因此特别像壹個线程池。事实上,在 java.util.concurrent 包中的 ExecutorService 的实现就是壹個线程池的实现。 ExecutorService…

前后端交互,网络请求
这边文章主要根据我自己的前端开发工作经验,东拼西凑出来的一点理解,希望能够对大家有点帮助,如果有误导或者错误的地方还请帮助指正,感谢!!! 前后端交互我理解主要分为三个主要的部分…

HDU 1877 另一个版本 A+B
另一个版本 AB Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12894 Accepted Submission(s): 4901Problem Description输入两个不超过整型定义的非负10进制整数A和B(<231-1)。输出AB的m (1 < m <10…

gatsby_如何使用Gatsby.js来获取内容
gatsbyby Dimitri Ivashchuk由Dimitri Ivashchuk 如何使用Gatsby.js来获取内容 (How to source content with Gatsby.js) Gatsby.js is a powerful static site generator (with dynamic capabilities) which can be used to build super performant web-sites. It has a very…

使用 AFNetworking 进行 XML 和 JSON 数据请求
(1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLDictionary 进行数据转换 使用 XMLDictionary 的好处:有效避免自行实现 NSXMLParserDelegate 委托代理…

批处理+定时任务实现定时休息提醒
前言:俗话说的好,懒是第一生产力,懒是提高生产效率的必要条件。而现今windows是大部分人的第一生产工具,批处理定时任务这对黄金搭档就是提升生产效率的第一工具。大家在生产过程中经常会遇到各种周期性的重复的工作,比…

后端返回的数据中换行符 html换行
标签代码 <span v-html"model3_txt"></span> vue js代码 var txt "恭喜你\n获得某某某奖品"; if(txt.indexOf(\n)!-1){var reg new RegExp("/r/n", "g");txttxt.replace(reg, "<br/>");console.log(t…

vim block vim_如何不再害怕Vim
vim block vim精选最流行的命令以及如何使用它们 (A curation of the most popular commands and how to use them) If you’ve ever used Vim, you know how difficult it can get to reach the same speed as in a “normal” GUI editor. But once you do, the payoff is ex…

android EditText 限定中文个数与英文个数的解决方式
EditText 限定中文8个英文16个的解决方法。 在EditText上控件提供的属性中有限定最大最小长度的方法。可是,对于输入时,限定中文8个英文16个时,怎么办?相当于一个中文的长度是两个英文的长度。原理就不说了。自己看一下android的源…

根据二叉树的前序遍历和中序遍历重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。1 /**2 * Definition for …