public class

ObservableField<T>

extends androidx.databinding.BaseObservableField

implements java.io.Serializable

 java.lang.Object

androidx.databinding.BaseObservable

↳androidx.databinding.BaseObservableField

↳androidx.databinding.ObservableField<T>

Subclasses:

ObservableParcelable<T>

Gradle dependencies

compile group: 'androidx.databinding', name: 'databinding-runtime', version: '8.8.0-alpha01'

  • groupId: androidx.databinding
  • artifactId: databinding-runtime
  • version: 8.8.0-alpha01

Artifact androidx.databinding:databinding-runtime:8.8.0-alpha01 it located at Google repository (https://maven.google.com/)

Androidx class mapping:

androidx.databinding.ObservableField android.databinding.ObservableField

Overview

An object wrapper to make it observable.

Observable field classes may be used instead of creating an Observable object. It can also create a calculated field, depending on other fields:

public class MyDataObject {
     private Context context;
     public final ObservableField<String> first = new ObservableField<String>();
     public final ObservableField<String> last = new ObservableField<String>();
     public final ObservableField<String> display =
         new ObservableField<String>(first, last) {
             @Override
             public String get() {
                 return context.getResources().getString(R.string.name, first.get, last.get());
             }
         };
     public final ObservableInt age = new ObservableInt();
 }
Fields of this type should be declared final because bindings only detect changes in the field's value, not of the field itself.

Summary

Constructors
publicObservableField()

Creates an empty observable object

publicObservableField(Observable dependencies[])

Creates an ObservableField that depends on dependencies.

publicObservableField(java.lang.Object value)

Wraps the given object and creates an observable object

Methods
public java.lang.Objectget()

public voidset(java.lang.Object value)

Set the stored value.

from BaseObservableaddOnPropertyChangedCallback, notifyChange, notifyPropertyChanged, removeOnPropertyChangedCallback
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public ObservableField(java.lang.Object value)

Wraps the given object and creates an observable object

Parameters:

value: The value to be wrapped as an observable.

public ObservableField()

Creates an empty observable object

public ObservableField(Observable dependencies[])

Creates an ObservableField that depends on dependencies. Typically, ObservableFields are passed as dependencies. When any dependency notifies changes, this ObservableField also notifies a change.

Parameters:

dependencies: The Observables that this ObservableField depends on.

Methods

public java.lang.Object get()

Returns:

the stored value.

public void set(java.lang.Object value)

Set the stored value.

Parameters:

value: The new value

Source

/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.databinding;

import androidx.annotation.Nullable;

import java.io.Serializable;

/**
 * An object wrapper to make it observable.
 * <p>
 * Observable field classes may be used instead of creating an Observable object. It can also
 * create a calculated field, depending on other fields:
 * <pre><code>public class MyDataObject {
 *     private Context context;
 *     public final ObservableField&lt;String&gt; first = new ObservableField&lt;String&gt;();
 *     public final ObservableField&lt;String&gt; last = new ObservableField&lt;String&gt;();
 *     public final ObservableField&lt;String&gt; display =
 *         new ObservableField&lt;String&gt;(first, last) {
 *             &#64;Override
 *             public String get() {
 *                 return context.getResources().getString(R.string.name, first.get, last.get());
 *             }
 *         };
 *     public final ObservableInt age = new ObservableInt();
 * }</code></pre>
 * Fields of this type should be declared final because bindings only detect changes in the
 * field's value, not of the field itself.
 *
 * @param <T> The type parameter for the actual object.
 * @see ObservableParcelable
 */
public class ObservableField<T> extends BaseObservableField implements Serializable {
    static final long serialVersionUID = 1L;
    private T mValue;

    /**
     * Wraps the given object and creates an observable object
     *
     * @param value The value to be wrapped as an observable.
     */
    public ObservableField(T value) {
        mValue = value;
    }

    /**
     * Creates an empty observable object
     */
    public ObservableField() {
    }

    /**
     * Creates an ObservableField that depends on {@code dependencies}. Typically,
     * ObservableFields are passed as dependencies. When any dependency
     * notifies changes, this ObservableField also notifies a change.
     *
     * @param dependencies The Observables that this ObservableField depends on.
     */
    public ObservableField(Observable... dependencies) {
        super(dependencies);
    }

    /**
     * @return the stored value.
     */
    @Nullable
    public T get() {
        return mValue;
    }

    /**
     * Set the stored value.
     *
     * @param value The new value
     */
    public void set(T value) {
        if (value != mValue) {
            mValue = value;
            notifyChange();
        }
    }
}