かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

RoboBinding使うまで

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.
 */
@PresentationModel
public class MainViewModel implements HasPresentationModelChangeSupport {

    private PresentationModelChangeSupport support = new PresentationModelChangeSupport(this);

    private String message = "Hello robobinding.";

    public void sayHello() {
        this.setMessage("こんにちは世界");
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
        this.support.firePropertyChange("message");
    }

    @Override
    public PresentationModelChangeSupport getPresentationModelChangeSupport() {
        return this.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" />

    <Button
        android: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;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainViewModel vm = new MainViewModel();
        View view = Binders.inflateAndBind(this, R.layout.activity_main, vm);
        setContentView(view);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean 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 SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

gradle弱者なので、gradleの設定に凄い手間取った。