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 xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"xmlns:bind="http://robobinding.org/android"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"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"bind:text="{message}"android:id="@+id/textView" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/button"android:layout_below="@+id/textView"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"bind:onClick="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) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); returntrue; } @Overridepublicboolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId(); //noinspection SimplifiableIfStatementif (id == R.id.action_settings) { returntrue; } returnsuper.onOptionsItemSelected(item); } }
gradle弱者なので、gradleの設定に凄い手間取った。