Practice of Android MVVM+DataBinding framework mode

Recently, I simply learned the MVVM framework and recorded it.

Results demonstration:

 

 

Analyze the composition of its functions under different frameworks:

  • Frameless

 

 

It can be clearly felt that in the absence of framework, although a single Activity can achieve the function, it is overburdened and the code review is tedious. Once it needs to be modified, complex projects are extremely difficult to maintain.

  • MVC

Under the framework of MVC, although the data acquisition and interface display are separated, the Controller layer still has a lot of rights. With the increase of functions, the amount of code will increase greatly, which is not conducive to maintenance and modification.

  • MVP

 

 

 

 

When using the MVP framework, the View layer does not communicate with the Model layer, all of which are passed through the Presenter layer, and the Presenter layer is not directly related to the specific View, but interacts through the defined interface, which may lead to a large number of interface generation, complex code and difficult maintenance.

Therefore, when using MVP, it is better to follow certain specifications:

  1. Interface normalization (encapsulating the parent interface to reduce interface usage)
  2. Automatically generate MVP code using third-party plug-ins
  3. For some simple interfaces, you can choose not to use the framework
  4. According to the complexity of the project, some modules can choose not to use the interface
  • MVVM

The MVVM framework implements the data binding between data and view. When the data changes, the view will update automatically; otherwise, when the view changes, the data will update automatically.

 

 

To use DataBinding:

  • Enable DataBinding
  • Modify layout file to DataBinding layout
  • Data binding

To use the MVVM framework:

  • Provide three layers of View, ViewModel and Model
  • Change layout to DataBinding layout
  • View and ViewMedel communicate through DataBinding
  • Get the data and show it on the interface

For further learning, you can use LiveData+ViewModel

Here is the code using the MVVM framework:

 

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);  //Initialization viewModel
12     }
13 }

 

MVVMModel

 1 public class MVVMModel {
 2 
 3     //Analog query account data
 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, you need to transfer in Application Object, convenient in ViewModel Use in application
18     //    such as sharedpreferences Need to use
19     public MVVMViewModel(Application application, ActivityMvvmBinding binding) {
20         this.binding=binding;
21         mvvmModel = new MVVMModel();
22 
23     }
24 
25     public void getData(View view){
26 
27         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("Message get failed");
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>
12 
13     <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="Please enter account name"
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="Query account information"
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="No account information has been obtained"
40             android:text="@{viewModel.result}"/>
41 
42     </LinearLayout>
43 </layout>

 

The Internet can provide us with a lot of information. I think it's better to follow the code when watching more videos and articles, so it's easier to understand.

Thank you very much for your video explanation. It's very friendly to beginners. Students who want to learn can search and learn: MVC? MVP? MVVM in Android

To learn more about the differences between MVC, MVP, and MVVM, see my previous blog: MVC, MVP and MVVM of Android

Tags: Android xml encoding

Posted on Sat, 04 Apr 2020 17:54:03 -0700 by rowantrimmer