Is it possible to bind one ObservableField to another?

前端 未结 3 1411
星月不相逢
星月不相逢 2021-02-04 07:49

I understand that the purpose of Android\'s data-binding library is for views to observe data and automatically update when that data changes.

Question:

3条回答
  •  野的像风
    2021-02-04 08:22

    Use @Bindable annotation and the Observable interface. It avoids the boilerplate code and u can use it for primitive data types.

    /**
     * Created by Amardeep on 11/2/16.
    */
    public class Cart implements Observable {
    
    private final PropertyChangeRegistry mPropertyChangeRegistry = 
    new PropertyChangeRegistry();
    
    @Bindable
    private int itemCount;
    
    @Bindable 
    private String name;
    
    
    public int getItemCount() {
        return itemCount;
    }
    
    public void setItemCount(int itemCount) {
        this.itemCount = itemCount;
        mPropertyChangeRegistry.notifyChange(this, BR.itemCount);
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
        mPropertyChangeRegistry.notifyChange(this, BR.name);
    }
    
    @Override
    public void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
    
        mPropertyChangeRegistry.add(callback);
    }
    
    @Override
    public void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
    
        mPropertyChangeRegistry.remove(callback);
    }
    }
    

提交回复
热议问题