2017-03-28 32 views
0

我正在使用MVVM設計模式。 我想使用dataBinding或RXJava來在模型發生變化時通知視圖。 dataBinding可以在xml之內完成。 但我想通知活動對模型進行更改並執行更復雜的操作。 讓我們假設我希望我的TextView在文本不爲空時更改顏色。 你能幫我通過dataBinding或RXJava做到嗎?android-使用dataBinding,在Activity上而不是佈局

這裏是我的代碼:

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> 

活動

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); 
     } 
    } 

個視圖模型

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); 
    } 

} 
+1

*但我想通知活動模型中的變化,並做一些更復雜的事情*。爲什麼是活動?虛擬機獲取更改並通知屬性。這應該是全部。如果綁定不是*本地*支持的,你可以使用'@ BindingAdapter'來添加你自己的 – Blackbelt

+0

謝謝你的答案Blackbelt。 我閱讀了@BindingAdapter,並意識到它只能在setter中使用。 但我需要它在一個吸氣。 如果我想在我的模型中顯示/隱藏視圖 – ediBersh

+0

的一些布爾標誌,請在您的''中添加'android:background =「@ {viewModel.textViewColor}」'並添加@Bindable int getTextViewColor( )'MyViewModel'中的'方法 – pskink

回答

1

你可以做的就是創建一個回調機制來觸發你的活動的方法。這仍然會首先通知ViewModel,但效果會一樣。

您可以創建一個新的接口(或增加它的視圖模型內)

public interface ViewModelCallback{ 
    void ActionCallback(); 
} 

在你的活動:

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); 
} 

呼叫notifyCallback後

public class MainActivity extends AppCompatActivity implements ViewModelCallback 

在您的視圖模型您NotifyPropertyChanged()。 不要忘記你的活動添加爲回調的onCreate

viewModel.addCallback(this) 

你也應該刪除它的onDestroy。

+0

謝謝, 但我不希望我的視圖模型引用該活動。 它違背了MVVM原則。 – ediBersh

+0

我不確定,這只是一個回調,並沒有使用適合Android的代碼。你很快就會意識到這種技巧並不意味着你的應用不遵循MVVM原則(就像WPF中的代碼一樣,後面的代碼並不意味着違反了MVVM) –

相關問題