Using ViewGroup.setOnHierarchyChangeListener I can only set one listener right? So if I wanted to set two listeners for the same event, I would have to use composite listener. How could I get the already existing listener? How could I remove it?
Asked
Active
Viewed 610 times
1 Answers
2
I checked the code of ViewGroup
and here are the answers:
Yes, you can set only one listener.
For more than one listener you can use a pattern similar to the above one. You don't need necessarily to create many listener objects, you could just use one listener and perform different actions based on some logic (which in turn could be equivalent to that pattern btw).
There is no getter for this listener, so you cannot get it from the
ViewGroup
. There are other solutions to this problem, depending on your case:- to control all code paths to the setter synching each time a reference to the listener
- to implement the interface such that it "leaks" a reference to itself somewhere you can store it
- etc.
The listener can be removed this way:
setOnHierarchyChangeListener(null)

Gil Vegliach
- 3,542
- 2
- 25
- 37
-
Thanks! It is weird how a getter is not implemented for a ViewGroup. – zed Sep 19 '14 at 23:09