0

データバインディングライブラリを使用して新しいアプリケーションを実装しています。残念ながら、(android.support.v4.widget.DrawerLayoutを使用して)ナビゲーションドロワーで双方向バインディングの例を見つけることはできません。引き出しレイアウトには、いくつかの可変テキストと2つのトグルスウィッチが含まれています(android.support.v7.widget.SwitchCompatを使用)。NavigationDrawerのAndroid双方向データバインディング

このシナリオでデータバインディングを使用するのは意味がありますか?これを実装する方法に関するアイデア?誰もこの種の実装のためのいくつかのサンプルコードを持っていますか?

+1

これに関するアップデートは何ですか? – vjamit

+0

あなたは私の[答え]を参照することができます(http://stackoverflow.com/questions/33962548/how-to-data-bind-to-a-header/38846146#38846146)または[ブログ](のhttp:// tonyz93 .blogspot.hk/2016/08/learn-data-binding-of-android.htmlを参照してください)。 – Tony

答えて

2

このシナリオでデータバインディングを使用するのは意味がありますか?

本当にアーキテクチャによって異なります。データバインディングは、MVVMアーキテクチャーと一緒に使用されることがよくあります(詳しくは、this exampleを参照してください)。しかし、いずれにせよ、あなたが一貫してあなたのコードを書くことを好む場合は、従来のfindViewById()でデータバインディングをミックスしたくないと、あなたはどこにでもデータバインディングを持っているしたいと思います。

実装は、他のデータバインディングの場合とあまり変わりません。ビューモデルは、このような活動にバインドする必要があります

public class MainActivityVM { 

    private ObservableField<String> mMutableText = new ObservableField<>(); 
    private ObservableBoolean mOptionEnabled = new ObservableBoolean(false); 

    public MainActivityVM() { 
     //Your stuff here. E.g., you can add a listener to track the switch toggle: 
     mOptionEnabled.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() { 
      @Override 
      public void onPropertyChanged(Observable sender, int propertyId) { 
       if (mOptionEnabled.get()) { 
        //Do some stuff if the user turned the switch on 
       } 
      } 
     }); 
     mMutableText.set("Hello world!"); 
    } 

    public ObservableField<String> getMutableText() { 
     return mMutableText; 
    } 

    public void setOptionEnabled(ObservableBoolean optionEnabled) { 
     this.mOptionEnabled = optionEnabled; 
    } 

    public ObservableBoolean isOptionEnabled() { 
     return mOptionEnabled; 
    } 
} 

<layout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"> 

<data> 

    <import type="android.view.View"/> 

    <variable 
     name="VM" 
     type="your.package.name.MainActivityVM"/> 
</data> 

<android.support.v4.widget.DrawerLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <!-- Put your main activity content here--> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:orientation="vertical"> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@{VM.mutableText}" 
       tools:text="Sample text" /> 

     <android.support.v7.widget.SwitchCompat 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="@={VM.optionEnabled}" 
      android:text="Option"/> 

    </LinearLayout> 
</android.support.v4.widget.DrawerLayout> 
</layout> 

ビューモデルに対応するゲッターを持つ必要があります(この例ではR.layout.act_mainという名前)のレイアウトは、双方向結合を持つべきです

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ActMainBinding binding = DataBindingUtil.setContentView(this, R.layout.act_main); 

     MainActivityVM viewModel = new MainActivityVM(); 
     binding.setVM(viewModel); 
    } 
} 
関連する問題