Why is the Glide library displaying glitchy images?
- Dominant language
- Java
- Stars
- 35k
- Forks
- 6.2k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 8
Description
I faced a problem while using the Glide library to display images within a RecyclerView in my original project. To track the problem, I created a simple app for testing purposes to determine if the same problem would persist or not.
[Here is a video demonstrating the problem on YouTube.](https://www.youtube.com/v/HdIZ8UU6xAk)
Although I have been using the Glide library for a considerable amount of time, this is the first time I have encountered this particular issue. Could you explain why the library is displaying images with a glitch effect?
**Here is the code to see if there are any potential causes for this problem:**
_activity_main.xml_
```
```
_row_product.xml_
```
```
_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 {
private Context context;
private List productsList;
public ProductsAdapter(Context context, List 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() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
holder.rowProductBinding.rowProductImageViewImage.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target 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 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));
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.