子视图焦点更改android [英] Child view focus change android
问题描述
我有一个 ViewGroup
,它可以有很多 LinearLayout
.每个 LinearLayout
可以有许多嵌套的子视图.我想要的是,如果 LinearLayout
内的任何视图(甚至是深层嵌套)获得焦点,它应该调用其主要父级 LinearLayout
的自定义方法.
I have a ViewGroup
that can have many LinearLayout
s. And each LinearLayout
can have many nested child views. What I want is that, if any view(even deep nested) inside a LinearLayout
gets focus, it should call a custom method of its main parent LinearLayout
.
我想要做的最后一件事是,在 LinearLayout
的每个深层嵌套子视图上设置 OnFocusChangeListener
,该侦听器将调用其父级的自定义方法.但这真的很糟糕.
The last thing I want to do is, set OnFocusChangeListener
on every single deep nested child views of LinearLayout
and that listener will call its parent's custom method. But that is really bad way to go for.
是否有任何方法可以覆盖父 LinearLayout
每次其任何嵌套子视图的焦点更改时都会调用?
Is there any method I can override of parent LinearLayout
that gets called every time its any nested child view's focus changes ?
推荐答案
您可以使用 contentView.getViewTreeObserver().addOnGlobalFocusChangeListener()
或覆盖 ViewGroup.requestChildFocus()
根视图组的(requestChildFocus()
沿视图父链传递).
You could use contentView.getViewTreeObserver().addOnGlobalFocusChangeListener()
or override ViewGroup.requestChildFocus()
of the root ViewGroup (requestChildFocus()
is passed along the view parent chain).
这篇关于子视图焦点更改android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!