数据绑定:不调用onClick方法

时间:2017-12-15 17:50:01

标签: android-databinding

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()不会被调用。 为什么呢?

2 个答案:

答案 0 :(得分:5)

您遇到了一个常见问题。首先,您必须使用绑定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();
}

答案 1 :(得分:0)

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