我在android studio中使用数据绑定库,我创建了处理程序来检测我正在使用以下代码的onlcik()
事件
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="handlers"
type="com.example.MainActivity.MyHandlers"/>
</data>
<RelativeLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<include
android:layout_height="wrap_content"
layout="@layout/grid_layout_carinfo"
app:includedViewGroup="@{1}"
bind:handlers="@{handlers}"
android:layout_width="wrap_content"/>
</RelativeLayout>
</layout>
grid_layout_carinfo.xml
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="handlers"
type="com.example.MainActivity.MyHandlers"/>
<variable
name="includedViewGroup"
type="int"/>
</data>
<GridLayout
android:id="@+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:columnCount="3"
android:rowCount="4"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/driver_info_view_margin"
android:layout_marginRight="@dimen/driver_info_view_margin"
android:layout_column="0"
android:layout_row="0">
<ImageView
android:id="@+id/car_photo_front_view"
android:src="@mipmap/abc"
android:layout_marginTop="8dp"
android:onClick="@{(view)->handlers.onCarPhotoFrontView(view)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="ContentDescription" />
</LinearLayout>
</GridLayout>
</layout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
}
public class MyHandlers {
public void onClickItem(View view){
}
}
}
当我尝试构建项目时,我收到错误
Error:(134, 29) error: cannot find symbol method setHandler(MainActivity.MyHandlers)
答案 0 :(得分:2)
搜索之后我找到了bind:handlers="@{handlers}"
中的名称必须相同的答案,该名称在data
标记的包含视图中使用,清理并重建项目并运行