2017-12-15 17 views
0

Android 4.3 이상데이터 바인딩 : onClick 메서드를 호출하지 않음

데이터 바인딩을 사용합니다. 내 XML 레이아웃 파일에서

dataBinding { 
     enabled = true 
    } 

: 나는 그래서 응용 프로그램/build.gradle 여기에 Data binding

에서 공식 문서를 사용하여 여기

<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 :

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