RoboBinding使うまでにはまったのでメモ。
appの中のbuild.gradleに対して以下の記述を追記します。
apply plugin: 'com.android.application'
// 追記ここから
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
}
}
apply plugin: 'com.neenbedankt.android-apt'
// 追記ここまで
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.kazuki.myapplication"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
// 追記ここから
compile"org.robobinding:robobinding:0.8.9"
apt "org.robobinding:codegen:0.8.9"
// 追記ここまで
}
あとは、getting startでもみて適当にViewModelこしらえて
package com.example.kazuki.myapplication;
import org.robobinding.annotation.PresentationModel;
import org.robobinding.presentationmodel.HasPresentationModelChangeSupport;
import org.robobinding.presentationmodel.PresentationModelChangeSupport;
Created by Kazuki on 2015/01/07.@PresentationModelpublicclass MainViewModel implements HasPresentationModelChangeSupport {
private PresentationModelChangeSupport support = new PresentationModelChangeSupport(this);
private String message = "Hello robobinding.";
publicvoid sayHello() {
this.setMessage("こんにちは世界");
}
public String getMessage() {
return message;
}
publicvoid setMessage(String message) {
this.message = message;
this.support.firePropertyChange("message");
}
@Overridepublic PresentationModelChangeSupport getPresentationModelChangeSupport() {
returnthis.support;
}
}
Viewとバインドして
<RelativeLayout xmlnsandroid="http://schemas.android.com/apk/res/android"xmlnstools="http://schemas.android.com/tools"androidlayout_width="match_parent"xmlnsbind="http://robobinding.org/android"androidlayout_height="match_parent"androidpaddingLeft="@dimen/activity_horizontal_margin"androidpaddingRight="@dimen/activity_horizontal_margin"androidpaddingTop="@dimen/activity_vertical_margin"androidpaddingBottom="@dimen/activity_vertical_margin"toolscontext=".MainActivity"><TextView androidlayout_width="wrap_content"androidlayout_height="wrap_content"bindtext="{message}"androidid="@+id/textView" /><Buttonandroidlayout_width="wrap_content"androidlayout_height="wrap_content"androidtext="New Button"androidid="@+id/button"androidlayout_below="@+id/textView"androidlayout_alignParentLeft="true"androidlayout_alignParentStart="true"bindonClick="sayHello"/></RelativeLayout>
Activityで紐づける。
package com.example.kazuki.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import org.robobinding.binder.Binders;
publicclass MainActivity extends ActionBarActivity {
@Overrideprotectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainViewModel vm = new MainViewModel();
View view = Binders.inflateAndBind(this, R.layout.activity_main, vm);
setContentView(view);
}
@Overridepublicboolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
returntrue;
}
@Overridepublicboolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
}
gradle弱者なので、gradleの設定に凄い手間取った。