Robobinding is an Android open source framework that implements the data-bound Presentation model (MVVM) pattern. The MVVM pattern is an important update to the MVC pattern, making the project structure more graceful, easy to maintain, and easy to test. This is why several mainstream languages have a corresponding MVVM framework implemented such as Windows Phone,wpf,silverlight,javafx and Flex.
With no performance penalty (using ASPECTJ bytecode generation instead of Java reflection), Robobinding helps you write UI code that is more readable and easy to test and maintain. The framework has the following advantages:
-
Remove by binding-lots of unnecessary code (such as Addxxlistener (), Findviewbyid (), etc.).
-
The hard-to-test Android code and the long-running, unrealistic Android unit tests--become Pojo Presentationmodels and its normal junit unit tests.
-
Provides the object type cursor to replace-relationship type cursor because we are accustomed to manipulating objects.
-
It's easy to write property binding implementations for any custom component, third-party component, or Android widget, simplifying the code and making the project easy to maintain.
From a simple point of view, he removes unnecessary code such as Addxxlistener (), Findviewbyid (), even injectview like bufferknife do not need, because your code generally does not need to rely on these interface component information. The following is an example of the simplest ANDROIDMVVM.
Layout:
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2 3Xmlns:tools= "Http://schemas.android.com/tools"4 5xmlns:bind= "Http://robobinding.org/android" >6 7<TextView8 9bind:text= "{Hello}"/>Ten One ... A -<Button - theandroid:text= "Say Hello" - -bind:onclick= "SayHello"/> - +</LinearLayout>
Presentation Model:
1 Public classPresentationmodelextendsAbstractpresentationmodel {2 3 PrivateString name;4 5 PublicString Gethello () {6 7 returnName + ": Hello Android MVVM (Presentation Model)!";8 9 }Ten One ... A - Public voidSayHello () { - theFirepropertychange ("Hello"); - - } - +}
The activity binds layout to the corresponding presentation model.
1 Public classMainactivityextendsActivity {2 3 @Override4 5 protected voidonCreate (Bundle savedinstancestate) {6 7 ...8 9Presentationmodel Presentationmodel =NewPresentationmodel ();Ten OneView Rootview = Binders.inflateandbindwithoutpreinitializingviews ( This, R.layout.activity_main, Presentationmodel); A - Setcontentview (rootview); - the } - -}
This layout {Hello} is bound to Presentationmodel.hello, and the SayHello of layout is bound to the Presenationmodel.sayhello method. We don't need to define textview in layout, the button ID because we don't care, and it's not necessary. When we look further, we find that Presentationmodel is a pure POJO. This is why the software community, Martin Fowler, introduced the presenation model (MVVM) pattern in 2004. It is an upgrade of the MVC we are familiar with, which further decouples the interface state from the logic into the presentation model. We can learn robobinding using the following few sample projects, they can all be directly imported into Android studio without additional configuration:
1.AndroidMVVM, the smallest robobinding use example.
2.Album sample, is the original example of Martin Fowler's presentation model model based on Robobinding's Android translation version.
3.Gallery, is used to display the various features of robobinding including fragment, Menu, Viewpager, etc.
The Chinese document address for the project is: http://robobinding.github.io/RoboBinding/index.zh.html
Project home: http://www.open-open.com/lib/view/home/1410667351476
On Android MVVM frame robobinding