2017-01-30 3 views
5

activity_layout.xmlデータバインディングボタンのonclick動作しない

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 

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

     <variable 
      name="callback" 
      type="com.buscom.ActionCallBack" /> 
    </data> 

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/ll_oml" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/grey_50" 
     android:orientation="vertical"> 

     <android.support.design.widget.CoordinatorLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:onClick="@{(v) -> callback.onClick(v)}" 
       android:text="Menu" /> 
     </android.support.design.widget.CoordinatorLayout> 
    </LinearLayout> 
</layout> 

ActionCallBack.java

これは

MainActivity.java私はMainActivity

public interface ActionCallback { 
    void onClick(View view); 
} 

に実装するインタフェースです

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); actionCallBack = new ActionCallBack() { @Override public void onClick(View view) { System.out.println("Call onclick method *****"); } } } 

ボタンをクリックするとonClick()メソッドが呼び出されず、出力が表示されるか、または何も実行されません。しかし、伝統的な方法でonClickListenerを使って作業しています

答えて

14

あなたの活動宣言に間違いがあると思います。とにかく、あなたはまだのような、あなたのコールバックを設定されていません。

binding.setCallback(this);

または

binding.setCallback(actionCallback);

+0

をAndroidのメーカーは、バインディングタイプを探します。あなたが記述した間違いは明らかではありません – Sergey

+0

@Sergeyもしあなたが少し注意を払うなら、彼のコードがうまく見えないので私が質問を編集したのを見たことがあるでしょう、それは私が意味するエラーです。あなたがそれが何であると思っても、 – Chisko

+0

@Chiskoそれは今、とても感謝しています。 'binding.setCallBack(actionCallback);'解決済み –

関連する問題