【问题标题】:Making DependsOn work in RoboBinding使 DependsOn 在 RoboBinding 中工作
【发布时间】:2015-09-23 06:13:46
【问题描述】:

在 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}"

怎么了?

【问题讨论】:

    标签: java android mvvm data-binding robobinding


    【解决方案1】:

    查看 RoboBinding AndroidMVVM 示例,使用PresentationModelChangeSupport 实现HasPresentationModelChangeSupport 并让setter 调用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");
        }
    }
    

    【讨论】:

    • 您可以添加 RoboBinding aspectJ 支持以避免手动编写 firePropertyChange 代码。它会自动为您编织 firePropertyChange 代码。
    • @Cheng 有没有办法可以从 ItemPresentationModel 中执行此操作或 firePropertyChange() ?
    • @Cheng btw 非常感谢您为 RoboBinding 所做的工作:)
    • @beerBear 你可以在 ItemPresentationModel 中做所有 PresentationModel 可以做的事情。 ItemPresentationModel 是 PresentationModel 的概念扩展。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2014-10-19
    相关资源
    最近更新 更多