MVVM:基礎知識(2)

7402 ワード

Data Binding
一、概念
Data bindingが2015年7月に発表したAndroid Studio v 1.3.0バージョンで導入、2016年4月にAndroid Studio v 2.0.0リリースで正式にサポートされているのは、supportライブラリ(データとインタフェースのバインドを実現し、双方向バインドをサポート)であり、最低でもAndroid 2.1(API Level 7+)をサポートしています.
二、作用
Data Bindingを使用する前に、findView ByyId()、setText()、setVisibility()、setEnabled()、setOnClickListener()などの多くの重複コードを記述することは避けられません.データBindingにより、大量の重複コードを記述することなく、データとインタフェースのバインドを簡素なコードで実現することができます.
三、使用
1.データ変更時の通知画面の更新
(1)データクラス実装データクラス実装方式1:データクラスに以下のクラスを使用させる:ObservableField,ObservableBoolean,ObservableByte,ObservableChar,ObservableShort,ObservableInt,ObservableLong,ObservableFloat,ObservableDouble,ObservableParcelable
public class User {
    public ObservableField username = new ObservableField<>();
    public ObservableInt age = new ObservableInt();
    public ObservableInt rp = new ObservableInt();

    public User(String username, int age, int rp){
        this.username.set(username);
        this.age.set(age);
        this.rp.set(rp);
    }
}

データクラス実装方式2:BaseObservableをデータクラスに継承させ,通知が必要な属性に@Bindableを付け,通知が必要な属性のsetメソッドにnotifyPropertyChangedメソッドを呼び出すことでインタフェースリフレッシュを通知する.
public class User extends BaseObservable {
    @Bindable
    private String username;
    @Bindable
    private int age;
    @Bindable
    private int rp;

    public User(String username, int age, int rp){
        this.username = username;
        this.age = age;
        this.rp = rp;
    }

    public String getUsername() {
        return username;
    }

    public int getAge() {
        return age;
    }

    public int getRp() {
        return rp;
    }

    public void setUsername(String username){
        this.username = username;
        notifyPropertyChanged(com.tomorrow.mvvm.BR.username);
    }

    public void setAge(int age) {
        this.age = age;
        notifyPropertyChanged(com.tomorrow.mvvm.BR.age);
    }

    public void setRp(int rp) {
        this.rp = rp;
        notifyPropertyChanged(com.tomorrow.mvvm.BR.rp);
    }
}

(2)Viewレイヤレイアウトファイル:
//activity_rpcalculator.xml



    
    



    

    
        

        

    

    
        

        

    

    

Activity:

//RpCalculatorActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "zwm, onCreate");
    ActivityRpcalculatorBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_rpcalculator);
    userViewModel = new UserViewModel(this, this, binding);
}

(3)ViewModel層
//UserViewModel
public UserViewModel(Context context, IRpCalculatorView rpCalculatorView, ActivityRpcalculatorBinding binding){
    this.context = context;
    this.rpCalculatorView = rpCalculatorView;
    this.binding = binding;
    init();
}

private void init(){
    Log.d(TAG, "zwm, init");
    dataModel = new DataManager(context);

    //    
    user = new User(null, -1, 0);
    binding.setUser(user);
    //    
    binding.setUserViewModel(this);
}

public void onClick(View view) {
    int id = view.getId();
    switch (id){
        case R.id.calculate_btn:
            getRp();
            break;
    }
}

public void updateRp(User user){
    Log.d(TAG, "zwm, updateRp");
    //       1,        :
    //this.user.username.set(user.username.get());
    //this.user.age.set(user.age.get());
    //this.user.rp.set(user.rp.get());

    //       2,        :
    this.user.setUsername(user.getUsername());
    this.user.setAge(user.getAge());
    this.user.setRp(user.getRp());
}

2.インタフェース更新時の通知データ変更
データ変更時通知インタフェースの更新とインタフェース更新時通知データの変更の実装方法の違い:レイアウトファイルで「@{}」を「@={}」に変更します.
//           
 //@{}

//           
 //@={}