1

我只是遇到了一个问题,使用 android 数据绑定库。

这是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">

<data>
    <import type="com.test.app.ObservableFieldWrapper"/>
    <variable
        name="org"
        type="ObservableFieldWrapper"/>
</data>
<LinearLayout
    android:id="@+id/headerListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">      
    <com.test.app.NSpinner
        android:id="@+id/orgSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:org="@{org.getSilent ? org.content : &quot;silent&quot;}"/>
</LinearLayout>

这是我的 NSpinner:

 public class ObservableFieldWrapper{

    private final ObservableBoolean silent;

    private final ObservableField<String> content;

    @BindingAdapter("org")
    public static void setOrg(Spinner view, String org) {
        assert org != null;
        if (org.equals("silent")) {
           Log.i("ObsWrapper", "SET ORG called via binding adapter but got denied, because of SILENCE");
            } else {
                Log.i("ObsWrapper", "SET ORG called via binding adapter NORMALLY");
                view.setSelection(Cache.GetOrgIndexForSpinner(), true);
            }
    }

    public ObservableFieldWrapper(String startValue) {
        content = new ObservableField<>(startValue);
        silent = new ObservableBoolean();
        silent.set(false);
    }

    public void setContent(String newValue) {
        silent.set(false);
        content.set(newValue);
        content.notifyChange();
    }

    public void setContentSilent(String newValue) {
        silent.set(true);
        content.set(newValue);
    }

  //Bunch of getters
}

并且此调用应调用 ObservableFieldWrapper 类提供的静态 getter(假设所有绑定都已设置):

ObservableFieldWrapper someField = new ObservableFieldWrapper("someString");
someField.setContent("some other string");

好吧,问题是......它什么都不调用。但是如果我改变我的 xml 部分

app:org="@{org.getSilent ? org.content : &quot;silent&quot;}"

共同的

app:org="@{org.content}"

它开始工作!我真的需要这个带有布尔值的额外功能,我真的很想找到这个问题。


找到了一个解决方法,在 xml 表达式中没有使用任何逻辑,我只是​​将 2 个参数传递给我的函数并在那里完成了所有工作。

@Bindable ("{org, silent}")

然而,这个问题仍然没有答案。

4

1 回答 1

0

正如 George Mount 所提到的 - 删除可观察字段上的任何 getter 很重要,否则它将不起作用,我花了很多时间处理这个问题,然后提到我有一个 getter,在删除它之后 - 一切都开始工作了。

于 2015-12-11T19:05:57.600 回答