Android 4.3+
我尝试使用数据绑定。我使用qazxsw poi的官方文档
所以在app / build.gradle中:
Data binding
在我的xml布局文件中:
dataBinding {
enabled = true
}
在这里我的片段SettingsFragment.java:
<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>
但是,当我点击容器contactUsContainer时,方法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) {
}
}
不会被调用。为什么?
您遇到了一个常见问题。首先,您必须使用绑定inflate()调用来扩展绑定。其次,您必须设置绑定变量:
onClickContactUs()