Stay organized with collections
Save and categorize content based on your preferences.
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. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[]]