public class

BaseObservable

extends java.lang.Object

implements Observable

 java.lang.Object

↳androidx.databinding.BaseObservable

Subclasses:

ObservableDouble, ObservableByte, ObservableLong, ViewDataBinding, ObservableBoolean, ObservableFloat, ObservableChar, ObservableShort, ObservableParcelable<T>, ObservableInt, ObservableField<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.BaseObservable android.databinding.BaseObservable

Overview

A convenience class that implements Observable interface and provides BaseObservable.notifyPropertyChanged(int) and BaseObservable.notifyChange() methods.

Summary

Constructors
publicBaseObservable()

Methods
public voidaddOnPropertyChangedCallback(Observable.OnPropertyChangedCallback callback)

public voidnotifyChange()

Notifies listeners that all properties of this instance have changed.

public voidnotifyPropertyChanged(int fieldId)

Notifies listeners that a specific property has changed.

public voidremoveOnPropertyChangedCallback(Observable.OnPropertyChangedCallback callback)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public BaseObservable()

Methods

public void addOnPropertyChangedCallback(Observable.OnPropertyChangedCallback callback)

public void removeOnPropertyChangedCallback(Observable.OnPropertyChangedCallback callback)

public void notifyChange()

Notifies listeners that all properties of this instance have changed.

public void notifyPropertyChanged(int fieldId)

Notifies listeners that a specific property has changed. The getter for the property that changes should be marked with Bindable to generate a field in BR to be used as fieldId.

Parameters:

fieldId: The generated BR id for the Bindable field.

Source

/*
 * Copyright (C) 2014 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.NonNull;

/**
 * A convenience class that implements {@link androidx.databinding.Observable}
 * interface and provides {@link #notifyPropertyChanged(int)} and
 * {@link #notifyChange} methods.
 */
public class BaseObservable implements Observable {
    private transient PropertyChangeRegistry mCallbacks;

    public BaseObservable() {
    }

    @Override
    public void addOnPropertyChangedCallback(@NonNull OnPropertyChangedCallback callback) {
        synchronized (this) {
            if (mCallbacks == null) {
                mCallbacks = new PropertyChangeRegistry();
            }
        }
        mCallbacks.add(callback);
    }

    @Override
    public void removeOnPropertyChangedCallback(@NonNull OnPropertyChangedCallback callback) {
        synchronized (this) {
            if (mCallbacks == null) {
                return;
            }
        }
        mCallbacks.remove(callback);
    }

    /**
     * Notifies listeners that all properties of this instance have changed.
     */
    public void notifyChange() {
        synchronized (this) {
            if (mCallbacks == null) {
                return;
            }
        }
        mCallbacks.notifyCallbacks(this, 0, null);
    }

    /**
     * Notifies listeners that a specific property has changed. The getter for the property
     * that changes should be marked with {@link Bindable} to generate a field in
     * <code>BR</code> to be used as <code>fieldId</code>.
     *
     * @param fieldId The generated BR id for the Bindable field.
     */
    public void notifyPropertyChanged(int fieldId) {
        synchronized (this) {
            if (mCallbacks == null) {
                return;
            }
        }
        mCallbacks.notifyCallbacks(this, fieldId, null);
    }
}