ちょっと興味があったのでandrooid-binding使ってみました。
参考にしたサイトは以下の通りです。
このサイトで使われているのは「android.binding.0.2.jar」なので、今回は現時点で最新の「android-binding-0.45-update.jar」を使ってみます。
android-binding
説明の前にまずは今回触ったファイルを貼付け。
BMIApplication.java
public class BMIApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Binder.init(this);
}
}
BMIActivity.java
public class BMIActivity extends Activity {
private BMIPoJoViewModel viewModel = new BMIPoJoViewModel();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = Binder.bindView( this, Binder.inflateView( this, R.layout.main, null, false ), viewModel );
setContentView( v );
}
}
BMIPoJoViewModel.java
public class BMIPoJoViewModel implements PojoViewModel {
private PojoViewModelHelper helper = new PojoViewModelHelper();
public final StringObservable height = new StringObservable();
public final StringObservable weight = new StringObservable();
public final DoubleObservable bmi = new DoubleObservable(0);
@Override
public PojoViewModelHelper getHelper() {
return helper;
}
@Override
public void notifyPropertyChanged( String propertyName ) {
helper.notifyPropertyChanged( propertyName );
}
public Command Calculate = new Command() {
@Override
public void Invoke(View arg0, Object... arg1) {
double buf = Double.parseDouble( weight.get() ) / Math.pow( Double.parseDouble( height.get() ) / 100.0, 2 );
bmi.set( buf );
}
};
}
main.xml
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Height" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
binding:text="height" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
binding:text="weight" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
binding:text="bmi" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="計算"
binding:onClick="Calculate" />
</LinearLayout>
AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".BMIApplication" >
<activity
android:label="@string/app_name"
android:name=".BMIActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Binderの初期化はBMIApplication内で行なっています。AndroidManifest.xmlへの記述を忘れないようにしてください。
Binder.init(this)
次にビューのバインドに関してですが、setAndBindContentViewが@deprecatedになってしまっているので、bindViewを使用してみました。これで使い方あってるのかなぁ。
View v = Binder.bindView( this, Binder.inflateView( this, R.layout.main, null, false ), viewModel );
setContentView( v );
参考にしたサンプルから、BMIPoJoViewModelを大きく変えてしまいました。
StringObservableとDoubleObservableを使ってEditTextとバインドしています。
さらに、CalculateをCommandに変更しました。
現在のバージョンではViewModelのように双方向のバインドができているようです。
onTextChangedも拾えているので、試しに身長に175と入力した場合、ログには以下のように出力されます。
onTextChagnged, mV=null arg0=1
onTextChagnged, mV=1 arg0=17
onTextChagnged, mV=17 arg0=175
BMIに関しても値をセットすることでViewに反映されます。notifyPropertyChanged(“BMI”)のような記述は行なっていません。
bmi.set( buf );
ちょっと使っただけなので使い方が間違っているかもしれませんが、なかなか面白いライブラリですね。
これってFragmentでも使えるのかなぁ。