我在使用 Glide 库在原始项目的 RecyclerView 中显示图像时遇到了问题。为了跟踪问题,我创建了一个简单的应用程序用于测试目的,以确定相同的问题是否会持续存在。
虽然我已经使用 Glide 库很长时间了,但这是我第一次遇到这个特殊问题。你能解释一下为什么图书馆显示的图像有故障效果吗?
这里是代码,看看是否有任何可能导致此问题的原因:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_products"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:overScrollMode="never"
android:paddingHorizontal="2.5dp"
android:paddingVertical="2.5dp"
android:scrollbars="none"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="3"
tools:listitem="@layout/row_product" />
</androidx.constraintlayout.widget.ConstraintLayout>
row_product.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<com.google.android.material.card.MaterialCardView
style="@style/Widget.Material3.CardView.Elevated"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2.5dp"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/rowProduct_imageView_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#66000000"
android:scaleType="centerCrop"
android:visibility="gone"
tools:ignore="ContentDescription" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/rowProduct_materialTextView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_margin="7.5dp"
android:ellipsize="end"
android:lineSpacingExtra="0dp"
android:maxLines="3"
android:textSize="11sp"
app:fontFamily="@font/font_bold" />
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
ProductModel.java
package com.test.app;
public class ProductModel {
private int id;
private String image;
private String title;
public ProductModel(int id, String image, String title) {
this.id = id;
this.image = image;
this.title = title;
}
public int getId() {
return id;
}
public String getImage() {
return image;
}
public String getTitle() {
return title;
}
}
ProductsAdapter.java
package com.test.app;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.test.app.databinding.RowProductBinding;
import java.util.List;
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.VH> {
private Context context;
private List<ProductModel> productsList;
public ProductsAdapter(Context context, List<ProductModel> productsList) {
this.context = context;
this.productsList = productsList;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new VH(RowProductBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
holder.rowProductBinding.rowProductMaterialTextViewTitle.setText(productsList.get(position).getTitle());
if (productsList.get(position).getImage() != null)
Glide.with(context).load(productsList.get(position).getImage()).addListener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
holder.rowProductBinding.rowProductImageViewImage.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
holder.rowProductBinding.rowProductImageViewImage.setVisibility(View.VISIBLE);
holder.rowProductBinding.rowProductImageViewImage.setImageDrawable(resource);
return false;
}
}).preload();
else
holder.rowProductBinding.rowProductImageViewImage.setImageDrawable(null);
}
@Override
public int getItemCount() {
return productsList.size();
}
@Override
public long getItemId(int position) {
return position;
}
public static class VH extends RecyclerView.ViewHolder {
private final RowProductBinding rowProductBinding;
public VH(@NonNull RowProductBinding rowProductBinding) {
super(rowProductBinding.getRoot());
this.rowProductBinding = rowProductBinding;
}
}
}
MainActivity.java
package com.test.app;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.test.app.databinding.ActivityMainBinding;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(activityMainBinding.getRoot());
List<ProductModel> productsList = new ArrayList<>();
productsList.add(new ProductModel(1, "https://i.ebayimg.com/images/g/qZsAAOSwIRxirTew/s-l1600.jpg", "Fragrance One Date for Men 3.4oz / 100ml NEW IN BOX BNIB"));
productsList.add(new ProductModel(2, "https://i.ebayimg.com/images/g/sqsAAOSwwIJkRBT-/s-l1600.jpg", "Office For Men Batch 3 By Fragrance One Jeremy Fragrance"));
productsList.add(new ProductModel(3, "https://i.ebayimg.com/images/g/0E8AAOSwWcRkRWan/s-l1600.jpg", "Club De Nuit Intense Man LIMITED EDITION - PARFUM. by ARMAF Fragrance"));
productsList.add(new ProductModel(4, "https://i.ebayimg.com/images/g/b4gAAOSwKwFkB6y~/s-l1600.jpg", "Chanel Bleu De Chanel Parfum Pour Homme 3.4 Ounces"));
productsList.add(new ProductModel(5, "https://i.ebayimg.com/images/g/x-IAAOSwF~pkUtsS/s-l1600.jpg", "1 Million prive By Paco Rabanne Fragrance"));
productsList.add(new ProductModel(6, "https://i.ebayimg.com/images/g/l8cAAOSwK2xkRsA7/s-l1600.jpg", "Followed 2023 By KEROSENE Fragrances"));
productsList.add(new ProductModel(7, "https://i.ebayimg.com/images/g/ZIMAAOSw8ItjYU4i/s-l500.jpg", "FRAGRANCE YVES SAINT LAURENT LA NUIT DE L'HOMME EDT FOR MEN 100 ml SPRAY"));
productsList.add(new ProductModel(8, "https://i.ebayimg.com/images/g/ZfgAAOSwYJVkEuqu/s-l1600.jpg", "Tommy Bahama By Tommy Bahama Fragrance"));
productsList.add(new ProductModel(9, "https://i.ebayimg.com/images/g/9j4AAOSwTFZiDhu4/s-l1600.jpg", "Vintage Davidoff Good Life EDT 125ml men's perfume"));
activityMainBinding.recyclerViewProducts.setAdapter(new ProductsAdapter(MainActivity.this, productsList));
}
}