ListenerUtil

public class ListenerUtil
extends Object

java.lang.Object
   ↳ android.databinding.adapters.ListenerUtil


Utility class that helps with tracking listeners for views from binding adapters.

Summary

Public constructors

ListenerUtil()

Public methods

static <T> T getListener(View view, int listenerResourceId)

Returns the previous value for a listener if one was stored previously with trackListener(View, Object, int)

static <T> T trackListener(View view, T listener, int listenerResourceId)

This method tracks listeners for a View.

Inherited methods

From class java.lang.Object

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.