Design MVVM for Android

In the recently released book, Best Recommendations for Android, I read that MVVM is a good design pattern for android programming. Having tried it myself in my last project, it seems that it is beneficial to split the code into more manageable sections.

The view only handles the creation of view and interface elements with the ViewModel. ViewModel implements the interface and processes operations on the presentation and interaction with the Model. Sample code below:

Model

 public class MyModel{
    public String myString;
    public MyModel(String myString){
       this.myString = myString;
    }
}

View

public class MyActivity{

    public ViewManager delegate;

    public interface ViewManager{
        void registerTextView(TextView tvText);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        delegate = new ViewController(this);
        TextView tvText = (TextView) view.findViewById(R.id.tvText);
        delegate.registerTextView(tvText);
    }
}

ViewModel

 public class ViewController implements MyActivity.ViewManager{
     Context activity;
     TextView tvText;
     MyModel myModel;

     public ViewController(Context app_context){
        activity = app_context;
        myModel = new MyModel("Hello World");
     }

    @Override
    public registerTextView(TextView tvText){
        this.tvText = tvText;
        tvText.setText(myModel.myString);           
    }
 }

However, I have not seen this approach anywhere on the Internet and cannot find much information that supports it as a good design template for Android. I also have a few questions, such as:

ViewModel ?

? , fragmentManager?

, ?

- - - , MVVM???

+4
3

. , , , MVVM ( . MVVM ). POJO ViewModel ( Presentaion), ViewModels maxmium. , . , , Android. , - , AndroidMVVM, .

: ( + ), ViewModel, (-: , ..). , , , ViewModel.

. , . , .

+4

MVVM Android

enter image description here

- , Android Android 2.1

android {
    ....
    dataBinding {
        enabled = true
    }
}

Android.

Advance Guide

+4

Android MVVM. .

https://github.com/manas-chaudhari/android-mvvm

:

  • XML/View ViewModel, XML ViewModel
  • ViewModel . ViewModels
  • ViewModel ↔ View

Related Blog Post : https://manaschaudhari.com/blog/2016/08/19/rxjava-meets-data-binding-part-3

+1

Source: https://habr.com/ru/post/1547917/


All Articles