数据绑定:不调用 onClick 方法

Data binding: Not call onClick method

Android 4.3+

我想使用数据绑定。我使用这里的官方文档 Data binding

所以在app/build.gradle:

dataBinding {
        enabled = true
    }

在我的 xml 布局文件中:

<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() 没有被调用。 为什么?

您遇到了一个常见问题。首先,您必须使用绑定 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();
}
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

userDetailViewBinding=DataBindingUtil.inflate(inflater,R.layout.fragment_user_detail_view, container, false);
        ProfileViewModel.Factory factory = new ProfileViewModel.Factory(getActivity().getApplication());

        viewModel = ViewModelProviders.of(this, factory)
                .get(ProfileViewModel.class);
        observeViewModel(viewModel);
        userDetailViewBinding.setProfileViewModel(viewModel);
        userDetailViewBinding.setUserDetailViewFrag(this);
        return userDetailViewBinding.getRoot();
    }