なか日記

一度きりの人生、楽しく生きよう。

Android Bindingを使ってみた(1)

@さんが最近使われたのを見てて、自分も使ってみました。
発展した使い方は@さんの資料「」に載っていますが、まずは簡単な方法で使ってみたい思います。

android-binding
Providing a framework that enabes the binding of android view widgets to data model. It helps to implement MVC or MVVM patterns in android applications.

ソース一式はこちら
https://github.com/nakaji/AndroidBindingSample

ダウンロードとビルドパスへの追加

ダウンロード

ダウンロードサイトからandroid.binding.0.2.jarをダウンロードします。

ビルドパスへの追加

eclipseAndroidプロジェクトのビルドパスにandroid.binding.0.2.jarを追加します。

レイアウト(View)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:binding="http://www.gueei.com/android-binding/"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
	<TextView
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:text="身長"
		/>
	<EditText
		android:id="@+id/height"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:inputType="numberDecimal"
		/>
	<TextView
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:text="体重"
		/>
	<EditText
		android:id="@+id/weight"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:inputType="numberDecimal"
		/>
	<Button
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:text="計算"
		binding:onClick="Calculate"
		/>
	<TextView
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		binding:text="BMI"
		/>
</LinearLayout>

計算ボタンのonClickイベントとBMI表示用のtextでバインドを行ってます。

アクティビティ

import gueei.binding.Binder;
import gueei.binding.Command;
import gueei.binding.observables.StringObservable;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class AndroidBindingSampleActivity extends Activity {

    private EditText heightText;
    private EditText weightText;

    public StringObservable BMI = new StringObservable("");

    public Command Calculate = new Command() {
        @Override
        public void Invoke(View arg0, Object... arg1) {
            double height = Integer.valueOf(heightText.getText().toString());
            double weight = Integer.valueOf(weightText.getText().toString());
            double bmi = weight / Math.pow(height / 100.0, 2);
            BMI.set("Your BMI is " + String.valueOf(bmi));
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Binder.init(this.getApplication());
        Binder.setAndBindContentView(this, R.layout.main, this);

        heightText = (EditText) findViewById(R.id.height);
        weightText = (EditText) findViewById(R.id.weight);
    }
}

onCreateのBinder.setAndBindContentViewで自分自身をViewModelとして登録しています。
Viewがバインドしているコマンドやオブジェクトはpublicにして公開しています*1

何が良くなったか

  • findViewByIdして、計算ボタンの処理を割り当てる必要がなくなった
  • BMI表示のためにfindViewByIdして、setTextしなくて良くなった
  • findViewByIdしなくて良くなったので、上記2つのコントロールについてIDを振る必要がなくなった

この例ではあまりメリットが感じられない気がしますね。
もっと上手く使うと、Androidに依存する部分がきれいに切り離せてUnitTestが行える様になるようなので勉強します。

*1:この辺はちょっと微妙な気がします