Android Basics--The practice of databinding of frame mode MVVM

Source: Internet
Author: User

This article contains the following content:

  • Introduction to MVVM
  • The practice of MVVM
    • Use of the layout label of DataBinding
    • DataBinding data and the use of variable tags
    • DataBinding the use of fixed-point hit events

MVVM is a shorthand for Model-view-viewmodel, which provides two-way data binding to the view and the view model, allowing the state change of the view model to be automatically passed to the view

  • Model: Data layer, which handles loading or storing of data
  • View: Views layer, responsible for the display of interface data, interaction with the user
  • ViewModel: Responsible for completing the interaction between view and model, responsible for business logic

Model Diagrams for MVVM:

MVVM Pros:

  • Low coupling. Views can be independent of model changes and modifications, a viewmodel can be bound to different "view", when the view changes the model can be unchanged, when the model changes when the view can be unchanged.
  • Reusability. You can put some view logic in a viewmodel and let many views reuse this view logic.
  • Independently developed. Developers can focus on business logic and Data Development (ViewModel), designers can focus on page design, and using Expression Blend makes it easy to design interfaces and generate XML code.
  • can be tested. The interface is always more difficult to test, and now the test can be written for ViewModel.

Using databinding to support the MVVM framework mode on Android, let's use databinding to practice

Practice Tool: Android Studio 2.1

Preparation: Open the DataBinding in Android and need to be declared in the Build.gradle under module

  • Layout label function: As a mark of databinding, omit Findviewbyid () method

The first step: we take the student management information as an example, we nest a layer of layout file in the outer layers of a layout label, and attach a namespace, in EditText and TextView to add the corresponding ID

Step two: In our main activity, we need to bind our XML layout through Databindingutil.

The Databindingutil.setcontentview () method returns a data-bound object whose naming rules are automatically generated by the system, because our layout name is Activity_ Login.xml, so generate the rule: Remove the underscore, extract the XML name (letter capital) +binding

The third step: if we need to manipulate the XML view, we do not need Findviewbyid (), directly call the binding object inside the view, its control of the naming rules: Remove the underscore, extract the XML name (letter capital)

  • The role of data and variable tags: passing objects into the layout XML file

The first step: because the layout tag also needs to use view in the binding object, does not fully reflect the real effect of MVVM, the following in the layout file declaration data and variable tag

In variable, we take a name and point its type to a Bean object, which is bound to the object, and, when used, binds the object's properties to the control in the format of @{}.

Step two: In the main activity, just set this object to

  • Bind ontextchanged Event: We update TextView with EditText modification
  • Bind onclick Event: Click TextView to eject Toast

The first step: we create a controller class in the main activity, kite note that the name and parameters of this ontextchange method must be the same as EditText native Ontextchange, and the OnClick method is

Second step: In the layout file, declare this class, and give EditText add ontextchanged event, to TextView plus onclick event

Note that controller.ontextchanged and Controller.onclick must correspond to the name of the controller, the controller inside the method name can be arbitrarily taken, but the parameters must be consistent with the original method parameters, or compile error

Step three: Set up this controller in the main activity

  • Binding Onclicklistener Events: passing XML objects to activity, which is the embodiment of MVVM bidirectional data binding

The first step: We continue in the main activity of the Controller class, create the method

Step Two: We use this method in TextView and pass the student object to the activity

Here we use lambda expression, it is difficult to understand, you know.

Source

Github:https://github.com/androidhensen/design-mode

Android Basics--The practice of databinding of frame mode MVVM

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.