Пытаюсь получить ссылку "src"
на одно изображение из List<Images>
используя DataBinding
, но не могу до конца понять как. Без использования DataBinding
получал ссылку на изображение в onBindViewHolder
следующим образом:
String imagePath = products.get(position).getImages().get(0).getSrc();
Получаю следующую ошибку:
Cannot find a setter for <android.widget.ImageView bind:mainImagePath> that accepts parameter type 'java.util.List<com.example.lafiores.model.product.Image>'
If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.
Мой код:
public class Product extends BaseObservable implements Parcelable {
@SerializedName("images")
@Expose
private List<Image> images = null;
@BindingAdapter({"mainImagePath"})
public static void loadMainImage(ImageView imageView, String imageUrl) {
Glide
.with(imageView)
.load(imageUrl)
.into(imageView);
}
@Bindable
public List<Image> getImages() {
return images;
}
public void setImages(List<Image> images) {
this.images = images;
notifyPropertyChanged(BR.images);
}}
Класс Image
public class Image extends BaseObservable implements Parcelable {
@SerializedName("src")
@Expose
private String src;
@Bindable
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
notifyPropertyChanged(BR.src);
}}
Adapter
public class ListProductAdapter extends RecyclerView.Adapter<ListProductAdapter.ListProductViewHolder> {
private Context context;
private ArrayList<Product> products = new ArrayList<Product>();
private Application application;
public ListProductAdapter(Context context, ArrayList<Product> products) {
this.context = context;
this.products = products;
}
@NonNull
@Override
public ListProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ListProductItemBinding listProductItemBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()),
R.layout.list_product_item,
parent,
false);
return new ListProductViewHolder(listProductItemBinding);
}
public ListProductAdapter(Application application) {
this.application = application;
}
@Override
public void onBindViewHolder(@NonNull ListProductViewHolder holder, int position) {
Product product = products.get(position);
Product product = products.get(position);
holder.listProductItemBinding.setProduct(product);
}
@Override
public int getItemCount() {
return products.size();
}
class ListProductViewHolder extends RecyclerView.ViewHolder {
private ListProductItemBinding listProductItemBinding;
public ListProductViewHolder(@NonNull ListProductItemBinding listProductItemBinding) {
super(listProductItemBinding.getRoot());
this.listProductItemBinding = listProductItemBinding;
listProductItemBinding.getRoot().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, DetailProductActivity.class);
itemView.getContext().startActivity(intent);
}
});
}
}}
XML
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="product"
type="com.example.lafiores.model.product.Product" />
</data>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<ImageView
android:id="@+id/listProductItemImageView"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:scaleType="fitCenter"
bind:mainImagePath="@{product.images}" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
UPDATE:
@BindingAdapter({"mainImagePath"})
public static void loadMainImage(ImageView imageView, List<Image> images) {
Glide
.with(imageView)
.load(images.get(0).getSrc()) //вывожу первую картинку
.into(imageView);
}
<ImageView
android:id="@+id/listProductItemImageView"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:scaleType="fitCenter"
bind:mainImagePath="@{product.images}" />