ListenerUtil
public
class
ListenerUtil
extends Object
Utility class that helps with tracking listeners for views from binding adapters.
Summary
Inherited methods |
From
class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
Public constructors
ListenerUtil
ListenerUtil ()
Public methods
getListener
T getListener (View view,
int listenerResourceId)
Returns the previous value for a listener if one was stored previously with
trackListener(View, Object, int)
Parameters |
view |
View : The View to check for a listener previously stored with
trackListener(View, Object, int) |
listenerResourceId |
int : A unique resource ID associated with the listener type. |
Returns |
T |
The previously tracked listener. This will be null if the View did not have
a previously-tracked listener. |
trackListener
T trackListener (View view,
T listener,
int listenerResourceId)
This method tracks listeners for a View. Only one listener per listenerResourceId
can be tracked at a time. This is useful for add*Listener and remove*Listener methods
when used with BindingAdapters. This guarantees not to leak the listener or the View,
so will not keep a strong reference to either.
Example usage:
@BindingAdapter("onFoo")
public static void addFooListener(MyView view, OnFooListener listener) {
OnFooListener oldValue = ListenerUtil.trackListener(view, listener, R.id.fooListener);
if (oldValue != null) {
view.removeOnFooListener(oldValue);
}
if (listener != null) {
view.addOnFooListener(listener);
}
}
Parameters |
view |
View : The View that will have this listener |
listener |
T : The listener to keep track of. May be null if the listener is being removed. |
listenerResourceId |
int : A unique resource ID associated with the listener type. |
Returns |
T |
The previously tracked listener. This will be null if the View did not have
a previously-tracked listener. |