如何在child POJO 中使用@Bindable?

How to use @Bindable in child POJO?

我做了2个POJO必用的布局。所以我创建了一个这样的界面:

public interface GameItemParent {
  ...
  boolean isChecked();
}

child之一类:

public class FavoriteGame implements GameItemParent,Observable {
  @SerializedName(SerCons.C_CHECKED) private int checked;

  private PropertyChangeRegistry registry = new PropertyChangeRegistry();

  public FavoriteGame() {
  }

  @Bindable public boolean isChecked() {
    return checked == 1;
  }

  public void setChecked(boolean checked, boolean notifyObserver) {
    this.checked = checked ? 1 : 0;
    if (notifyObserver)
      registry.notifyChange(this, BR.checked);
  }

  public void inverseChecked() {
    setChecked(!isChecked(), true);
  }

  @Override public void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
    registry.add(callback);
  }

  @Override public void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
    registry.remove(callback);
  }
  ...
}

监听'isChecked'的XML文件变化:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >
  <data>
    <import type="android.view.View"/>
    <variable
        name="game"
        type="com.consoleco.console.objectParents.GameItemParent"
        />
  </data>
  <androidx.constraintlayout.widget.ConstraintLayout
      .../>
    <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="@{game.checked}"
        android:visibility="@{game.hasCheckButton() ? View.VISIBLE : View.GONE}"
        app:buttonTint="?attr/colorAccent"
        app:layout_constraintBottom_toBottomOf="@+id/icon"
        app:layout_constraintEnd_toEndOf="@+id/icon"
        app:layout_constraintStart_toEndOf="@+id/icon"
        app:layout_constraintTop_toBottomOf="@+id/icon"
        />
  </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

如您所见,我将 'GameItemParent' 接口声明为 'game' 数据。因为我也必须将这个 XML 用于另一个 child。

现在,当我在运行时更改 'isChecked' 时,UI(实际上是复选框)不会发生变化。

GameItemParent 是一个简单的接口,因此绑定库只知道 属性 本身。它应该工作,当它还扩展 Observable:

public interface GameItemParent extends Observable {
    ...
    @Bindable boolean isChecked();
}