android.databinding.adapters.ListenerUtil Java Examples

The following examples show how to use android.databinding.adapters.ListenerUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BindingConfig.java    From Android-MVVMFramework with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {"onRefreshListener", "refreshingAttrChanged"}, requireAll = false)
public static void setOnRefreshListener(final SwipeRefreshLayout view,
                                        final SwipeRefreshLayout.OnRefreshListener listener,
                                        final InverseBindingListener refreshingAttrChanged) {

    SwipeRefreshLayout.OnRefreshListener newValue = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            if (listener != null) {
                if (refreshingAttrChanged != null) {
                    refreshingAttrChanged.onChange();
                }
                listener.onRefresh();
            }
        }
    };

    SwipeRefreshLayout.OnRefreshListener oldValue = ListenerUtil.trackListener(view, newValue, R.id.onRefreshListener);
    if (oldValue != null) {
        view.setOnRefreshListener(null);
    }
    view.setOnRefreshListener(newValue);
}
 
Example #2
Source File: BindingConfig.java    From Android-MVVMFramework with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {"onRefreshListener", "refreshingAttrChanged"}, requireAll = false)
public static void setOnRefreshListener(final SwipeRefreshLayout view,
                                        final SwipeRefreshLayout.OnRefreshListener listener,
                                        final InverseBindingListener refreshingAttrChanged) {

    SwipeRefreshLayout.OnRefreshListener newValue = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            if (listener != null) {
                if (refreshingAttrChanged != null) {
                    refreshingAttrChanged.onChange();
                }
                listener.onRefresh();
            }
        }
    };

    SwipeRefreshLayout.OnRefreshListener oldValue = ListenerUtil.trackListener(view, newValue, R.id.onRefreshListener);
    if (oldValue != null) {
        view.setOnRefreshListener(null);
    }
    view.setOnRefreshListener(newValue);
}
 
Example #3
Source File: ListBindingAdapters.java    From views-widgets-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Assign a list of items to a ViewGroup. This is used with the {@code entries} and
 * {@code layout} attributes in the application namespace. Example Usage:
 * <pre><code>&lt;LinearLayout
 *     android:layout_width="match_parent"
 *     android:layout_height="wrap_content"
 *     android:orientation="vertical"
 *     app:entries="@{items}"
 *     app:layout="@{@layout/item}"/&gt;
 * </code></pre>
 * <p>
 * In the above, {@code items} is a List or ObservableList. {@code layout} does not
 * need to be hard-coded, but most commonly will be. This BindingAdapter will work
 * with any ViewGroup that only needs addView() and removeView() to manage its Views.
 * <p>
 * The layout, &commat;layout/item for example, must have a single variable named
 * {@code data}.
 */
@BindingAdapter({"entries", "layout"})
public static <T> void setEntries(ViewGroup viewGroup,
        List<T> oldEntries, int oldLayoutId,
        List<T> newEntries, int newLayoutId) {
    if (oldEntries == newEntries && oldLayoutId == newLayoutId) {
        return; // nothing has changed
    }

    EntryChangeListener listener =
            ListenerUtil.getListener(viewGroup, R.id.entryListener);
    if (oldEntries != newEntries && listener != null && oldEntries instanceof ObservableList) {
        ((ObservableList)oldEntries).removeOnListChangedCallback(listener);
    }

    if (newEntries == null) {
        viewGroup.removeAllViews();
    } else {
        if (newEntries instanceof ObservableList) {
            if (listener == null) {
                listener =
                        new EntryChangeListener(viewGroup, newLayoutId);
                ListenerUtil.trackListener(viewGroup, listener,
                        R.id.entryListener);
            } else {
                listener.setLayoutId(newLayoutId);
            }
            if (newEntries != oldEntries) {
                ((ObservableList)newEntries).addOnListChangedCallback(listener);
            }
        }
        resetViews(viewGroup, newLayoutId, newEntries);
    }
}
 
Example #4
Source File: HuePickerBindingAdapters.java    From spline with Apache License 2.0 5 votes vote down vote up
/**
 * This is how you implement 2-way data binding of a custom attribute if you want to maintain
 * a custom listener interface - OnHueChangeListener, in this case - while also leveraging
 * the synthetic attributes hueAttrChanged (the InverseBindingListener). For an example of
 * how to implement custom 2-way binding with just the synthetic AttrChanged attribute's
 * InverseBindingListener, see DocumentView or LayerListView's currentLayer 2-way binding
 * (along with the InverseBindingMethod declarations in DocumentViewModelBindingAdapters).
 *
 * @param view                   The view we're 2-way binding to.
 * @param onHueChangeListener    The OnHueChangeListener bound by the developer on the view
 *                               in question.
 * @param inverseBindingListener Synthetic attribute representing an InverseBindingListener
 *                               for changes to the hue attribute.
 */
@BindingAdapter(value = {"onHueChange", "hueAttrChanged"}, requireAll = false)
public static void setListeners(HuePicker view,
                                final HuePicker.OnHueChangeListener onHueChangeListener,
                                final InverseBindingListener inverseBindingListener) {
    HuePicker.OnHueChangeListener newListener;
    if (inverseBindingListener == null) {
        // If the synthetic listener param is null, just keep track of the listener set in
        // the binding
        newListener = onHueChangeListener;
    } else {
        // Otherwise define a new listener to wraps both listeners and invoke them at the
        // right time within the new listener if they are non-null
        newListener = new HuePicker.OnHueChangeListener() {
            @Override
            public void onHueChange(HuePicker huePicker, float hue) {
                if (onHueChangeListener != null) {
                    onHueChangeListener.onHueChange(huePicker, hue);
                }
                inverseBindingListener.onChange();
            }
        };
    }

    // Use track listener to record listener we're adding and return any old listeners
    // registered through this method so that on rebind we don't add duplicate listeners
    HuePicker.OnHueChangeListener oldListener = ListenerUtil.trackListener(view, newListener,
            R.id.hueChangeListener);

    // Remove the old listener we set using this BindingAdapter, if there is one
    if (oldListener != null) {
        view.removeHueChangeListener(oldListener);
    }

    // Add our new listener
    if (newListener != null) {
        view.addHueChangeListener(newListener);
    }
}
 
Example #5
Source File: ListBindingAdapters.java    From android-ui-toolkit-demos with Apache License 2.0 5 votes vote down vote up
/**
 * Assign a list of items to a ViewGroup. This is used with the {@code entries} and
 * {@code layout} attributes in the application namespace. Example Usage:
 * <pre><code>&lt;LinearLayout
 *     android:layout_width="match_parent"
 *     android:layout_height="wrap_content"
 *     android:orientation="vertical"
 *     app:entries="@{items}"
 *     app:layout="@{@layout/item}"/&gt;
 * </code></pre>
 * <p>
 * In the above, {@code items} is a List or ObservableList. {@code layout} does not
 * need to be hard-coded, but most commonly will be. This BindingAdapter will work
 * with any ViewGroup that only needs addView() and removeView() to manage its Views.
 * <p>
 * The layout, &commat;layout/item for example, must have a single variable named
 * {@code data}.
 */
@BindingAdapter({"entries", "layout"})
public static <T> void setEntries(ViewGroup viewGroup,
        List<T> oldEntries, int oldLayoutId,
        List<T> newEntries, int newLayoutId) {
    if (oldEntries == newEntries && oldLayoutId == newLayoutId) {
        return; // nothing has changed
    }

    EntryChangeListener listener =
            ListenerUtil.getListener(viewGroup, R.id.entryListener);
    if (oldEntries != newEntries && listener != null && oldEntries instanceof ObservableList) {
        ((ObservableList)oldEntries).removeOnListChangedCallback(listener);
    }

    if (newEntries == null) {
        viewGroup.removeAllViews();
    } else {
        if (newEntries instanceof ObservableList) {
            if (listener == null) {
                listener =
                        new EntryChangeListener(viewGroup, newLayoutId);
                ListenerUtil.trackListener(viewGroup, listener,
                        R.id.entryListener);
            } else {
                listener.setLayoutId(newLayoutId);
            }
            if (newEntries != oldEntries) {
                ((ObservableList)newEntries).addOnListChangedCallback(listener);
            }
        }
        resetViews(viewGroup, newLayoutId, newEntries);
    }
}