使DependsOn在RoboBinding中工作

时间:2015-07-06 07:32:41

标签: java android mvvm data-binding robobinding

在RoboBinding中有注释DependsOnStateOf。 在这样的PresentationModel中使用它时:

@PresentationModel
class GreetingPresentationModel {
    String firstname;
    String lastname;
    //getters and setters for both
    @DependsOnStateOf("firstname")
    public boolean isLastnameInputEnabled() {
        return !TextUtils.isEmpty(firstname);
    }
}

这不起作用。以下绑定始终为false且不会更改。

bind:enabled="{lastnameInputEnabled}"

怎么了?

1 个答案:

答案 0 :(得分:0)

查看RoboBinding AndroidMVVM示例,使用HasPresentationModelChangeSupport实现PresentationModelChangeSupport并使设置者调用firePropertyChange至关重要:

@PresentationModel
public class GreetingPresentationModel implements HasPresentationModelChangeSupport {
    PresentationModelChangeSupport changeSupport;

    @Override
    public PresentationModelChangeSupport getPresentationModelChangeSupport() {
        return changeSupport;
    }

    public GreetingPresentationModel() {
        changeSupport = new PresentationModelChangeSupport(this);
    }
    // Rest of the code here
    // Then change each setter, e.g.
    public void setFirstname(String firstname) {
        this.firstname = firstname;
        changeSupport.firePropertyChange("firstname");
    }
}