Android's practice MVVM + DataBinding frame mode

Recently simply learning MVVM framework to record it.

The results demonstrate: 

 

 

Analysis of its function in different frame configuration:

  • Frameless

 

 

Can be clearly felt in the absence of framework, although a separate Activity function can be realized, but the burden is too heavy, cumbersome code review time, once the need to modify, complex projects is extremely difficult to maintain.

  • MVC

In MVC framework, although the data acquisition and display interface separated, but for the Controller layer, still has many rights, with the increase in functionality, it will also greatly increase the amount of code, is not conducive to the maintenance changes.

  • MVP

 

 

 

 

MVP framework in use, is not in communication with Model View layer layer layer Presenter passing through, and with a particular View Presenter layer is not directly related, but interact through defined interfaces, which may lead to a large number of interface generation, code complex and cumbersome, difficult to maintain.

Therefore, when using the MVP according to certain specifications do best:

  1. Standardized interfaces (encapsulated interfaces parent to reduce the amount of interface)
  2. Using third-party plug-ins to automatically generate code for MVP
  3. For some simple interface, you can choose not to use frames
  4. The complexity of the item, the module may choose not to use part of the interface
  • MVVM

 MVVM framework implements the binding data and a view (the DataBinding), when the data changes, the view is automatically updated; Conversely, when the view changes, the data is automatically updated.

 

 

DataBinding Use these steps:

  • Enable DataBinding
  • Modify the layout file layout DataBinding
  • Data Binding

Use MVVM framework steps:

  • Provide View, ViewModel and Model Layer
  • The layout was modified layout DataBinding
  • View between and communicate through DataBinding ViewMedel
  • Data acquisition and display on the screen

Then a deeper level of learning, you can use LiveData + ViewModel

The following code is employed MVVM frame:

Account

 1 public class Account {
 2     String name;
 3     int level;
 4 
 5     public String getName() {
 6         return name;
 7     }
 8 
 9     public void setName(String name) {
10         this.name = name;
11     }
12 
13     public int getLevel() {
14         return level;
15     }
16 
17     public void setLevel(int level) {
18         this.level = level;
19     }
20 }

 

 

MCallBack

1 public interface MCallback {
2     public void onSuccess(Account account);
3     public  void onFailed();
4 }

 

 

MVVMActivity

 1 public class MVVMActivity extends AppCompatActivity {
 2     private ActivityMvvmBinding binding;
 3     private MVVMViewModel mvvmViewModel;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         binding = DataBindingUtil.setContentView(this,R.layout.activity_mvvm);
 9 
10         mvvmViewModel = new MVVMViewModel(getApplication(),binding);
11         binding.setViewModel (mvvmViewModel);   // initialize viewModel 
12 is      }
 13 is }

 

MVVMModel

 1 public class MVVMModel {
 2 
 3     //模拟查询账号数据
 4     public void getAccountData(String accountName, MCallback callback){
 5         Random random = new Random();
 6         boolean isSuccess = random.nextBoolean();
 7         if(isSuccess){
 8             Account account = new Account();
 9             account.setName(accountName);
10             account.setLevel(100);
11             callback.onSuccess(account);
12         }else {
13             callback.onFailed();
14         }
15     }
16 }

 

MVVMViewModel

 1 public class MVVMViewModel extends BaseObservable {
 2 
 3     private ActivityMvvmBinding binding;
 4     private MVVMModel mvvmModel;
 5     private String Input;
 6     private String result;
 7 
 8     @Bindable
 9     public String getResult() {
10         return result;
11     }
12 
13     public void setResult(String result) {
14         this.result = result;
15         NotifyPropertyChanged (com.example.mvvmdemo2.BR.result);
 16      }
 . 17      //     generally need to pass the Application object, easy to use application in the ViewModel
 18      //     such sharedpreferences required 
. 19      public MVVMViewModel (Application application, ActivityMvvmBinding Binding) {
 20 is          the this .binding = Binding;
 21 is          mvvmModel = new new MVVMModel ();
 22 is  
23 is      }
 24  
25      public  void the getData (View View) {
 26 is  
27          the Input = binding.etAccount.getText () toString ();.
 28         mvvmModel.getAccountData(Input, new MCallback() {
29             @Override
30             public void onSuccess(Account account) {
31                 String info = account.getName() + "|" + account.getLevel();
32                 setResult(info);
33             }
34 
35             @Override
36             public void onFailed() {
37                 setResult("消息获取失败");
38             }
39         });
40     }
41 }

 

xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <layout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools">
 5 
 6     <data>
 7         <variable
 8             name="viewModel"
 9             type="com.example.mvvmdemo2.mvvm.MVVMViewModel" />
10 
11     </data>
<1312 
     LinearLayout
14         android:layout_width="match_parent"
15         android:layout_height="match_parent"
16         tools:context=".mvvm.MVVMActivity"
17         android:orientation="vertical"
18         android:gravity="center">
19         <EditText
20             android:id="@+id/et_Account"
21             android:layout_width="match_parent"
22             android:layout_height="wrap_content"
23             android:hint="请输入账户名称"
24             android:layout_marginBottom="50dp"
25             />
26         <Button
27             android:id="@+id/btn_getAccount"
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:text="查询账户信息"
31             android:layout_marginBottom="50dp"
32             android:onClick="@{viewModel.getData}"
33             />
34 
35         <TextView
36             android:id="@+id/tv_getResult"
37             android:layout_width="wrap_content"
38             android:layout_height="wrap_content"
39             android:hint="暂未获取账户信息"
40             android:text="@{viewModel.result}"/>
41 
42     </LinearLayout>
43 </layout>

 

The network can provide a lot of information to us, personally I think that no amount of watching videos and articles as good as actually knocked to follow the code, which is more convenient to deepen understanding.

Thank you very much oohuo teacher video to explain, can be said to be very friendly for beginners, the students want to learn can learn to search for: Android in MVC_MVP_MVVM

Want to learn more about the difference between MVC, MVP, MVVM can see my previous blog: Android's MVC, MVP, MVVM

Guess you like

Origin www.cnblogs.com/jiani/p/12609272.html