2017-12-15 7 views
0

Android 4.3+データバインディング:onClickメソッドを呼び出さない

私はデータバインディングを使用しています。

<layout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <data>  
     <variable 
      name="handler" 
      type="com.myproject.SettingsFragment" />  
    </data>  
    <android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent">  
     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"    > 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <android.support.constraint.ConstraintLayout 
        android:id="@+id/contentContainer" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

        <android.support.constraint.ConstraintLayout 
         android:id="@+id/contactUsContainer" 
         android:layout_width="match_parent" 
         android:onClick="@{handler::onClickContactUs}">  

         <TextView 
          android:id="@+id/contactUsTextView" 
          android:layout_width="0dp" 
          android:layout_height="wrap_content"/>  

        </android.support.constraint.ConstraintLayout>   
       </android.support.constraint.ConstraintLayout> 
      </FrameLayout> 
     </ScrollView> 
    </android.support.constraint.ConstraintLayout>  
</layout> 

そして、ここで私のフラグメントSettingsFragment.java:私のXMLレイアウトファイルで

dataBinding { 
     enabled = true 
    } 

:私はアプリ/ build.gradleでだからここData binding

から公式ドキュメントを使用します:

public class SettingsFragment extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.settings, container, false); 
     return rootView; 
    } 

    public void onClickContactUs(View view) {  
    } 
} 

しかし、コンテナcontactUsContainerをクリックすると、onClickContactUs()メソッドは呼び出されません。 なぜですか?

答えて

1

よくある問題が発生しています。まず、バインディングinflate()呼び出しを使用してバインディングを膨張させる必要があります。次に、バインディング変数を設定する必要があります。

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    MyLayoutBinding binding = MyLayoutBinding.inflate(inflater, container, false); 
    binding.setHandler(this); 
    return binding.getRoot(); 
} 
関連する問題