The first article talked about DataBinding, which comes back to the architecture of shuoshuoMVVM

juejin.cn/post/685999…

So databinding is basically a tool for MVVM, for communication between a View and a ViewModel, and in development I used an Activity and a ViewModel, and acrivity was responsible for initialization, UI operations, popups and all that stuff, and ViewModel was responsible for holding data, Network requests, logical operations, and ultimately the viewModel takes over the business logic, and drives UI changes. How does Google demonstrate this? They use the LiveData class, such as baseViewModel:

/** * UI changes LiveData, Public Final Class UIChangeLiveData extends SingleLiveEvent {private SingleLiveEvent<String> showToastEvent; private SingleLiveEvent<String> showLoadingEvent; private SingleLiveEvent<Void> dismissLoadingEvent; private SingleLiveEvent<Map<String, Object>> startActivityEvent; private SingleLiveEvent<Map<String, Object>> startContainerActivityEvent; private SingleLiveEvent<Void> finishEvent; private SingleLiveEvent<Void> onBackPressedEvent; public SingleLiveEvent<String> getShowToastEvent() { return showToastEvent = createLiveData(showToastEvent); } public SingleLiveEvent<String> getShowLoadingEvent() { return showLoadingEvent = createLiveData(showLoadingEvent); } public SingleLiveEvent<Void> getDismissLoadingEvent() { return dismissLoadingEvent = createLiveData(dismissLoadingEvent); } public SingleLiveEvent<Map<String, Object>> getStartActivityEvent() { return startActivityEvent = createLiveData(startActivityEvent); } public SingleLiveEvent<Map<String, Object>> getStartContainerActivityEvent() { return startContainerActivityEvent = createLiveData(startContainerActivityEvent); } public SingleLiveEvent<Void> getFinishEvent() { return finishEvent = createLiveData(finishEvent); } public SingleLiveEvent<Void> getOnBackPressedEvent() { return onBackPressedEvent = createLiveData(onBackPressedEvent);  } private <T> SingleLiveEvent<T> createLiveData(SingleLiveEvent<T> liveData) { if (liveData == null) { liveData = new SingleLiveEvent<>(); } return liveData; } @Override public void observe(LifecycleOwner owner, Observer observer) { super.observe(owner, observer); }}Copy the code

Many events are defined, such as pop-ups, loading, toast, back button listening, etc., to enable the Activity to refresh the UI by changing data.

Another example is a network request. In the viewModel, you start requesting data, get data, return this LiveData to the activity, and finally the activity gets the requested data

LoginActivity:

/** * login button */ fun login(view: View) {viewmodel.login ().observe(this, Observer {loginResult -> .name) }) }Copy the code

LoginViewModel:

Public MutableLiveData<LoginResponse> login(){HashMap<String,Object> map = new HashMap(); map.put("phoneNo",phoneNo.get()); map.put("captcha",captcha.get()); final MutableLiveData<LoginResponse> data = new MutableLiveData<>(); Disposable = rxhttp.postform (api.login)// interface name. AddEncoded ("data", New Gson().tojson (map))// Post data.asBaseresponse (loginResponse.class)// Callback data type Compose (RxUtils.com monTransformer ()) / / thread of execution and failures retry. Compose (getLoadingTransformer ()) / / loading popup window .subscribe((Consumer<LoginResponse>) response -> { data.setValue(response); MsgLog. Set (" login successful "+ response. GetName () +" : "+ response. GetPhoneNo ()); LoginUtil.saveLoginInfo(response); LoginUtil.saveAuth(response.getAuth()); LoginUtil.saveLoginPhoneNo(response.getPhoneNo()); }, (Consumer<Throwable>)throwable -> showToast(throwable.getMessage()) ); // Add to the lifecycle addSubscribe(disposable); return data; }Copy the code

In this case, data is a LiveData. We return this LiveData to the activity. When the interface data is returned, we set the value of data.

Not only activities can be used, but also custom layout controls such as dialog can be used.

  mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.dialog_select, null, false);
    setContentView(mBinding.getRoot());
    SelectDialogBean data = new SelectDialogBean();
    mBinding.setData(data);
Copy the code

Conclusion: Databinding and liveData are both part of Google’s jetpack toolkit, which is easy to use. In particular, you can write a lot of the original UI code in XML layout code, which greatly improves the efficiency of UI development and takes the tedious UI code away from the business logic. Make XML not just a static layout file, but a dynamic display of page changes.