Android Studio 3.6
build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
viewBinding {
enabled = true
}
dataBinding {
enabled = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
在某些班级,例如MyAdapter我创建带有注释loadImage
的公共静态方法@BindingAdapter
:
public class MyAdapter extends DataBindingRecyclerViewAdapter {
public MyAdapter(Context context, List<?> data) {
super(context, data);
}
@Override
protected int getLayoutForPosition(int position) {
return R.layout.forecast_data_list_item;
}
@BindingAdapter({"app:weatherImage"})
public static void loadImage(ImageView view, String imageUrl) {
Glide.with(view.getContext())
.load(imageUrl).apply(new RequestOptions().circleCrop())
.into(view);
}
}
在xml中:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="model"
type="com.myproject.model.SomeModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/invoiceMainContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_margin="@dimen/half_default_margin"
android:scaleType="centerCrop"
android:src="@drawable/default_image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/containerRight"
app:layout_constraintStart_toStartOf="parent"
app:weatherImage="http://www.gravatar.com/avatar/11111?s=200x200"
app:layout_constraintTop_toTopOf="parent" />
但是出现编译错误:
AAPT: error: attribute weatherImage (aka com.myproject:weatherImage) not found.
在您的BindingAdapter中