我正在设置带有产品列表的recyclerview,当我向上/向下滚动时,其中的某些项会更改其内容。
我已经调试了我的代码,但是产品列表没有更改。它仅出现在此回收站视图中。
我的问题在这里是视觉的。
这是适配器代码
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
productItemBinding = DataBindingUtil.inflate(inflater, R.layout.product_item, parent, false);
return new ProductViewHolder(productItemBinding);
}
@Override
public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
Product product = mProductList.get(position);
holder.bindHolder(product);
}
public void setData(List<Product> productList) {
mProductList = productList;
notifyDataSetChanged();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
ProductViewHolder(@NonNull ProductItemBinding productItemBinding) {
super(productItemBinding.getRoot());
}
void bindHolder(Product product) {
productItemBinding.setProducts(product);
productItemBinding.setAdapter(ProductAdapter.this);
productItemBinding.executePendingBindings();
}
}
这是我给适配器的电话
productViewModel.getActionProducts("sale", parameter).observe(this, productsContainer -> {
productsArrayList = new ArrayList<>();
productsArrayList = actionProductsContainer.getData().getProducts();
activityProductListingBinding.rvProducts.setLayoutManager(new GridLayoutManager(this, 2));
productsAdapter = new ProductsAdapter(this, this);
activityProductListingBinding.rvProducts.setAdapter(productsAdapter);
productsAdapter.setData(productsArrayList);
activityProductListingBinding.pbLoadingActionProducts.setVisibility(View.GONE);
});
这是我用于recyclerview的XML。
<data>
<!--<import type="com.workout.height.helper.AppUtils" />-->
<variable
name="products"
type="com.lalaland.ecommerce.data.models.products.Product" />
<variable
name="adapter"
type="com.lalaland.ecommerce.adapters.ProductAdapter" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/iv_product"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:layout_constraintDimensionRatio="3:4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:setProductImage="@{products.primaryImage}" />
<LinearLayout
android:id="@+id/info_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iv_product">
<TextView
android:id="@+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{products.brandName}"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold"
tools:text="@tools:sample/full_names" />
<TextView
android:id="@+id/tv_product_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="20"
android:maxLines="1"
android:text="@{products.quantity}"
android:textColor="@android:color/black"
tools:text="@tools:sample/lorem/random" />
<TextView
android:id="@+id/tv_product_current_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{products.salePrice}"
android:textColor="@android:color/holo_red_dark"
android:textSize="18sp"
tools:text="@tools:sample/us_zipcodes" />
<TextView
android:id="@+id/tv_product_pre_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{products.actualPrice}"
android:textColor="@android:color/black"
tools:text="@tools:sample/us_zipcodes" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是自定义绑定以加载图像
@BindingAdapter("setProductImage")
public static void setImageFromServer(ImageView imageView, String imageName) {
String imageSrc = PRODUCT_STORAGE_BASE_URL.concat(imageName);
Glide
.with(imageView.getContext())
.load(imageSrc)
.placeholder(R.drawable.place_holder)
.fitCenter()
.apply(RequestOptions.centerInsideTransform())
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.into(imageView);
}