This is one of the best things I've seen on Android in a long time: MVVM architecture. Of course, Android platform doesn't support it, but there is hope! RoboBinding. OK, the spec perhaps isn't the best, but the idea is great! Finally
This is how simple it is with Android Studio:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
buildscript { repositories { mavenCentral() maven() { name 'RoboBinding AspectJPlugin Maven Repository' url "https://github.com/RoboBinding/RoboBinding-aspectj-plugin/raw/master/mavenRepo" //url "file://D:/git/RoboBinding-aspectj-plugin/mavenRepo" } } dependencies { classpath 'com.android.tools.build:gradle:1.+' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+' classpath 'org.robobinding:aspectj-plugin:0.8.4' } } apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' apply plugin: 'org.robobinding.android-aspectj' android { compileSdkVersion 21 buildToolsVersion "21.1.1" defaultConfig { minSdkVersion 8 targetSdkVersion 21 testApplicationId 'org.robobinding.albumsampletest' testInstrumentationRunner "android.test.InstrumentationTestRunner" testHandleProfiling true testFunctionalTest true } compileOptions { sourceCompatibility JavaVersion.VERSION_1_6 targetCompatibility JavaVersion.VERSION_1_6 } lintOptions { disable 'UnusedAttribute', 'ValidFragment', 'GradleDependency', 'OnClick', 'MissingPrefix', 'MenuTitle' } signingConfigs { release { storeFile file('robobinding.keystore') storePassword '123456' keyAlias 'robobinding' keyPassword '123456' } } buildTypes { release { minifyEnabled true proguardFiles 'proguard-rules.txt' signingConfig signingConfigs.release } } } repositories { mavenCentral() maven() { name 'SonaType snapshot repository' url 'https://oss.sonatype.org/content/repositories/snapshots' //url "file://D:/git/RoboBinding/framework/mavenRepo" } /* maven() { name 'RoboBinding Framework Maven Repository' url "file://D:/git/RoboBinding/framework/mavenRepo" } maven() { name 'RoboBinding CodeGen Maven Repository' url "file://D:/git/RoboBinding/codegen/mavenRepo" }*/ } ext { robobindingVersion = 'latest.integration' //robobindingVersion = '0.8.8' } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' compile ("org.robobinding:robobinding:$robobindingVersion:with-aop-and-dependencies") { exclude group: 'com.google.guava', module: 'guava' } //aspectPath fileTree(dir: 'libs', include: '*.jar') aspectPath ("org.robobinding:robobinding:$robobindingVersion:with-aop-and-dependencies") { exclude group: 'com.google.guava', module: 'guava' } apt "org.robobinding:codegen:$robobindingVersion" androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1' } |
Activity code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.example.associativelearning; import android.app.Activity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import viewmodels.MainActivityViewModel; import org.robobinding.binder.Binders; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainActivityViewModel presentationModel = new MainActivityViewModel(); View rootView = Binders.inflateAndBindWithoutPreInitializingViews(this, R.layout.activity_main, presentationModel); setContentView(rootView); } } |
Your ViewModel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@PresentationModel public class MainActivityViewModel { private String text; public String getText() { return text; } public void setText(String text) { this.text = text; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:bind="http://robobinding.org/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" bind:text="${text}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" bind:text="{text}"/> </RelativeLayout> |
What a beauty! Interestingly enough, the code doesn't work if you use ActionBarActivity
. I am not sure why.