Android - 侦听器/回调,当View子项数量改变时 [英] Android - Listener/Callback for when number of View children changes

查看:769
本文介绍了Android - 侦听器/回调,当View子项数量改变时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,当给定视图的子项数动态变化时,是否可以使用/实现一个监听器来触发回调?例如,一个视图有3个直接的孩子,一个通过代码删除,我需要一个事件来说,直接孩子的数量已经改变。

In Android can one use/implement a listener that fires a callback when the number of children for a given View changes dynamically? Say for example a View has 3 immediate children, and one is removed through code, I need an event to fire to say that the number of immediate children has changed.

澄清,我不需要在View树上执行深度遍历来计数孩子,我只是想计算给定视图第一层的孩子数量。

To clarify, I don't need to perform a depth traversal on the View tree to count children, I'm just interested in counting the number of children at the first layer of a given View.

非常感谢

推荐答案

我相信你想使用 OnHierarchyChangeListener


当此视图中的层次结构更改时将调用的回调。每当子视图添加到此视图或从视图中删除时,层次结构都会更改。

Interface definition for a callback to be invoked when the hierarchy within this view changed. The hierarchy changes whenever a child is added to or removed from this view.

它有两个回调:

  • onChildViewAdded()
  • onChildViewRemoved()

这篇关于Android - 侦听器/回调,当View子项数量改变时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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