Android 为什么不从子适配器调用方法?
我使用数据绑定 这里是父适配器:Android 为什么不从子适配器调用方法?,android,android-databinding,Android,Android Databinding,我使用数据绑定 这里是父适配器: public abstract class PreviewSortAdapter extends RealmRecyclerViewAdapter { protected Context context; @BindingAdapter("imageUrl") public static void loadImage(ImageView view, String imageUrl) { Glide.with(view.g
public abstract class PreviewSortAdapter extends RealmRecyclerViewAdapter {
protected Context context;
@BindingAdapter("imageUrl")
public static void loadImage(ImageView view, String imageUrl) {
Glide.with(view.getContext()).load(imageUrl)
.apply(RequestOptions.bitmapTransform(
new GlideRoundedCornersTransformation(view.getContext(), (int) AndroidUtil.dpToPx(view.getContext(),
view.getContext().getResources().getInteger(R.integer.image_rounded_corner_radius_dp)),
0, GlideRoundedCornersTransformation.CornerType.TOP)))
.into(view);
}
}
这里是我的孩子适配器:
public class MapListSortAdapter extends PreviewSortAdapter {
public MapListSortAdapter(Context context, OrderedRealmCollection<Merchant> data) {
super(context, data, true);
}
@BindingAdapter("imageUrl")
public static void loadImage(ImageView view, String imageUrl) {
Debug.d(TAG, "loadImage: ");
Glide.with(view.getContext()).load(imageUrl)
.into(view);
}
@Override
protected int getLayoutForPosition(int position) {
return R.layout.map_list_item;
}
}
如您所见,我使用自定义标记app:imageUrl
调用方法loadImage()
。
问题是该方法是call,但它调用父适配器-PreviewSortAdapter.loadImage()
但是我需要在子适配器中调用此方法:
MapListSortAdapter.loadImage()
使用static修饰符指定的方法没有层次性。只需删除静态修饰符,它就可以工作了在数据绑定中,我可以使用非静态方法从xml调用吗?这里是Google的官方示例:BindingAdapter({“bind:imageUrl”,“bind:error”})公共静态void loadImage(ImageView视图,字符串url,可绘制错误){Picasso.with(view.getContext()).load(url).error(error).into(view);
}是的,这是可能的。您可以调用在XML中传递的变量的任何方法。如果您遇到此错误,是因为您试图在没有类的对象实例的情况下调用非静态方法。尝试使用所需的方法(loadImage())传递对象。在xml中:app:loadImage=“@{(item)->clickHandler.loadImageRounded(item)}”
但出现错误:@BindingAdapter loadImageRounded(com.myproject.android.customer.api.model.Offer)有1个属性和0个值参数。应该有1个或2个值参数。public void loadImageRounded(Offer Offer){
如果使用非静态BindingAdapter,则必须提供一个DataBindingComponent
。例如,使用DataBindingUtil.setDefaultComponent()
。它用于告诉数据绑定在绑定调用中使用哪个实例。
<?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="item"
type="com.myproject.android.customer.api.model.Merchant" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:minHeight="90dp">
<ImageView
android:id="@+id/imageViewPhoto"
android:layout_width="90dp"
android:layout_height="90dp"
android:scaleType="centerCrop"
app:imageUrl="@{item.preview.formats.reference.url}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>