Login
Immutable PageDiscussionInfoAttachments
alstamber/AndroidDevelopment/AndroidBinding

MMA

なにこれ

前提

Viewの定義

   1 mEditText = (EditText)findViewById(R.id.num1);

   1 <?xml version="1.0" encoding="utf-8"?>
   2 <LinearLayout
   3         xmlns:android="http://schemas.android.com/apk/res/android"
   4         xmlns:binding="http://www.gueei.com/android-binding/"
   5         android:layout_width="fill_parent"
   6         android:layout_height="fill_parent"
   7         android:orientation="vertical">
   8     <EditText
   9             android:contentDescription="height"
  10             android:layout_width="fill_parent"
  11             android:layout_height="wrap_content"
  12             android:inputType="number"
  13             binding:text="num1"/>
  14     <EditText
  15             android:contentDescription="weight"
  16             android:layout_width="fill_parent"
  17             android:layout_height="wrap_content"
  18             android:inputType="number"
  19             binding:text="num2"/>
  20     <EditText
  21             android:contentDescription="weight"
  22             android:layout_width="fill_parent"
  23             android:layout_height="wrap_content"
  24             android:inputType="number"
  25             binding:text="answer"/>
  26     <Button
  27             android:contentDescription="calc"
  28             android:layout_width="wrap_content"
  29             android:layout_height="wrap_content"
  30             android:layout_gravity="right"
  31             android:text="calc"
  32             binding:onClick="calculate"/>
  33 </LinearLayout>

Activityの定義

   1 public class MainActivity extends BindingActivity {
   2     /**
   3      * Called when the activity is first created.
   4      */
   5     @Override
   6     public void onCreate(Bundle savedInstanceState) {
   7         super.onCreate(savedInstanceState);
   8 
   9         MainViewModel model = new MainViewModel();
  10         setAndBindRootView(R.layout.main, model);
  11     }
  12 }

ViewModelの定義

   1 public class BmiViewModel {
   2 
   3     public final StringObservable num1 = new StringObservable();
   4 
   5     public final StringObservable num2 = new StringObservable();
   6 
   7     public final StringObservable answer = new StringObservable();
   8 
   9     public final Command calculate = new Command() {
  10         @Override
  11         public void Invoke(View arg0, Object... arg1) {
  12             double result = Calc.calc(Double.parseDouble(num1.get()), Double.parseDouble(num2.get()));
  13             .set(String.format("%.1f", result));
  14         }
  15     };
  16 }

Modelの定義

   1 public class Calc
   2     public static double calc(double num1, double num2){
   3         return num1 + num2;
   4     }
   5 }

ViewModelからViewに仕事を移譲する

   1 EventAggregator eventAggregator = EventAggregator.getInstance(this);

   1 public void setEventAggregator(EventAggregator ea){
   2     mEventAggregator = ea;
   3 }

   1 model.setEventAggregator(eventAggregator);

   1 eventAggregator.subscribe("hoge", new EventSubscriber(){
   2     @Override
   3     public void onEventTriggered(String arg0, Object arg1, Bundle arg2){
   4                                 
   5     }
   6 });

   1 mEventAggregator.publish("hoge", MainActivityModel.this, null);

alstamber/AndroidDevelopment/AndroidBinding (last edited 2013-03-30 19:16:04 by alstamber)