mvvm - android - using dataBinding, on Activity instead of layout -


i working in mvvm design pattern. want use databinding or rxjava in order notify view when model has changed. databinding can inside xml. want notify activity on change in model , more complicated. let's assume want textview change color when text in not empty. can me via databinding or rxjava ?

here code:

xml

<?xml version="1.0" encoding="utf-8"?> 

<data>      <variable         name="viewmodel"         type="edi.com.mydatabindingsimple.myviewmodel" /> </data>  <linearlayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <edittext         android:id="@+id/edittext"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:ems="10"         android:inputtype="textpersonname"         android:text="@={viewmodel.txt}" />      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@={viewmodel.txt}" />  </linearlayout> 

activity

import android.databinding.databindingutil; import android.os.bundle; import android.support.v7.app.appcompatactivity;  import edi.com.mydatabindingsimple.databinding.activitymainbinding;      public class mainactivity extends appcompatactivity {          myviewmodel viewmodel = new myviewmodel();          @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             final activitymainbinding binding = databindingutil.setcontentview(this, r.layout.activity_main);             binding.setviewmodel(viewmodel);         }     } 

viewmodel

    import android.databinding.baseobservable; import android.databinding.bindable;    public class myviewmodel extends baseobservable{     private string txt;      @bindable     public string gettxt() {         return txt;     }      public void settxt(string txt) {         this.txt = txt;         notifypropertychanged(edi.com.mydatabindingsimple.br.txt);     }  } 

what can create callback mechanism trigger method in activity. still notify viewmodel first, effect same.

you can create new interface (or add inside viewmodel)

public interface viewmodelcallback{    void actioncallback(); } 

in activity:

public class mainactivity extends appcompatactivity implements viewmodelcallback 

in viewmodel

private arraylist<viewmodelcallback> callbacks = new arraylist<>();  public void notifycallbacks(){   for(viewmodelcallback c : callbacks){      if(c != null){          c.actioncallback       }    } }  public void addcallback(viewmodelcallback c){      callbacks.add(c); } 

call notifycallback after notifypropertychanged(). , don't forget add activity callback in oncreate

viewmodel.addcallback(this) 

you should remove in ondestroy.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -