Android MVVM Development mode (i)

Source: Internet
Author: User


Android MVVM Development mode
Concept
MVVM is a change in the MVP architecture, and the goal is to link some of the changes in the view with the model so that the model's data changes and is directly notified to the view to solve the MVP The interface between the v-p inside the schema is too heavy for the problem.
So the core of MVVM solves the problem: to weaken the direct relationship of V-p, and use the binding method (dataBinding) to directly feed the model changes to the view.
For a complete databinding explanation, please see here
Https://github.com/LyndonChin/MasteringAndroidDataBinding
What is the problem solved? One aspect is the simple use of the databinding approach, one that explains how it is done internally.
Meaning of MVVM:
MVVM refers to model view and ViewModel.
Model is data implementation and logic processing.
View is the interface display.
ViewModel creates an association that binds the model and view together. After that, our model changes are fed back to the view via ViewModel. (The view XML layout file, after the specific writing, the compilation tool processing, the generated code will receive the ViewModel data notification message, automatically refreshes the interface)

Say how useless, look directly at the example:
1 Build.gradle in the project

2 in the Build.gradle of the module
Join DataBinding
3 Creating a User class

There's a property name in it, and we'll use it for a while.
4 Writing Activity_main.xml layout files

Layout root node changed to layout
Join the Data node
Write a variable with the name User, type Com.xm.mvvmdemo.Model.User, which is the position of the Class 3.
Write @{user.name} on TextView property Android:text
Here this action, after compiling the code will generate similar: Textview.settext (User.getname ()); it's actually a bit special.
5 Modify inside the mainactivity

Replace Setcontentview (R.layout.activity_main) with Databindingutil.setcontentview (this, r.layout.activity_main), Returned is the generated binding class activitymainbinding (this is our XML name Activity_main Remove the connection, capitalize the first letter, and stitch together a binding composition. )
Here to change to this databindingutil.setcontentview, what do the internal actions? After the default Setcontentview action, a binding action is made that associates the view with the data. Depending on how it relates, you can see the activitymainbinding generated class.

Create a user, set the value. Then bind.
By passing our data back to view through Binding.setuser (), we can assign the value directly to it through our auto-generated code.

Now let's brush up on this idea.
Change the XML, compile it, come out with a real layout XML and a binding code (here is Activitymainbinding.java), Here activitymainbinding inside is generated to the XML inside the binding place description to code: (Here Android:text: "@{user.name}") Born into

The first parameter is our TextView class, and the second value we see is the value of getname from the instance of the user above.
How the first parameter comes in:

Change Setcontentview single action, change to Databindingutil.setcontentview. The idea of this has been mentioned above.

Now that we have written the binding XML and replaced the Setcontentview, after the two actions have been completed, our binding class has already bound the view and the required data, so do we need to assign a value?

Binding.setuser (user), the data is assigned to the past, in this way, the information is fed back to the interface.

6 Summary
In fact, when used, the first impression of my XML can write what Ah, this is my first question.
As we choose to project mode, pull to the bottom of the place, see external Libraries, you can see a adapters-1.1.jar, open to see all the available properties.

I want to define my own properties, the default system is not enough?
Well, you can write a static method labeled @bindingadapter. Randomly, the compiler will find this comment for itself.

Note: The compilation process of the binding we don't care, we treat it as a black box, and look at it to deal with the before and after comparison of our layout XML.

Here we see that the name and type are tagged in the data node, and then the properties underneath the name are used. We have found that we only need to agree on this association method, which can be generated automatically.
Therefore, the acquisition of our default properties is obtained through get. Here User.Name becomes uset.getname () about this android:text attribute, the default is the corresponding set method, here is not using the default, why? Because in order to be rigorous, you need to do assignment filtering. The system provides specific methods by default:

Let's look at the contrast between this layout and the resulting layout:

The layout and data are removed directly from the build. Android:text also removed, where do these go? (The answer is that this layout XML generates a real layout file and a code to manage bindings, this is activitymainbinding)
Activitymainbinding mainly completes a few things 1: will be inside the need to bind the view instance to get. 2: Generate the associated code for the view and model. (i.e. how the model should be displayed on the view)
Having said so much, once again summed up what DataBinding did:
Based on the agreed XML (layout as the root node), the real XML and a binding class are generated, and the binding class gives us the rules for updating the view and data between the interfaces.
Then we replace Setcontentview with Databindingutil.setcontentview, because it does an action on the basis of Setcontentview, is to give our binding class the rules for updating the view and data in this interface directly.
As a result, we found that the Findviewbyid and settext we were going to write were automatically generated by the tool, and we wrote the actions into the layout so that the tools helped us code generation, freeing up tedious and repetitive interface operations.
Well, first of all, this architecture has to be written too much, so please look forward to it.

Android MVVM Development mode (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.